整齐地显示与表中的ID相关的记录

时间:2019-03-28 15:31:50

标签: php html sql

代码和查询工作正常,它正确显示所有记录,但是所有内容都显示在一行中,而不是整齐地出现在下面。我试过将它们放在列表中,但这样做却相反。全部显示在没有真实结构的列表中。

<?php       
    if($state == "Grass")
    { 
        $sql="SELECT t_grassland.landid, t_land.landLocation, t_grassland.grasslandAmount, t_livestock.livestockAnimal, t_livestock.livestockBreed 
            FROM(( t_grassland 
            INNER JOIN t_livestock ON t_grassland.livestockid = t_livestock.livestockid) 
            INNER JOIN t_land ON t_grassland.landID = t_land.landID)
            WHERE t_land.landid = '".$id."';";

        $result = mysqli_query($con, $sql) or die(mysqli_error());

        ?>     

        <table width="100%" border="1" style="border-collapse:collapse;">
            <thead>
                <tr>
                    <th><strong>ID</strong></th>
                    <th><strong>Location</strong></th>
                    <th><strong>Amount</strong></th>
                    <th><strong>Animal</strong></th>
                    <th><strong>Breed</strong></th>    
                </tr>
            </thead>          

            <tbody>
                <tr>
                    <?php while($row = mysqli_fetch_assoc($result))
                    { ?>
                        <td><?php echo $row["landid"]; ?> </td>
                        <td><?php echo $row["landLocation"]; ?> </td>
                        <td><?php echo $row["grasslandAmount"]; ?> </td>
                        <td><?php echo $row["livestockAnimal"]; ?> </td>
                        <td><?php echo $row["livestockBreed"]; ?> </td>
                    <?php 
                    } ?>
                </tr>
            </tbody>
        </table>
    <?php       
    } 
        else if ($state == "Growing")
    {
        growingDetails();
    }
    else
    {
        elseDetails();
    } 
?>

enter image description here

1 个答案:

答案 0 :(得分:1)

如@DinoCoderSaurus所指出的那样,将tr标记移至循环内,以便db中的每个记录将生成一个新的表行,因此结果将是垂直的,而不是一行中的全部< / p>

   <?php 
        while($row = mysqli_fetch_assoc($result)) { 
    ?>

    <tr>
        <td><?php echo $row["landid"]; ?> </td>
        <td><?php echo $row["landLocation"]; ?> </td>
        <td><?php echo $row["grasslandAmount"]; ?> </td>
        <td><?php echo $row["livestockAnimal"]; ?> </td>
        <td><?php echo $row["livestockBreed"]; ?> </td>
    </tr>

    <?php 
        } //end loop
    ?>