I'm trying to figure out how to put two rows from a table beside each other like this: <table><tr><td>row1</td><td>row2</td></tr> <tr><td>row3</td><td>row4</td></tr> etc..</table> Code (markup): Right now I have this: <tr><td><p><? echo "$name"; ?></p></td></tr> Code (markup): and it only shows the name of the rows below each other.. I've been searching everywhere but I can't seem to find a way to get this done :S
You need to use the modulus operator '%' when you are looping. Something like: $i = 0; $rows = mysql_num_rows($query); $columns = 2; //enter number of columns you want while($row = $mysql_fetch_array($query)) { if(($i%$columns==0) || ($i == 0)){ echo "<tr>"; } echo "<td>".$row['stuff']."</td>"; if(($i%$columns==0) || ($i == $rows)){ echo "</tr>"; } $i++; } PHP: If you're using mysqli it would be: $query = $db->query("SELECT STUFF HERE..."); $i = 0; $rows = $query->num_rows; $columns = 2; //enter number of columns you want while($row = $query->fetch_assoc()) { if(($i%$columns==0) || ($i == 0)){ echo "<tr>"; } echo "<td>".$row['stuff']."</td>"; if(($i%$columns==0) || ($i == $rows)){ echo "</tr>"; } $i++; } PHP: