基本上,我正在做数字标牌,并且试图将名称从MySQL数据库拉到PHP页面。现在,其所有内容都集中在一列中,但是我希望结果并排在两列中。我怎样才能做到这一点?

$sql = "SELECT * FROM donor WHERE DonationAmount = 5000 AND Category = '1' or DonationAmount = 5000 AND Category IS NULL ORDER BY LastName ASC";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {

            // test if the DisplayName field is empty or not
            if(empty($row['DisplayName']))
            {
                // it's empty!
                    if(empty($row['FirstName'])){
                        echo $row['LastName']. "<br>";
                    }

                    else{
                        echo $row["LastName"]. ", " . $row["FirstName"]. "<br>";
                    }

            }else{
                // Do stuff with the field
                    echo $row["DisplayName"]. "<br>";
            }

    }
} else {

}


基本上,我希望此数据分布在两列中,而不是一页。

最佳答案

您可以使用表,并对行进行计数以确定是否需要开始新的表行。

$i = 0;
$total_rows = $result->num_rows;
echo "<table><tr>";
while($row = mysqli_fetch_assoc($result)) {

    // test if the DisplayName field is empty or not
    echo "<td>";
    if(empty($row['DisplayName']))
    {
        // it's empty!
        if(empty($row['FirstName'])){
            echo $row['LastName'];
        }

        else{
            echo $row["LastName"]. ", " . $row["FirstName"];
        }

    }else{
        // Do stuff with the field
        echo $row["DisplayName"]. "";
    }
    echo "</td>";
    $i++;
    if($i % 2 == 0 && $i != $total_rows) {
        echo "</tr><tr>";
    }

}
echo "</tr></table>";

07-24 16:05