Forum Moderators: coopster

Message Too Old, No Replies

Display 3 items and start new line?

         

danny_s

9:34 am on Jul 12, 2005 (gmt 0)

10+ Year Member



Hi Guys
I'm trying to display 3 items and then start a new line so i dont get a long horizontal line of items, I had some help with this code before here but it doesnt seem to work, I.E it doesnt break the code but it doesnt start a bew line

any ideas and help would be great

regards
Danny

<?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>";
}

// new row after 3

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>";

?>

mogenshoj

10:05 am on Jul 12, 2005 (gmt 0)

10+ Year Member



I have no idea what this is: $count % 3, so i'll just change the code for something else.

I think i would change the last bit from:

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>";
}

to:

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

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

danny_s

11:57 am on Jul 12, 2005 (gmt 0)

10+ Year Member



thanks for the help but it doesnt seem to work

it does start a new line but not after 3 and with no data, I need to push the fourth property to the new row.

I'm lost (as ever)
thanks anyway

Danny

[edited by: jatar_k at 4:49 pm (utc) on July 12, 2005]
[edit reason] no urls thanks [/edit]

mogenshoj

4:13 pm on Jul 12, 2005 (gmt 0)

10+ Year Member



I guess you want the last picture to be put on a new line.

try changing if ($count == 3) to if ($count == 2) like this:

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

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

danny_s

8:15 pm on Jul 12, 2005 (gmt 0)

10+ Year Member



i did try that already and it didnt make any differnce

regards
Danny

jatar_k

6:04 pm on Jul 13, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> I have no idea what this is: $count % 3

it is the modulus
[php.net...]

danny_s, should that "after 3" code be in the while that starts here

while ($a_row =mysql_fetch_array($result)) {

because it isn't, I also don't see the point to this
while($row = mysql_fetch_assoc($result)) {

unless there is another big loop outside of all of the code that you did not post

let me be sure I understand the logic
1. main query grabs featured listings
2. we start the main loop - while ($a_row =mysql_fetch_array($result)) {
3. start td
4. we grab images for the listing and then do some stuff for them
5. close td
6. end main loop

that is how the braces are matched, I am assuming that you should extend the ending brace there to incorporate your "close row and start new row" logic

danny_s

6:38 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



thats the trouble with using other peoples code,
I have no idea..

That is the full code, the bit that was added is after
// new row after 3

I'm just trying to learn this as I go along, i'll keep trying, thanks for the reply anyway.

Regards
Danny

jatar_k

6:44 pm on Jul 13, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



instead of using all the proper output try strirring it down and echo'ing your $count var and a few others to see how the loop is actually behaving, then you have a better understanding of how to fix it.

put things in the code like

echo '<p>#: ',$count;
or
echo '<p>I got to loop 2';

stuff like that so that it outputs data to the screen and you can know where it is dying

maybe iot fgoes through the whole first while loop and all of the featured listings with out even touching your 3rd cell code

that would mean the 3rd cell code is in the wrong place

danny_s

6:50 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



thanks ill try that now

regards
Danny

danny_s

7:49 pm on Jul 13, 2005 (gmt 0)

10+ Year Member



ok, im starting to see the problems..

1. a new row is not started after 3 listings, it just starts a new row regardless of how many listings.

2. it doesnt fill any data (listings) in the new row/colunms

so i guess im starting to understand it a bit better.

lol, im having fun anyway

regards
Danny

jatar_k

7:54 pm on Jul 13, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> lol, im having fun anyway

hehe, I'm glad, you are learning the most important skill in programming

debugging methods

looping is always a little nuts and can easily get out of control, sometimes bad things are happening that you didn't even realize