Trouble formatting Query results in a table

I am attempting to add a custom page to our Drupal Intranet website here at work.
Apologies in advance, as my php and MySQL knowledge is minimal.

It is using PHP to make a query from a staff contact database and generate a page containing staff photos with contact information below each photo.

The code I put together is successfully pulling data from the MySQL Database and I can output it as a single row or a single column on the page.

As written below, the code produces a single row of output as seen in the image below.
<?php

$dbc = mysqli_connect('localhost', 'username', 'password', 'employees_db') or die("Bad Connect: ".mysqli_connect_error());

$sql = "SELECT * FROM Employees ORDER BY fname";

$result = mysqli_query($dbc, $sql) or die("Bad Query: $sql");

echo "<table width='750' border='1'><tr>";
while($row = mysqli_fetch_assoc($result)) {

echo"<td align=left><img align=left width=150 border=0 src=http://192.168.0.230/intranet/pix/employee/{$row['Image']}.jpg><br><b>{$row['fname']} {$row['lname']}</b><br><b>Dept:</b> {$row['Dept']}<br><b>Ext:</b> {$row['Ext']}<br><b>Direct:</b> {$row['Direct']}<br><b>Cell:</b> {$row['Cell']}<br><b>PC<a href='http://192.168.0.228/node/1574' target=center><img src=http://192.168.0.230/intranet/pix/employee/questionmark.jpg border='0'></a>:</b> {$row['PC']}</td>";
}
echo "</tr></table>";
?>

image

My goal would be to have it look like the picture but output as multiple rows of 4 or 5 employees.
I feel like I am missing something silly. Thanks for the help.

If you retrieve all the data from the query into a php array variable, you will have all the information you need to produce any output you want. See array_chunk() to break the data into rows the size you want.

1 Like

that’s a frontend topic, no need for PHP. Even that you want to let it look like a table, your data is totally serial, so you can just generate an unordered list and apply a grid on it

1 Like

Thanks for the responses!

The CSS Grid worked perfectly!

Much appreciated.

Sponsor our Newsletter | Privacy Policy | Terms of Service