#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Bartlett, TN, USA
    Posts
    3
    Rep Power
    0
    I'm referring to the article published on www.phpbuilder.com titled "Building Next/Prev Buttons for Query Results". I'm trying to fit the article's suggestions to my own application. Asking only for my own clarification and understanding, are we querying the db/table twice for the same information? Is this the best way? I'm including the code from the article below. Thank you in advance. I hope I haven't offending the author of the article...

    Scott

    ---
    <?php
    $limit=20;*//*rows*to*return
    $numresults=mysql_query("select***from*TABLE*where*YOUR*CONDITIONAL*HERE*order*by*WHATEVER");
    $numrows=mysql_num_rows($numresults);
    //*next*determine*if*offset*has*been*passed*to*script,*if*not*use*0
    if*(empty($offset))*{
    ****$offset=1;
    }
    //*get*results
    $result=mysql_query("select*id,name,phone*".
    ****"from*TABLE*where*YOUR*CONDITIONAL*HERE*".
    ****"order*by*WHATEVER*limit*$offset,$limit");
    //*now*you*can*display*the*results*returned
    while*($data=mysql_fetch_array($result))*{
    ****//*include*code*to*display*results*as*you*see*fit
    }
    //*next*we*need*to*do*the*links*to*other*results
    if*($offset==1)*{*//*bypass*PREV*link*if*offset*is*0
    ****$prevoffset=$offset-20;
    ****print*"<a*href="$PHP_SELF?offset=$prevoffset">PREV</a>*&nbsp;*n";
    }
    //*calculate*number*of*pages*needing*links
    $pages=intval($numrows/$limit);
    //*$pages*now*contains*int*of*pages*needed*unless*there*is*a*remainder*from*division
    if*($numrows%$limit)*{
    ****//*has*remainder*so*add*one*page
    ****$pages++;
    }
    for*($i=1;$i<=$pages;$i++)*{*//*loop*thru
    ****$newoffset=$limit*($i-1);
    ****print*"<a*href="$PHP_SELF?offset=$newoffset">$i</a>*&nbsp;*n";
    }
    //*check*to*see*if*last*page
    if*(!(($offset/$limit)==$pages)*&&*$pages!=1)*{
    ****//*not*last*page*so*give*NEXT*link
    ****$newoffset=$offset+$limit;
    ****print*"<a*href="$PHP_SELF?offset=$newoffset">NEXT</a><p>n";
    }
    ?>
  2. #2
  3. No Profile Picture
    Apprentice Deity
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 1999
    Location
    Niagara Falls (On the wrong side of the gorge)
    Posts
    3,237
    Rep Power
    19
    Yes, you'll query twice. Don't worry, selects in MySQL are quick and painless.

    However, instead of the select * from .. to get the number of rows, use select count(*) instead. I was half asleep when I wrote that.

Similar Threads

  1. feedback - span links = yuck
    By shreela in forum Dev Shed Lounge
    Replies: 2
    Last Post: January 28th, 2004, 11:59 PM
  2. query syntax error
    By collegebloke in forum MS SQL Development
    Replies: 1
    Last Post: January 19th, 2004, 04:08 PM
  3. Overwriting mysql query with the query results
    By onerandomday in forum PHP Development
    Replies: 1
    Last Post: January 14th, 2004, 04:45 AM
  4. query multiple selection boxes
    By zayin7232 in forum PHP Development
    Replies: 13
    Last Post: December 30th, 2003, 09:11 PM
  5. Syntax error in INSERT INTO query
    By Lee Miles in forum Visual Basic Programming
    Replies: 6
    Last Post: December 19th, 2003, 07:37 AM

IMN logo majestic logo threadwatch logo seochat tools logo