2016-05-17 12 views
0

Danke für die Antwort. Jetzt ist es Arbeit, aber ich habe nächstes Problem. ich neu gemacht, ein Verfahren Datensätze aus der Datenbank anzuzeigen:geben Sie einen Datensatz aus der Datenbank

public $id, $nazwa, $quantity, $data, $godzina, $sn, $added, $kategoria, $numrows; 

    public function magazyn() { 
     $resultmag = $this->magazyn = $this->conn->prepare("SELECT * FROM `products` ORDER BY `id`"); 
     $resultmag = $this->conn->set_charset("utf8"); 
     $resultmag = $this->magazyn->execute(); 
     $resultmag = $this->magazyn->get_result(); 

     if ($isset = $resultmag->num_rows > 0) { 
      while ($row = $resultmag->fetch_object()) { 
      $this->id = $row->id; 
      $this->nazwa = $row->nazwa; 
      $this->quantity = $row->quantity; 
      $this->data = $row->data; 
      $this->godzina = $row->godzina; 
      $this->sn = $row->sn; 
      $this->added = $row->added; 
      $this->kategoria = $row->kategoria; 
      } 
      $this->numrows = $resultmag->num_rows; 
     } else { 
      echo "No results database"; 
     } 
    } 

    function __toString() { 
     return (string) $this->numrows; 
    } 

    //------------------------------- 
    $mag = new Magazyn(); 
    $mag->magazyn(); 

    ?> 
      <table class="table table-bordered"> 
       <tr><th style='text-align:center'>ID</th><th style='text-align:center'>DODAŁ</th><th style='text-align:center'>NAZWA</th><th style='text-align:center'>KATEGORIA</th><th style='text-align:center'>SERIAL</th><th style='text-align:center'>ILOŚĆ</th><th style='text-align:center'>DATA</th><th style='text-align:center'>GODZINA</th><th style='text-align:center' colspan='2'>AKCJA</th></tr> 
       <?php 
    $color = NULL; 
    $color1 = '#99bbff'; 
    $color2 = '#b3ccff'; 

    $color == $color1 ? $color = $color2 : $color = $color1; 
    echo "<tr class='active'>"; 
    echo "<td align='center' style='background-color:" . $color . ";'>" . $mag->id . "</td>"; 
    echo "<td align='center' style='background-color:" . $color . ";'><a href='panel.php?page=userpanel&user=" . $mag->added . "'>" . $mag->added . "</a></td>"; 
    echo "<td align='center' style='background-color:" . $color . ";'>" . $mag->nazwa . "</td>"; 
    echo "<td align='center' style='background-color:" . $color . ";'><a href='panel.php?page=category&cat=" . $mag->kategoria . "'>" . $mag->kategoria . "</a></td>"; 
    echo "<td align='center' style='background-color:" . $color . ";'><a href='panel.php?page=editserial&sn=" . $mag->sn . "'>" . $mag->sn . "</a></td>"; 
    echo "<td align='center' style='background-color:" . $color . ";'>" . $mag->quantity . "</td>"; 
    echo "<td align='center' style='background-color:" . $color . ";'>" . $mag->data . "</td>"; 
    echo "<td align='center' style='background-color:" . $color . ";'>" . $mag->godzina . "</td>"; 
    echo "<td align='center'><a href='panel.php?page=magazyn&action=edit&sn=" . $mag->sn . "&nazwa=" . $mag->nazwa . "&kategoria=" . $mag->kategoria . "&id=" . $mag->id . "'</a><button class='btn btn-default'>Edytuj</button></td>"; 
    echo "<td align='center'><a href='panel.php?page=magazyn&action=delete&sn=" . $mag->sn . "'</a><button class='btn btn-default'>Usuń</button></td>"; 
    echo "</tr>"; 

Das Problem ist, dass es zeigt nur einen Datensatz aus der Datenbank. Als ich während Echo hinzu:

while ($row = $resultmag->fetch_array()) { 
    echo $this->id = $row['id']; 
    echo $this->nazwa = $row['nazwa']; 
    echo $this->quantity = $row['quantity']; 
    echo $this->data = $row['data']; 
    echo $this->godzina = $row['godzina']; 
    echo $this->sn = $row['sn']; 
    echo $this->added = $row['added']; 
    echo $this->kategoria = $row['kategoria']; 
} 

Zeigt alle Datensätze, sondern durch:

$mag->magazyn(); 

enter image description here

+0

Also ... Was ist die Frage? Wie viele Datensätze sollte es anzeigen? – Mike

+0

überschreiben Sie Ihre Variablen in der Schleife, sollten Sie wahrscheinlich ein multidimensionales Array der Ausgabe machen –

+0

die Frage ist: Wie gebe ich Datensätze in der Tabelle zurück (

first_record second_reord ...
) ?? Sollte ich 2 Datensätze in der blauen Tabelle anzeigen – major697

Antwort

1

Wie Dagon Kommentar oben sagt, Ihre Werte zu einem mehrdimensionalen Array hinzuzufügen. Statt

public $id, $nazwa, $quantity, $data, $godzina, $sn, $added, $kategoria, $numrows; 

Just do

public $records = array(); 
public $numrows; 

Dann werden Sie alle Datensätze zu diesem Array anhängen:

public function magazyn() { 
    $resultmag = $this->magazyn = $this->conn->prepare("SELECT * FROM `products` ORDER BY `id`"); 
    $resultmag = $this->conn->set_charset("utf8"); 
    $resultmag = $this->magazyn->execute(); 
    $resultmag = $this->magazyn->get_result(); 

    if ($isset = $resultmag->num_rows > 0) { 
     while ($row = $resultmag->fetch_object()) { 
     $record['id'] = $row->id; 
     $record['nazwa'] = $row->nazwa; 
     // etc... Add all like above 

     $this->records[] = $record; 
     } 
     $this->numrows = $resultmag->num_rows; 
    } else { 
     echo "No results database"; 
    } 
} 

Dann in Ihrem HTML Sie Schleife über $mag->records:

<table class="table table-bordered"> 
    <?php foreach ($mag->records as $record) : ?> 
     <tr><th style='text-align:center'>ID</th><th style='text-align:center'>DODAŁ</th><th style='text-align:center'>NAZWA</th><th style='text-align:center'>KATEGORIA</th><th style='text-align:center'>SERIAL</th><th style='text-align:center'>ILOŚĆ</th><th style='text-align:center'>DATA</th><th style='text-align:center'>GODZINA</th><th style='text-align:center' colspan='2'>AKCJA</th></tr> 
     <?php 
     $color = NULL; 
     $color1 = '#99bbff'; 
     $color2 = '#b3ccff'; 

     $color == $color1 ? $color = $color2 : $color = $color1; 
     echo "<tr class='active'>"; 
     echo "<td align='center' style='background-color:" . $color . ";'>" . $record['id'] . "</td>"; 
     echo "<td align='center' style='background-color:" . $color . ";'><a href='panel.php?page=userpanel&user=" . $record['added'] . "'>" . $record['added'] . "</a></td>"; 
     echo "<td align='center' style='background-color:" . $color . ";'>" . $record['nazwa'] . "</td>"; 
     echo "<td align='center' style='background-color:" . $color . ";'><a href='panel.php?page=category&cat=" . $record['kategoria'] . "'>" . $record['kategoria'] . "</a></td>"; 
     echo "<td align='center' style='background-color:" . $color . ";'><a href='panel.php?page=editserial&sn=" . $record['sn'] . "'>" . $record['sn'] . "</a></td>"; 
     echo "<td align='center' style='background-color:" . $color . ";'>" . $record['quantity'] . "</td>"; 
     echo "<td align='center' style='background-color:" . $color . ";'>" . $record['data'] . "</td>"; 
     echo "<td align='center' style='background-color:" . $color . ";'>" . $record['godzina'] . "</td>"; 
     echo "<td align='center'><a href='panel.php?page=magazyn&action=edit&sn=" . $record['sn'] . "&nazwa=" . $record['nazwa'] . "&kategoria=" . $record['kategoria'] . "&id=" . $record['id'] . "'</a><button class='btn btn-default'>Edytuj</button></td>"; 
     echo "<td align='center'><a href='panel.php?page=magazyn&action=delete&sn=" . $record['sn'] . "'</a><button class='btn btn-default'>Usuń</button></td>"; 
     echo "</tr>"; 
    endforeach; 
    ?> 
</table> 
+0

hatte ich diese Hälfte getippt, dann gab ich auf :-) –

+0

@Dagon Ich nahm an, du wolltest das nicht, da du nichts gepostet hast. – Mike

+0

Mit foreach wird keine Tabelle angezeigt. Eine leere Seite zurückgeben – major697

Verwandte Themen