While Loop Problem (Tables)

Associate
Joined
12 Aug 2004
Posts
1,009
Location
Glasgow, Scotland
Hey folks,

I'm having a bit of trouble getting a while loop to work whilst it is producing a table. The problem would be relatively easy to resolve if I was trying to produce lot's of table rows, but instead I'm trying for numerous table columns with only two rows.

The outcome should look like this (when 7 different villa's exist):
screenie(temp).png


but actually look's like this, once the while loop is introduced:
screenie2(temp).png


The actual code for the while loops is:
Code:
	echo "<table border='0' cellpadding='0' cellspacing='4'><tr>";

	while($villa = mysql_fetch_array($villas))
	{
		$villaid = $villa['villaid'];
		$thumbnail = $villa['thumbnail'];
		$categoryid = $villa['categoryid'];
		//$villaid = htmlspecialchars($villa['villaid']);
		$thumbnail = htmlspecialchars($villa['thumbnail']);
		$categoryid = htmlspecialchars($villa['categoryid']);
		
		echo 	"<td class='villas-top'><a href='index.php?page=villapage&villaid=$villaid'><img src='$thumbnail' /></a></td></tr>".
				"<tr><td class='villas-bottom'>$categoryid Bedrooms</td>";
	}
	
	echo "</tr></table>";

I can't figure out how to create the table with it only every using 2 rows, as for what I have now it is producing a new row for every new villa that it finds.

Hopefully someone can figure it out , as I can't!

Thanks for any help given,

Steven.
 
Yeah the problem you have is trying to put down two rows of data and then move across. If you're going to use tables as opposed to divs I'd put another table within the td.

Code:
<table>
<tr>
<?php
while($villa = mysql_fetch_array($villas))
{
$villaid = $villa['villaid'];
$thumbnail = $villa['thumbnail'];
$categoryid = $villa['categoryid'];
//$villaid = htmlspecialchars($villa['villaid']);
$thumbnail = htmlspecialchars($villa['thumbnail']);
$categoryid = htmlspecialchars($villa['categoryid']);
?>
<td>
<table>
<tr>
<td class='villas-top'>
<a href='index.php?page=villapage&villaid=<?php echo $villaid; ?>'>
<img src='<?php echo $thumbnail; ?>' />
</a>
</td>
</tr>
<tr>
<td class='villas-bottom'>
<?php echo $categoryid; ?> Bedrooms
</td>
</tr>
</table>
</td>
<?php
}//end of while
?>
</tr>
</table>

Or you could load the rows into two separate variables and output those.
Code:
echo "<table border='0' cellpadding='0' cellspacing='4'>\r\n";

$top_row = "";
$bottom_row = "";

while($villa = mysql_fetch_array($villas))
{
$villaid = $villa['villaid'];
$thumbnail = $villa['thumbnail'];
$categoryid = $villa['categoryid'];
//$villaid = htmlspecialchars($villa['villaid']);
$thumbnail = htmlspecialchars($villa['thumbnail']);
$categoryid = htmlspecialchars($villa['categoryid']);
		
$top_row .= "<td class='villas-top'>";
$top_row .= "<a href='index.php?page=villapage&villaid=$villaid'>";
$top_row .= "<img src='$thumbnail' /></a></td>\r\n";

$bottom_row .= "<td class='villas-bottom'>$categoryid Bedrooms</td>\r\n";
}

echo "<tr>\r\n$top_row</tr>\r\n";
echo "<tr>\r\n$bottom_row</tr>\r\n";
echo "</table>";
 
Back
Top Bottom