[thelist] php output

David Bindel dbindel at austin.rr.com
Sun Sep 19 11:28:19 CDT 2004

Mark Groen wrote:
> I have a php web page displaying the results of a db table query using
> while($row = mysql_fetch_row($mysql_result)) and want to add in every
> few rows a "to top" link to the list of items returned.
> Not sure how to go about this, tried using while, foreach,
> mysql_numrows but not getting anywhere. Is this better achieved
> somehow through the db query maybe?  Can anyone point me to some
> sample code or a tute for something like this?

All you need to do is add a counter variable that counts the number of 
times the while loop has iterated.

Then, every time that count is divisible by four (i.e., after 4 records 
have been displayed) or some other arbitrary number, you would output 
the link to return to the top of the page.

This is done using the modulo operator, %, which evaluates to the 
remainder (the modulus) of the first operand divided by the second.

Here is how it would be implemented:

// initialize the $i variable to 0:
$i = 0;

while ($row = mysql_fetch_row($mysql_result)) {

     // do all of your output and such here

     // increment the counter:

     // check to see if the counter is divisible by 4:
     if ($i % 4 == 0) {
         // output your "to the top" link here

Hope the helps,
David Bindel

More information about the thelist mailing list