Forum Moderators: coopster

Message Too Old, No Replies

help with mysql & php

         

danny_s

5:47 pm on Jun 30, 2005 (gmt 0)

10+ Year Member



Hi there
I have a mysql query that displays some featured property listings in a horizontal bar at the bottom of the page constrained by a table set to 800 pixels wide,

if I add more than 3 featured listings its buts the table, so I need to change it so that after 3 listings its starts a new line.

I have no idea so any help would be great.
Thanks in advance.

Here is the code.

<?php
//handles the listing of featured properties

$result = mysql_query("SELECT * FROM homes WHERE featured = 'Y';",$link);
while ($a_row =mysql_fetch_array ($result) )
{
//add commas to price
$a_row[price] = number_format ($a_row[price]);

print "<td align=\"center\">";
print "<a href=\"./propview.php?view=$a_row[id]\"><b><span class=\"welcomtext\">$a_row[title]</span></b></a><BR>";
//select images connected to a given listing
$query = "SELECT * FROM tbl_files WHERE prop_num = $a_row[id] LIMIT 1";
$output = mysql_query("$query",$link);

$count = 0;
while ($image_row =mysql_fetch_array ($output) )
{

print "<a href=\"propview.php?view=$a_row[id]\">";

if ($useGDimg == 1)
{
print "<img src='$homesimgdir"."tn_"."$image_row[prop_num]$image_row[filename]' border=\"1\" width=\"$homeimgw\" alt=\"Photo\" title=\"Click here to discover more about $a_row[title]\" style=\"border: 1px solid #8b0304;\"></a><br>";
}
if ($useGDimg == 0)
{
print "<img src='$homesimgdir$image_row[prop_num]$image_row[filename]' border=\"1\" width=\"$homeimgw\" alt=\"Photo\" title=\"Click here to discover more about $a_row[title]\" style=\"border: 1px solid #8b0304;\"></a><br>";
}
$count++;
}

if ($count == 0)
{
print "<a href=\"./propview.php?view=$a_row[id]\"><img src=\"./images/nophoto.gif\" border=\"0\" width=100 alt=\"View Listing\" style=\"border: 1px solid #8b0304;\"></a><br>";
}

print "$a_row[beds] beds/$a_row[baths] baths<BR>";
print "$currency$a_row[price]";
print "</td>";
}
?>

regards
Danny

ergophobe

9:42 pm on Jun 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



use the modulus (remainder) operator

if (! ($count%3) )
{
echo "</tr><tr>"
}

So whenever count is divisible by 3, you start a new row.

Then remember to fill in empty cells at the end (until $count%3 == 0 once again) and close your last row after you exit the loop.

danny_s

10:01 pm on Jun 30, 2005 (gmt 0)

10+ Year Member



thanks ill give it a go
regards
Danny

danny_s

10:40 pm on Jun 30, 2005 (gmt 0)

10+ Year Member



so would it be somthing like this at the end of the piece of code
(this dont work but am I on the right track?)

if (! ($count%3) )
{
echo "</tr><tr>"
}

else (until $count%3 == 0) )
{
echo "</tr>"
}
?>

Regards
Danny

ergophobe

11:39 pm on Jun 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Not really. It's more like this

echo "<tr>"; // so I start my row before I enter the loop
$count = 0;

while($row = mysql_fetch_assoc($result))
{
$count++;
if ( ($count % 3) == 0)
{
echo "</tr><tr>"; // we close the current row and start another one
}
echo "<td>cell data</td>";
}

// now we need to create the empty cells to fill out the last row
$filled_cells = $count % 3;
for ($i=$filled_cells; $i<3; $i++)
{
echo "<td>&nbsp;</td>";
}

// finally we close our last row
echo "</tr>";

danny_s

8:49 am on Jul 1, 2005 (gmt 0)

10+ Year Member



many thanks, helps me big time
Regards
Danny