dongyun6229 2017-02-24 02:27
浏览 22
已采纳

表3列循环

I have read all the post about coding three columns for PHP. However, I can't seem to find any that will allow me to create three column loop. Everything I do either gives me an error or a blank white page. I also need to have a little space between the column loops. Here is what I have so far, can you tell me what I am missing?

table, td, th {    
    border: 1px solid #000;
    text-align: left;
}

table {
    border-collapse: initial;
    width: 100%;

}

td {
    padding: 10px;
    width: 5%;
    line-height: 2;
  
}

th  {
    background-color: grey;
       color: white;
       padding: 15px;
       width: auto;
    
    
}
$sql = "SELECT name, email, dropdown, description FROM basic";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<table>";
    $columns=3;
    // output data of each row
    while($row = $result->fetch_assoc()) {
        if ($row > 0 && ($columns) == 3) {
            echo "<th>". $row["name"]. "</th><tr> <td>email: ". $row["email"]. "</td><tr> <td>category: " . $row["dropdown"] . "</td><tr><td>Announcement: " . $row["description"] . "</td></tr>";
        }
    }
    echo "</table>";
} else {
    echo "0 results";
}
$conn->close();

</div>
  • 写回答

1条回答 默认 最新

  • drodsh7940 2017-02-24 13:30
    关注
    $sql = "SELECT name, email, dropdown, description FROM basic";
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
        echo "<table>";
        $columns=3;
        $x = 0;
        // output data of each row
        echo "<tr>";
        while($row = $result->fetch_assoc()) {
            if ($row > 0 && ($columns) == 3) {
                echo "<th>". $row["name"]. "</th><tr> <td>email: ". $row["email"]. "</td><tr> <td>category: " . $row["dropdown"] . "</td><tr><td>Announcement: " . $row["description"] . "</td></tr>";
    
                if ($x == 3) {
                    echo "</tr>";
                    $x = 0;
                }
    
                $x++;
            }
        }
        if ($x < 3) {
            echo "</tr>";
        }
        echo "</table>";
    } else {
        echo "0 results";
    }
    $conn->close();
    

    Explaination :- within the while loop , we will check the variable $x which we set it to 0 outside the loop and increment it for every iteration , when that variable be equal to 3 we will print the end of row </tr>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建