View Full Version : Five results per line
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?
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>
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.
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.
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.
Yep, that's done it. Thanks guys:)
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?
<=
I didn't :)
Thanks for your help Dan:)
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 :)
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.