Forum Moderators: coopster
Ideally I would like the output to run across the page as 4 rows with 5 items in each.
Can I build something into the print script such that it will start a new row after items 5 10 and 15.
Or should I be thinking of running the script 4 times and printing items 1-5, 6-10 etc.
Or is there an altogether more elegant way of doing this?
I would appreciate any guidance you can give.
Thanks you
Mark Barrett
$x = 0;
echo '<table><tr>';
while($row = mysql_fetch_object($result)){
echo "<td>$row->somevar</td>";
if($x%5 == 0){
echo '</tr><tr>';
}
$x++;
}
echo '</tr></table>';
This is untested, looking at it right now .. it might give you an extra <tr></tr> at the end, but it could be some place to start.
-sned
Fantastic! Just one small change I had to make - i.e. $x=1 - otherwise it put the new row command in at the end of lines 1, 6, 11 etc.
Having said that I worked that out by trial and error. Can you spare just another moment to explain what is happening with "if($x%5==0)" and "$x++". I guess it is mainly the % that I am struggling with. I can't understand from my PHP manual what that is doing.
I really do appreciate your help.
Mark
Sorry our posts crossed there.
Thanks for you reply but Sned has got me sorted. The reason for this was so I could print 20 .jpg's neatly in 4 rows to save having to scroll the page.
It will be the icing on the cake if Sned can explain how that bit of code is making it happen.
Best wishes
Mark
Then the $x++ adds one to $x. It's from C++.
basicly it can be re-writen;
$x=$x+1;
basicly he is using $x as an increment. and when ever $x is divisable by 5 it terminatets the table row and starts a new one.
I took a diffent aproch to this when I did it last and just did it when ever $x==5 then I reset $x.
if($x == 5){
echo '</tr><tr>';
$x=0;
}else{
$x++;
}
I'm thinking his way might actualy be better though.
Yeees, so straightforward now you explain it. Now I can see how both Sned's and yours work - just 2 ways of getting to the same place.
The real key I was missing was that $x is the "reference number" of the "output" (sorry for the rubbish terminology)
Is $x like a reserved variable name for that purpose? - otherwise I can't see from your scripts how it is interpreted correctly!
As ever thanks for solving my problem and improving my PHP knowledge.
Mark
and of course the all powerful string "Hello World"
Which btw I think if that is not in the first few lines of code you ever write you are uncerimoniously taken out by the code police and shot.
back to x,y,& z.
these probably origiante from our wonderful highschool algerbra classes. And probably have some relation to the x,y,z planes in Geometry... which came first though your guess is as good as mine.
this and the fact the we programers are natoriusly lazy and why type a 2 letter verriable when a 1 leter will surfice(of course back in the programing stone age 1 char took up a lot of memory and if you inflated your code with needless char by 10 bytes other programers would at the least give you the evil eye.)
Sarah
I have a query to get 100 results from my album database. I currently have this to show the results.
while ($result = mysql_fetch_row($sqlquery))
{
echo "$result[1]<br>";
}
How would I use the above code to get this list to stop at 50 then start a new column.
if not it certainly is a useful function to know
Surely you can do this with the same logic that sned gave me.
You would put your output into a table with 1 row and 2 cells.
The first 50 items would just run down the first cell and then at the end of item 50 you would need a </td><td> to start again in the next cell.
It's the same logic as sned gave me to start a new row.
This is only in my head so I may have missed something - but if you want to give it a try I will give you the script I think you need.
There's nothing to lose but a bit of time.
Let me know.
Mark
<?php
$sqlquery = mysql_query("SELECT * FROM yourtable LIMIT 0, 5");
while ($result = mysql_fetch_row($sqlquery))
{
echo ($result[1] . "   ");
}
?><p>
<?php
$sqlquery = mysql_query("SELECT * FROM yourtable LIMIT 5, 5");
while ($result = mysql_fetch_row($sqlquery))
{
echo ($result[1] . "   ");
}
?><p>
<?php
$sqlquery = mysql_query("SELECT * FROM yourtable LIMIT 10, 5");
while ($result = mysql_fetch_row($sqlquery))
{
echo ($result[1] . "   ");
}
?><p>
...and so on. It's not as efficient as the other examples, but it's very simple and easy to understand in my opinion.