Hosting Solutions
mobile_logo Table of Contents

Dynamic Grid Output Programming Array Data Tutorial

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 <table> 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>

PHP Videos

Array Programming

Dynamic Grid Output Programming Array Data TutorialReplace Characters with Emoticon Icons Array TutorialRandom Array Items From Flat Text File Database TutorialBad Word Filter Function and Harmful Character String Array ReplaceFind Value in Array Search Needle in Haystack TutorialMultiple Files Simultaneous Upload Array Programming TutorialBrowser and Operating System Detect Array Script