Dakota Wagner Dakota Wagner - 5 months ago 11
SQL Question

How to make a PHP page have two "column" regions?

Basically I'm doing digital signage and I'm trying to get names to be pulled from a MySQL database to a PHP page. Right now its all centered in one column, but I want the results to be in two columns side by side. How can I do this?

$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 {

}


Basically I want this data to be spread across two columns instead of 1 single page.

Answer

You can use tables, and count the rows to determine if you need to start a new table row.

$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>";