代码之家  ›  专栏  ›  技术社区  ›  cloudseeker

PHP中的组和循环问题

  •  0
  • cloudseeker  · 技术社区  · 5 年前

    SELECT * 其结果如下:

     -----------------------------------------------------------------------
     |                      Room_Location - Room_Name                       |
     -----------------------------------------------------------------------
     | Device                         | Technical        | Ergonomic        |
     -----------------------------------------------------------------------
     | Device_Model - Device_Type     | ☐ Pass  ☐ Fail  | ☐ Pass  ☐ Fail  |
     | IP_Address                     |                  |                  |
     -----------------------------------------------------------------------
     | Device_Model - Device_Type     | ☐ Pass  ☐ Fail  | ☐ Pass  ☐ Fail  |
     | IP_Address                     |                  |                  |
     -----------------------------------------------------------------------
    
     then a new table:
    
     -----------------------------------------------------------------------
     |                      Room_Location - Room_Name                       |
     -----------------------------------------------------------------------
     | Device                         | Technical        | Ergonomic        |
     -----------------------------------------------------------------------
     | Device_Model - Device_Type     | ☐ Pass  ☐ Fail  | ☐ Pass  ☐ Fail  |
     | IP_Address                     |                  |                  |
     -----------------------------------------------------------------------
     | Device_Model - Device_Type     | ☐ Pass  ☐ Fail  | ☐ Pass  ☐ Fail  |
     | IP_Address                     |                  |                  |
     -----------------------------------------------------------------------
    

    等等。。。

    我知道有些人反对将代码与HTML交织在一起,但撇开这一点不谈,我所经历的是: 第一条记录的文件室代码和文件室名称不会显示在第一条记录的上方,而是显示为第二条记录的标题,并向下延伸到最后一条记录。

    最后一条记录实际上是在其标题中显示上面房间记录的房间代码。

    我的连接代码:

     $result = mysqli_query($con,"SELECT * FROM RoomList ORDER BY Room_Code ASC");
    

    余数:

      <?php 
        
        /*// Start record fail error checking
        
        if (!$result) {
        printf("Error: %s\n", mysqli_error($con));
        exit();
    }
        // Start record fail error checking */
    
        if (mysqli_num_rows($result) === 0) {?>
    <div id="resultserror">
    No records could found for the selected view.</div>
              <?php } else { ?>
    <?php ;
    $data = array();
    while ($row = mysqli_fetch_assoc($result)) {
      $itemName = $row["Room_Code"];
      if (!array_key_exists($itemName, $data)) {
        $data[$itemName] = array();
      }
      $data[$itemName][] = $row;
    }                       
                            
    foreach ($data as $itemName => $rows) {
     
        ?>
              <table id="results" width="100%" cellpadding="3" cellspacing="0">
            <tr><td colspan="4"><?php echo($row['Room_Code']);?> - <?php echo($row['Room_Name']);?></td></tr>
            <tr>
    <th width="" align="center">Device</th>
    <th width="100" align="center">Technical</th>
    <th width="100" align="center">Ergonomic</th>
    </tr>
    
    <?php
        foreach ($rows as $row) {
            ?>
        <tr>
    
    <td align='left'>
    <strong>Device:</strong><?php echo($row['Device_Model']);?><br>
    <strong>Type:</strong> <?php echo($row['Device_Type']);?><br>
    <strong>IP Address:</strong> <?php echo($row['IP_Address']);?></td>
    <td align='center'>☐ Pass<br>
      ☐ Fail</td>
    <td align='center'>☐ Pass<br>
      ☐ Fail</td>
    </tr>
    <?php
    }
    ?>
    <tr>
      <td colspan="7" align="center">- end of room report -</td></tr>
    </table>
    <?php
    }
    mysqli_close($con);
                         }?>
    <div id="reportgen"><?php echo("Report Generated: " . date("Y-m-d H:i:s")); ?></div>
    <div id="reportcount"><?php echo("Record Count: " . mysqli_num_rows($result) . "");?></div>
    
    1 回复  |  直到 5 年前
        1
  •  1
  •   Nick SamSmith1986    5 年前

    你的问题在于:

    <tr><td colspan="4"><?php echo($row['Room_Code']);?> - <?php echo($row['Room_Name']);?></td></tr>
    

    你在呼应 $row

    <tr><td colspan="4"><?php echo($rows[0]['Room_Code']);?> - <?php echo($rows[0]['Room_Name']);?></td></tr>