生成HTML以从PHP并排放置两个表

时间:2015-01-02 17:18:46

标签: php html css

我希望在HTML表格中有两个彼此相邻的表格(由不同的列排序)。尝试:

 <tbody>
   <td>
     <?php
        require_once "db_data.php";
        $bids_results = $mysqli->query("SELECT bid_volume, bid FROM apple_bids ORDER BY bid DESC");
        while($bids = $bids_results->fetch_array()) {
          echo "<tr>\n\t<td>"
          . $bids['bid_volume']
          . "</td>\n\t<td>"
          . $bids['bid']
          . "</td>\n</tr>\n";
        }
        $bids_results->close();
     ?>
   </td>    
   <td>     
     <?php                          
       $offers_results = $mysqli->query("SELECT offer_volume, offer FROM apple_offers ORDER BY offer DESC");
       while($offers = $offers_results->fetch_array()) {
         echo "<tr>\n\t<td>"
         . $offers['offer']
         . "</td>\n\t<td>"
         . $offers['offer_volume']
         . "</td>\n</tr>\n" ;
       }
       $offers_results->close();
       $mysqli->close();    
     ?> 
   </td>  
 </tbody>

但由于某种原因,第二张表格位于第一张表格的下方。如果我将两个<td>放在<tr>

下面的<tbody>中,也会发生同样的情况

2 个答案:

答案 0 :(得分:1)

您不应该使用tbody,因为浏览器会生成<table> <tr> <td> <table> <?php require_once "db_data.php"; $bids_results=$ mysqli->query("SELECT bid_volume, bid FROM apple_bids ORDER BY bid DESC"); while($bids = $bids_results->fetch_array()) { echo " <tr> <td>" . $bids['bid_volume'] . "</td> <td>" . $bids['bid'] . "</td> </tr>"; } $bids_results->close(); ?> </table> </td> <td> <table> <?php $offers_results=$ mysqli->query("SELECT offer_volume, offer FROM apple_offers ORDER BY offer DESC"); while($offers = $offers_results->fetch_array()) { echo " <tr> <td>" . $offers['offer'] . "</td> <td>" . $offers['offer_volume'] . "</td> </tr>" ; } $offers_results->close(); $mysqli->close(); ?> </table> </td> </tr> </table> 。使用您的代码,正确的表结构将类似于以下内容:

{{1}}

答案 1 :(得分:0)

试试这个标记:

<table>
     <tr>
          <td>
               <!-- PHP output here -->
          </td>
          <td>
               <!-- PHP output here -->
          </td>
     </tr>
</table>