2017-06-07 39 views
0

我想從數據庫中生成一個列表從數據庫中,就像下面的圖像;PHP HTML表檢索多個數據在一個​​標籤

https://i.stack.imgur.com/61XLl.png

這裏是我所做的;

https://i.stack.imgur.com/lLsvF.png

和代碼;

<table cellpadding="3" border="1" style="width:100%;margin-top:30px; margin-bottom:50px; font-size:12px"> 
    <thead>  
     <tr> 
      <th>KURSUS</th> 
      <th rowspan="2">NAMA PENSYARAH</th> 
      <th rowspan="2">NO. SIRI</th> 
     </tr> 
     <tr> 
      <th>NAMA</th> 
     </tr> 
    </thead> 
    <tbody align="center"> 
     <?php 
      if($numrow>0) 
      { 
       while($row = $select->fetch_assoc()){ 

        $code=explode("/",$row['po_code']); 
        $list=$connect->query("SELECT * FROM polist WHERE polist_poid='".$row['po_id']."' ORDER BY polist_bil ASC"); 
        ?> 

     <tr> 
      <td><?php echo $row['po_name']; ?></td> 
      <?php while($rowlist = $list->fetch_assoc()){ 

        $name=$connect->query("SELECT * FROM user WHERE user_id='".$rowlist['polist_userid']."'"); 
        $rowname=$name->fetch_array();?> 
      <td><?php echo $rowname['user_name']; ?></td> 
      <td><?php echo $code[0]."/PO/".$code[1]." - ".$rowlist['polist_bil']; ?></td> 
      <?php } ?> 
     </tr> 

        <?php 
       } 
      } 
     ?> 
    </tbody> 
</table> 

幫幫我。預先感謝您:)

回答

0

使用此代碼。 Concat用戶名和代碼在第二個while循環中帶有「br」標籤,並在while循環後顯示在「tds」中。

<tbody align="center"> 
     <?php 
      if($numrow>0) 
      { 
       while($row = $select->fetch_assoc()){ 

        $code=explode("/",$row['po_code']); 
        $list=$connect->query("SELECT * FROM polist WHERE polist_poid='".$row['po_id']."' ORDER BY polist_bil ASC"); 
        ?> 

     <tr> 
      <td><?php echo $row['po_name']; ?></td> 
      <?php 
       $user_names = $codes = ''; // define empty variables 
       while($rowlist = $list->fetch_assoc()){ 

        $name=$connect->query("SELECT * FROM user WHERE user_id='".$rowlist['polist_userid']."'"); 
        $rowname=$name->fetch_array(); 
        $user_names .= $rowname['user_name']."<br/>"; //concat to a single string 
        $codes .= $code[0]."/PO/".$code[1]." - ".$rowlist['polist_bil']."<br/>"; //concat to a single string 
      }?> 
      <td><?php echo $user_names;?></td> 
      <td><?php echo $codes;?></td> 
     </tr> 

        <?php 
       } 
      } 
     ?> 
    </tbody> 
+0

非常感謝你,它的工作原理! – zyod

0

<td><?php while($rowlist = $list->fetch_assoc()){

外部或您的所有數據開始顯示HT​​ML並將其存儲在一個多維數組前。然後簡單地遍歷數據數組。這樣你就不會有像PHP那樣多的PHP。