PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have a database with products in and I want ti to automatically show every product on the page. Now I am able to do this easy enough but what I am struggling to do is that I want the products to be displayed in 3 columns. I can get them to display one under the other easy enough but I want them to be in rows with 3 columns instead.
The code you provided doesn't produce valid HTML, to begin with. It never closes the <td> tags, and there aren't any <tr> tags. I'm guessing that you copied this code from somewhere and are trying to modify it. There's nothing wrong with doing that if you understand what you're doing, but you can't do that sort of thing without knowing at least the basics of PHP and HTML.
That said, the way you achieve 3 columns is by generating 1 row <tr> with 3 cells <td>'s within the while loop, which in turn requires that you fetch 3 rows for each iteration of the loop.
Beyond that, the syntax of your code uses deprecated (outdated) HTML tags.
echo("<table>");
$ct=1; //Initialize counter
while($row = mysql_fetch_array($result)){
if ($bgcolor == "#F7DF42"){
$bgcolor = "#FFFFFF";
}else{
$bgcolor = "#F7DF42";
}
if($ct==1) echo "<tr bgcolor=$bgcolor>";//Start row
echo "\n<td>";
echo '<a href="../products/' . $row["product_code"] . '">' . $row["description1"] . '</a>';
echo '<img src="' . $row["small_image"] . '" alt="' . $row["description1"] . ' ' . $row["description2"] . '"></a>';
echo "</td>\n";
if($ct==3){ echo "</tr>"; $ct=0; }//End row 3rd time through and reset counter to 0 so that $ct++ will set it to 1 for the new row. For more columns change $ct==3 to $ct==x. x being the number of columns you want.
$ct++;
}
if($ct!==1) echo "</tr>";//If the loop ends on 3 then the $ct=0; and $ct++; will still fire making it a one. Therefore we test for $ct not equal to 1 in order to terminate the row properly.
echo("</table>");