Forum Moderators: coopster

Message Too Old, No Replies

How to print 20 items in 4 rows of 5

         

Mark Barrett

9:48 pm on Jun 9, 2005 (gmt 0)

10+ Year Member



I have a very simple script that selects the 20 most recent items added to a database and prints each one in an separate table cell down the page.

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

sned

9:54 pm on Jun 9, 2005 (gmt 0)

10+ Year Member



You could do something like:

$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

Mark Barrett

10:36 pm on Jun 9, 2005 (gmt 0)

10+ Year Member



Sned

Many thanks for the prompt reply

I will see if I can make this work.

My knowledge of PHP is rudimentary but this will be a great exercise for me to work through.

Cheers

Mark

Sarah Atkinson

2:52 pm on Jun 10, 2005 (gmt 0)

10+ Year Member



what are you trying to use this for.
I just did a script that prints a calander mon-friday and I did it all in one quary. Unfortunaly without going over my code I can't tell you how I did it cause I'm running on 3 hours sleep and just back from a 2 week vaction. although I had a lot of earlier posts aboult it.

Mark Barrett

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

10+ Year Member



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

Mark Barrett

3:08 pm on Jun 10, 2005 (gmt 0)

10+ Year Member




Sarah

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

Sarah Atkinson

3:19 pm on Jun 10, 2005 (gmt 0)

10+ Year Member



The % is a modulus operator. It's kind of like division only it returns the remainder. Like where 15 goes into 32 2 times with a remainder of 2. with the % will return that 2.

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.

Mark Barrett

4:20 pm on Jun 10, 2005 (gmt 0)

10+ Year Member



Sarah

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

sned

4:24 pm on Jun 10, 2005 (gmt 0)

10+ Year Member



Glad to see you got it figured out Mark.

Thanks for explaning the code Sarah.

-sned

Sarah Atkinson

4:59 pm on Jun 10, 2005 (gmt 0)

10+ Year Member



No $x is not reserved however it and it's siblings $y & $z is kind of a tradition. similar to foobar and snufu
(though I did know one person who gave all his veriables like this proper names usualy with similar associations like one group might have benen 17th centery french authors or something---drove programers around him batty)

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

hwange

5:27 pm on Jun 10, 2005 (gmt 0)

10+ Year Member



I am trying to achieve something similar to the topic of this didcussion.

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.

jatar_k

5:37 pm on Jun 10, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could limit your select to 50 or use a counter in the loop to stop at 50 or use a for loop to only loop 50 times

ah, so many ways ;)

Sarah Atkinson

5:54 pm on Jun 10, 2005 (gmt 0)

10+ Year Member



this function my also help you
mysql_data_seek() [us2.php.net]

if not it certainly is a useful function to know

Mark Barrett

10:45 am on Jun 11, 2005 (gmt 0)

10+ Year Member



Hwange

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

daneosporin

8:12 pm on Jun 11, 2005 (gmt 0)

10+ Year Member



hwange:
you could also try changing what $sqlquery is. Something like this...

<?php
$sqlquery = mysql_query("SELECT * FROM yourtable LIMIT 0, 5");
while ($result = mysql_fetch_row($sqlquery))
{
echo ($result[1] . " &nbsp ");
}
?>

<p>

<?php
$sqlquery = mysql_query("SELECT * FROM yourtable LIMIT 5, 5");
while ($result = mysql_fetch_row($sqlquery))
{
echo ($result[1] . " &nbsp ");
}
?>

<p>

<?php
$sqlquery = mysql_query("SELECT * FROM yourtable LIMIT 10, 5");
while ($result = mysql_fetch_row($sqlquery))
{
echo ($result[1] . " &nbsp ");
}
?>

<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.

hwange

11:39 am on Jun 12, 2005 (gmt 0)

10+ Year Member



Thanks all I will have a look through all your suggestions.

I'm still quite new to this all so I'm sure I'll work it out soon using all your input.