PDA

View Full Version : Five results per line



Mike
02-10-2004, 09:31 AM
Hi all,

I'm trying to work out how to make it so I can have 5 cells per line in a table, with the results slotting in. Then when five are filled up, a new row is started.

Please could anyone help?

Thanks,
Mike

MarkB
02-10-2004, 09:58 AM
You mean, display the results of a query in columns?

So it'd be:

result 1 - result 2 - result 3 - result 4 - result 5

result 6 - result 7

etc?

Mike
02-10-2004, 10:01 AM
Exactly Mark :)

flyingpylon
02-10-2004, 11:53 AM
Put in a counter where each time a cell is added, the counter is incremented by 1. When it gets to 5, start a new row.

Conceptually it would look something like this (adapt this for ASP/PHP/whatever):

i = 0
<tr>
begin loop
if i = 5 then
</tr><tr>
i = 0 (reset counter)
end if
<td>result</td>
i = i + 1
end loop
</tr>

Mike
02-10-2004, 11:57 AM
That'll be it, thanks Paul:)

Chris
02-10-2004, 12:14 PM
I like to do it with division.

If I was making a table and I wanted it to print out 3 cells to a row all you need to do is put the following code at the end of the loop.

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

For 4 cells to a row change the 3 to a 4, etc.

Mike
02-10-2004, 12:39 PM
Seems easier...

So do you put that when the main loop has finished, or actually inside the loop? Also, could you tell me what the % on the first line does please?

Thanks a lot,
Mike

Chris
02-10-2004, 01:01 PM
You put it inside the loop at the end.

while mysqlresultsetblahblah{

echo stuff

if{
(what I posted)
}
}

Chris
02-10-2004, 01:04 PM
The % is the modulus operator.

$i is the counter variable, you still need to increment it ($i++) at the bottom.

The modulus operator returns the remainder from division. So it returns the remainder of $i/3. If there is no remainder, then 3 is a factor of $i, hence its time to start a new row.

Chris
02-10-2004, 01:07 PM
Check out this article for more in php operators.

http://www.developer.com/lang/php/article.php/938511

Oddly enough it was written by the woman who did the design for WebsitePublisher.

Mike
02-10-2004, 01:31 PM
Heh, thanks Chris :)

Mike
02-12-2004, 01:13 PM
I tried it out today, and it kinda worked. Only problem was that it had the first emotion on a seperate line to the rest. To get what I mean, look: http://www.msn-emotions.net/downloads/animal-emotions

My code is as follows:



for ($y=0; $y < mysql_num_rows($result2); $y++) {
$row2 = mysql_fetch_assoc($result2);
echo ("<td width=10%>");
echo ("<img src='");
echo $row2['URL'];
echo ("'></td>");
if(!($y % 5)){
echo "</tr><tr>";
}
}


Please could someone help?

Thanks a lot,
Mike

GCT13
02-12-2004, 01:23 PM
Perhaps start $y at 1, because at zero it's not doing what you want.

for ($y=1; $y <= mysql_num_rows($result2); $y++) {

Chris
02-12-2004, 01:25 PM
I believe the way you have things setup y = 0 for the first row. Since 0/5 has no remainder a new row is created.

Try starting Y off as 1.

Mike
02-12-2004, 07:27 PM
Yep, that's done it. Thanks guys:)

Mike
02-20-2004, 10:38 AM
Just realised that every page misses one emotion out. Like I have 12 animal emotions, but only 11 are showing. Anyone know the possible cause?

Thanks,
Mike

GCT13
02-20-2004, 10:51 AM
Did you do the "less than or equal" in your 'for' statement?

<=

Mike
02-20-2004, 11:00 AM
I didn't :)

Thanks for your help Dan:)

r2d2
02-20-2004, 11:01 AM
Its prolly cos you are starting the loop at 1 rather than 0 where the array index starts. Start your for loop at $y = 0, and change ur row creator to:

if(!(($i + 1) % 5)){
echo "</tr><tr>";
}

Edit: Ah, you've sorted it now :)