Hi,
I've got the attached code selecting all records from a particular table and
then outputting max. 4 results per page using a while loop to output them:
<?php
$dbcnx = mysql_connect("localhost", "xxxx", "xxxx");
mysql_select_db("bir");
$totalsql = "select * from listings order by address";
$limit = 4; // rows to return
$numresults = mysql_query($totalsql, $dbcnx);
$totalnum = mysql_num_rows($numresults);
// next determine if page has been passed to script, if not use 1
if (empty($page)) {
$page = 1;
}
$offset = $limit * ($page - 1);
// Select rows
$sql = "SELECT ID, price,
address,description,pic1_desc,pic2_desc,pic3_desc,pic4_desc FROM listings
order by address limit $offset,$limit";
$result = @mysql_query($sql,$dbcnx);
$num = @mysql_num_rows($result);
$filelist = @mysql_query($sql)
or die("Database error: " . mysql_error());
?>
<html>
<HEAD>
<TITLE>Loop Test</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</HEAD>
<body LEFTMARGIN="0" TOPMARGIN="0" MARGINHEIGHT="0" MARGINWIDTH="0"
BGCOLOR="#F7F6EE">
<TABLE CELLPADDING="0" BORDER="0" CELLSPACING="5" WIDTH=518>
<TR>
<TD COLSPAN="3"><IMG SRC="images/hori_break.jpg" WIDTH=518 HEIGHT=2
BORDER=0 ALT=""></TD>
</TR>
<TR ID="sectionheader">
<TD COLSPAN="3" HEIGHT="10">Loop Test</TD>
</TR>
<TR>
<TD COLSPAN="3"><IMG SRC="images/hori_break.jpg" WIDTH=518 HEIGHT=2
BORDER=0 ALT=""></TD>
</TR>
<?php
$i = 1;
if (mysql_num_rows($filelist) > 0) {
while ($f = mysql_fetch_array($filelist)) {
if($i%2 != 0){
?>
<TR>
<TD>
<TABLE border=0>
<TR>
<TD WIDTH=258 HEIGHT="20" ALIGN="left"
ID="propertyheader"><?=$f["address"]?></TD>
</TR>
<TR>
<TD WIDTH=258 HEIGHT=158 ALIGN="center" VALIGN="middle"><img
src="images.php?action=view&field=main_pic&id=<?=($f["ID"])?>"
BORDER=0></TD>
</TR>
<TR>
<TD WIDTH=258 HEIGHT="20" ALIGN="center"
ID="propertyheader">$<?=$f["price"]?></TD>
</TR>
</TABLE>
</TD>
<TD WIDTH=2 HEIGHT=158 ALIGN="center" VALIGN="middle"><IMG
SRC="images/veri_break.jpg" WIDTH=2 HEIGHT=158 BORDER=0 ALT=""></TD>
<?php
}
if ($i%2 == 0){
?>
<TD>
<TABLE border=0>
<TR>
<TD WIDTH=258 HEIGHT="20" ALIGN="left"
ID="propertyheader"><?=$f["address"]?></TD>
</TR>
<TR>
<TD WIDTH=258 HEIGHT=158 ALIGN="center" VALIGN="middle"><img
src="images.php?action=view&field=main_pic&id=<?=($f["ID"])?>"
BORDER=0></TD>
</TR>
<TR>
<TD WIDTH=258 HEIGHT="20" ALIGN="center"
ID="propertyheader">$<?=$f["price"]?></TD>
</TR>
</TABLE>
</TD>
</TR>
<TR>
<TD COLSPAN="3"><IMG SRC="images/hori_break.jpg" WIDTH=518 HEIGHT=2
BORDER=0 ALT=""></TD>
</TR>
<?php
}
$i += 1;
?>
<?php
}
}
?>
</TABLE>
</body>
</html>
My problem:
If there's only one record in the recordset the formatting looks like this:
http://64.251.84.3/test/loop.php
2 records:
http://64.251.84.3/test/loop2.php
3 records:
http://64.251.84.3/test/loop3.php
and here's how it's supposed to look:
http://64.251.84.3/test/loop4.php
I'd like to have the while loop fill in the "empty" table cells with
non-breaking spaces so that even if there are less than 4 records the table
always looks the same (ie. has two table rows with 2 cells per row even if
some are empty).
My environment:
Win2K Server
PHP4
MySql
Note: For example's sake I have four different scripts writing/pulling from
4 separate tables to show how the output is affected by the various
recordsets.
Any help would be greatly appreciated.
Thanks,
Ian