2016-05-17 158 views
0

感謝您的回答。現在是工作,但我有下一個問題。 我重拍的方法從數據庫中顯示的記錄:從數據庫返回一條記錄

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>"; 

的問題是,它顯示只有一個記錄從數據庫中。 當我加入,而回聲:

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']; 
} 

顯示的所有記錄,但通過:

$mag->magazyn(); 

enter image description here

+0

那麼......問題是什麼?它應該顯示多少條記錄? – Mike

+0

你在循環中覆蓋你的變量,你應該做出一個多維數組的輸出 – 2016-05-17 21:59:02

+0

的問題是:我如何返回表中的記錄(

first_record second_reord ...
)?應該在藍色表 – major697

回答

1

由於袞的評論上面說,加入你的價值觀,以多維數組。取而代之的

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

只是做

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

那你要所有的記錄追加到該數組:在你的HTML你循環

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"; 
    } 
} 

然後在$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

顯示2記錄我已經打了一半,然後我放棄了:-) – 2016-05-17 22:27:26

+0

@Dagon我以爲你不想發佈任何東西,你不想。 – Mike

+0

通過使用foreach不顯示錶。返回空白頁 – major697