...

View Full Version : Resolved Blank Cell in table with MySQL DB table



Puppet Master
04-24-2011, 02:21 AM
I have this code that gets all the products off the DB and shows them in a color organized table.

It will load all the products, but if the user entered a product to be displayed in row 1 col 1 and another in row 1 col 3, the cell 1-3 will just move over to 1-2.

How can I change it so it leaves 1-2 empty rather than moving it mover?



echo "<table>";
for ($i = 1; $i <= 9; $i++)
{
$sql = "SELECT products.pname, product_categories.color, products.pid, products.clmn
FROM products, product_categories
WHERE products.cat=product_categories.pname
AND products.row={$i}
ORDER BY products.clmn;";
$query = mysql_query($sql);
echo "<tr>";
while ( $show = mysql_fetch_array($query) )
{
echo "<td bgcolor='#{$show['color']}' width='80' height='70'><a href='http://{$config['domain']}/transaction.php?pid={$show['pid']}&action={$action}&quant={$quant}'><b><div class=\"charge\">{$show['pname']}</div></b></a></td>";
}
echo "</tr>";
}

echo "</table>";

Fumigator
04-24-2011, 04:50 AM
Instead of a while ( $show = mysql_fetch_array($query) ) loop, use another for() loop, and with each iteration, compare the value of your column "clmn" to the iterator of the for() loop. If they match, great, echo the query results. If the iterator is less than "clmn", you know you need to echo an empty <td> and go through the loop again.

Your loop ends not when the query results end, but when the last column is compared (whatever number that is).

Puppet Master
04-24-2011, 08:19 AM
[QUOTE=Fumigator;1082250]Instead of a while ( $show = mysql_fetch_array($query) ) loop, use another for() loop, and with each iteration, compare the value of your column "clmn" to the iterator of the for() loop. If they match, great, echo the query results. If the iterator is less than "clmn", you know you need to echo an empty <td> and go through the loop again.

Okay, I wrote it the way you told me. It works, but is only showing me the first column of the table.

Is there a part I did wrong?



echo "<table>";
/***********get the rows**************************/
$getlimit = mysql_query('SELECT * FROM homescreen_layout');
$limit = mysql_fetch_array($getlimit);
$limit_num = $limit['max_rows'];
/**********show the rows********************************/
for ($i = 1; $i <= $limit_num; $i++)
{
$sql = "SELECT products.pname, product_categories.color, products.pid, products.clmn
FROM products, product_categories
WHERE products.cat=product_categories.pname
AND products.row={$i}
ORDER BY products.clmn;";
$query = mysql_query($sql);
$show = mysql_fetch_array($query);
/*******get the cols*************************************/
$getcols = mysql_query('SELECT * FROM homescreen_layout');
$cols_array = mysql_fetch_array($getcols);
$cols = $cols_array['max_cols'];
/*******************show the cols************************/
echo "<tr>";
for ($c=1; $c <= $cols; $c++)
{
//while ($show = mysql_fetch_array($query))
//{
if($c == $show['clmn'])
{

echo "<td bgcolor='#{$show['color']}' width='80' height='70'><a href='http://{$config['domain']}/transaction.php?pid={$show['pid']}&action={$action}&quant={$quant}'><b><div class=\"charge\">{$show['pname']}</div></b></a></td>";
}
else
{
echo "<td bgcolor='#FFFFFF' width='80' height='70'>&nbsp;</td>";
}
//}
}
echo "</tr>";
}

echo "</table>";

Fumigator
04-24-2011, 09:37 AM
As part of your for() loop you need to fetch from the products query.

Puppet Master
04-25-2011, 02:12 AM
It worked perfectly, Thanks for your help Fumigator :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum