表3列循环

时间:2017-02-24 02:27:14

标签: php css

我已阅读有关为PHP编写三列的所有帖子。但是,我似乎无法找到任何可以让我创建三列循环。我所做的一切都给了我一个错误或一个空白的白页。我还需要在列循环之间留一点空间。这是我到目前为止,你能告诉我我错过了什么吗?

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();

1 个答案:

答案 0 :(得分:0)

$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();

解释: - 在while循环中,我们将检查变量$x,我们在循环外将它设置为0,并在每次迭代时递增它, 当该变量等于3时,我们将打印行</tr>

的结尾