I am working on a very simple webshop, it should echo different products in a table of 2 <td>
by 4 <tr>
but now it only displays the different products downwards. Hopefully someone here can help me.
$result=mysql_query("select * from products");
while($row=mysql_fetch_array($result)) {
$artikel = '<div style="background-color:#E3E3E3;width:200px;height:200px;"><a href=""><img style="padding-top:10px;padding-left:25px;width:150px;height:150px;" src="'.htmlspecialchars($row['picture']).'"></a><br><div align="center"><b>'.htmlspecialchars($row['name']).'</b><br><h6>€'.htmlspecialchars($row['price']).'</h6></div></div>';
echo '<table><tr><td>'.$artikel.'</td>';
echo '</table>';
}
Why would it display a table of 2 by 4?
You are putting every product in its own table, so the first thing to do, is move the table
tags out of the loop and then you need to add logic to add a new row after every x
products.
Although you don't seem to need a table as your div
has fixed dimensions, so you can just get rid of the table and use css to get the grid you want.
If this is your entire code then you're missing a tr tag. Could be a possible cause for your lay-out shifting.
Instead of using breaks, I'd recommend using multiple divs (or table lay-out if you're more comfortable with that) as breaks aren't always handled the same in all browsers.
Instead of creating new table for every records, you can use one row per record.
$result=mysql_query("select * from products");
if(mysql_num_rows($result) > 0){
echo '<table>';
while($row=mysql_fetch_array($result)) {
$artikel = '<div style="background-color:#E3E3E3;width:200px;height:200px;"><a href=""><img style="padding-top:10px;padding-left:25px;width:150px;height:150px;" src="'.htmlspecialchars($row['picture']).'"></a><br><div align="center"><b>'.htmlspecialchars($row['name']).'</b><br><h6>€'.htmlspecialchars($row['price']).'</h6></div></div>';
echo '<tr><td>'.$artikel.'</td></tr>';
}
echo '</table>';
}