2017-06-07 27 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。