html php没有正确对齐每列

时间:2014-04-28 16:01:10

标签: php

我试图在第一个查询中运行两个循环来检索列。第二个查询获取第一个表的主id并执行查询。 最终结果应该与图像中给出的相似

enter image description here 我无法给出正确的对齐

以下是代码

<table  width="100%" border="1" cellspacing="1" cellpadding="1"   >
<tr>
    <td>Col1</td>
    <td>col2</td>
    <td>col3</td>
    <td>col4/td>
  </tr>
<?php 

$mast = mysql_query("select * from table1 where av_master_id='".$_REQUEST['id']."'");
      while($res_mas= mysql_fetch_assoc($mast))
      {
      ?>
      <tr>
    <td><?php echo $res_mas['col1'];?></td>
    <?php 
     $room=  mysql_query("SELECT * FROM `table2` WHERE  av_room_id='".$res_mas['av_room_id']."'");

     while($res_room= mysql_fetch_assoc($room))
      {
      ?>

    <td><?php echo $res_room['col2'];?></td>
    <td><?php echo $res_room['col3'];?></td>
     <td><?php echo $res_room['col4'];?></td>


    </td>



    </tr><tr>

  <?php }?>
   </tr>

  <?php } ?>



  </table>

1 个答案:

答案 0 :(得分:1)

这应该这样做:

<table width="100%" border="1" cellspacing="1" cellpadding="1"   >
    <tr>
        <td>Col1</td>
        <td>col2</td>
        <td>col3</td>
        <td>col4/td>
    </tr>
<?php 

$mast = mysql_query("select * from table1 where av_master_id='".$_REQUEST['id']."'");
while($res_mas = mysql_fetch_assoc($mast)) {
    $room = mysql_query("SELECT * FROM `table2` WHERE  av_room_id='".$res_mas['av_room_id']."'");

    $count = 0;
    while($res_room= mysql_fetch_assoc($room)) {
        $count += 1;
?>
    <tr>
        <td><?php echo $count === 1 ? $res_mas['col1'] : "";?></td>
        <td><?php echo $res_room['col2'];?></td>
        <td><?php echo $res_room['col3'];?></td>
        <td><?php echo $res_room['col4'];?></td>
    </tr>
<?php } } ?>
</table>

(未测试的)