# PHP - Dynamically display mysql results in a table

**URL:** <https://forum.kirupa.com/t/php-dynamically-display-mysql-results-in-a-table/309403>\
**Category:** programming\
**Created:** [May 14, 2010, 4:04am UTC](https://forum.kirupa.com/t/php-dynamically-display-mysql-results-in-a-table/309403 "2010-05-14T04:04:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Flash\_Man1](https://avatars.discourse-cdn.com/v4/letter/f/8797f3/32.png) [@Flash\_Man1](https://forum.kirupa.com/u/Flash_Man1)\
**Post date:** [May 14, 2010, 4:04am UTC](https://forum.kirupa.com/t/php-dynamically-display-mysql-results-in-a-table/309403/1 "2010-05-14T04:04:50Z")

</div>

Hi All,

I have a mysql database setup and working my only problem is in my php code I don’t know how to run a loop to create a table which is 4 columns across with a 3 columns spacing one between each column by howevery many rows/records there are so for example I would want it to look like this:

product 1 image product 2 image product 3 image product 4 image  
product 1 product 2 product 3 product 4  
price price price price  
spacer row spaning 7 columns

Here is the exact code for the table and how I want each row to be generated.

```auto

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="products">
                  <tr>
                    <td width="142">Product Image</td>
                    <td>&nbsp;</td>
                    <td width="142">Product Image</td>
                    <td>&nbsp;</td>
                    <td width="142">Product Image</td>
                    <td>&nbsp;</td>
                    <td width="142">Product Image</td>
                  </tr>
                  <tr>
                    <td width="142" align="center">Product Name</td>
                    <td>&nbsp;</td>
                    <td width="142" align="center">Product Name</td>
                    <td>&nbsp;</td>
                    <td width="142" align="center">Product Name</td>
                    <td>&nbsp;</td>
                    <td width="142" align="center">Product Name</td>
                  </tr>
                  <tr>
                    <td width="142" align="center">$10.00</td>
                    <td>&nbsp;</td>
                    <td width="142" align="center">$10.00</td>
                    <td>&nbsp;</td>
                    <td width="142" align="center">$10.00</td>
                    <td>&nbsp;</td>
                    <td width="142" align="center">$10.00</td>
                  </tr>
                  <tr>
                    <td width="142">&nbsp;</td>
                    <td>&nbsp;</td>
                    <td width="142">&nbsp;</td>
                    <td>&nbsp;</td>
                    <td width="142">&nbsp;</td>
                    <td>&nbsp;</td>
                    <td width="142">&nbsp;</td>
                  </tr>
                  <tr>
                    <td colspan="7">&nbsp;</td>
                  </tr>
                  <tr>
                    <td width="142">Product Image</td>
                    <td>&nbsp;</td>
                    <td width="142">Product Image</td>
                    <td>&nbsp;</td>
                    <td width="142">Product Image</td>
                    <td>&nbsp;</td>
                    <td width="142">Product Image</td>
                  </tr>
                  <tr>
                    <td width="142" align="center">Product Name</td>
                    <td>&nbsp;</td>
                    <td width="142" align="center">Product Name</td>
                    <td>&nbsp;</td>
                    <td width="142" align="center">Product Name</td>
                    <td>&nbsp;</td>
                    <td width="142" align="center">Product Name</td>
                  </tr>
                  <tr>
                    <td width="142" align="center">$10.00</td>
                    <td>&nbsp;</td>
                    <td width="142" align="center">$10.00</td>
                    <td>&nbsp;</td>
                    <td width="142" align="center">$10.00</td>
                    <td>&nbsp;</td>
                    <td width="142" align="center">$10.00</td>
                  </tr>
                  <tr>
                    <td width="142">&nbsp;</td>
                    <td>&nbsp;</td>
                    <td width="142">&nbsp;</td>
                    <td>&nbsp;</td>
                    <td width="142">&nbsp;</td>
                    <td>&nbsp;</td>
                    <td width="142">&nbsp;</td>
                  </tr>
                  <tr>
                    <td colspan="7">&nbsp;</td>
                  </tr>
                  <tr>
                    <td width="142">Product Image</td>
                    <td>&nbsp;</td>
                    <td width="142">Product Image</td>
                    <td>&nbsp;</td>
                    <td width="142">Product Image</td>
                    <td>&nbsp;</td>
                    <td width="142">Product Image</td>
                  </tr>
                  <tr>
                    <td width="142" align="center">Product Name</td>
                    <td>&nbsp;</td>
                    <td width="142" align="center">Product Name</td>
                    <td>&nbsp;</td>
                    <td width="142" align="center">Product Name</td>
                    <td>&nbsp;</td>
                    <td width="142" align="center">Product Name</td>
                  </tr>
                  <tr>
                    <td width="142" align="center">$10.00</td>
                    <td>&nbsp;</td>
                    <td width="142" align="center">$10.00</td>
                    <td>&nbsp;</td>
                    <td width="142" align="center">$10.00</td>
                    <td>&nbsp;</td>
                    <td width="142" align="center">$10.00</td>
                  </tr>
                  <tr>
                    <td width="142">&nbsp;</td>
                    <td>&nbsp;</td>
                    <td width="142">&nbsp;</td>
                    <td>&nbsp;</td>
                    <td width="142">&nbsp;</td>
                    <td>&nbsp;</td>
                    <td width="142">&nbsp;</td>
                  </tr>
                  <tr>
                    <td colspan="7">&nbsp;</td>
                  </tr>
                </table>

```

Here is my php code which I am using as well and this is what I have so far:

```php

$connect = mysql_connect($dbhost, $dbuser, $dbpwd);
	if (!$connect) { die("Could not connect: " . mysql_error()); }
	mysql_select_db($database, $connect);

	$result = mysql_query("SELECT * FROM products WHERE cat_id = 1");
	$num_rows = mysql_num_rows($result);
	
	if ($num_rows > 0) {
		$frow = floor($num_rows / 3) + ($num_rows % 3 >= 1 ? 1 : 0);
		$srow = $frow + floor($num_rows / 3) + ($num_rows % 3 == 2 ? 1 : 0);
		$x=1;
		
		echo '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
		echo "<tr><td>";
		
		while($row = mysql_fetch_array($result)) {
			echo $row['product_name'] . ' ' . $row["price"] . ' <img src="' . $img . $row["image"] . '" /><br />';
			if ($x == $frow || $x == $srow) {
				echo "</td>";
				echo "<td>";
			}
			$x++;
		}
	
		echo "</td></tr>"; // close last column, close row
		echo "</table>";
	}

	mysql_close($connect);

```

Any help would be much appreciated!

Thanks in advance,  
G
