âš ī¸ Warning âš ī¸ Deprecated Code! This video tutorial contains outdated code.
💡 If you wish to update it, any AI assistant will update the code for you in seconds.

Dynamic Grid Output Programming Array Data Tutorial

Published : November 22, 2014   •   Last Edited : November 24, 2025   •   Author : Adam Khoury
There may come a time when you would like to render a dynamic grid layout from array data you have access to in your scripts. Most times an HTML is used to make a grid of table rows and columns from a dynamic array of information. But you are not limited to tables, use DIVs with "display:inline" applied in their CSS, or any other container tag that can hold text and stuff on a web page. In the script below we are querying our MySQL database using PHP, then simply adding a few lines within our normal while loop to create grid layout logic. The logic in this lesson could be applied to any programming language.
<?php
// Include database connection
include_once 'connect_to_mysql.php';
// SQL query to interact with info from our database
$sql = mysql_query("SELECT id, member_name FROM member_table ORDER BY id DESC LIMIT 15"); 
$i = 0;
// Establish the output variable
$dyn_table = '<table border="1" cellpadding="10">';
while($row = mysql_fetch_array($sql)){ 
    
    $id = $row["id"];
    $member_name = $row["member_name"];
    
    if ($i % 3 == 0) { // if $i is divisible by our target number (in this case "3")
        $dyn_table .= '<tr><td>' . $member_name . '</td>';
    } else {
        $dyn_table .= '<td>' . $member_name . '</td>';
    }
    $i++;
}
$dyn_table .= '</tr></table>';
?>
<html>
<body>
<h3>Dynamic PHP Grid Layout From a MySQL Result Set</h3>
<?php echo $dyn_table; ?>
</body>
</html>