SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Jul 2002
    Location
    Sooner Nation
    Posts
    208
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pagination query

    I got a great script for pagination.

    include ("dbconnect.inc" );
    $numresults=mysql_query("SELECT * FROM classmates WHERE year='$classof'" );
    $numrows=mysql_num_rows($numresults)
    or die ("query 1 failed" );

    if (empty($offset)) {
    $offset=0;
    }

    $limit=3; // change to number of items displayed per page
    $query = "SELECT * FROM classmates WHERE year='$classof' limit $offset,$limit";
    $result = mysql_query ($query)
    or die ("query 2 failed" );

    while($r=mysql_fetch_array($result))
    {
    /* change the names to the data in your tables */
    $year=$r["year"];
    $first=$r["first"];
    $entry=$r["last"];
    $maiden=$r["maiden"];
    $classof=$classof;
    /* display whatever you want (your data in the rows are now variables)! */
    echo "$first<br>";
    echo "$year" . "<br>";
    }

    if ($offset >= 3) {
    $prevoffset = $offset - $limit;
    print "<a href=""$PHP_SELF?offset=$prevoffset>PREV</a>&nbsp;";
    }
    $pages=intval($numrows/$limit);
    if ($pages < ($numrows/$limit)){
    $pages=($pages + 1);
    }
    for ($i = 1; $i <= $pages; $i++) {
    $newoffset = $limit*($i-1);
    if ($newoffset == $offset) {
    print "$i&nbsp;\n";
    } else {
    print "<a href=""$PHP_SELF?offset=$newoffset>$i</a>&nbsp;";
    }
    }

    //show next if not last
    if (! ( ($offset/$limit) == ($pages - 1) ) && ($pages != 1) ) {
    $newoffset = $offset+$limit;
    print "<a href=""$PHP_SELF?offset=$newoffset>NEXT</a><p>";
    }

    as you can see from the queries I have set it to return values of those that graduated in a certain year from a search form select box.

    The script will return the first page of $limit=3;

    the problem is that when I click the next page I get an error - I suspect the error is because the WHERE statement is not answered. meaning $classof is not carried to the next page so I coded it into the offset like this . print "<a href=""$PHP_SELF?offset=$newoffset?classof=$classof>$i</a.>
    That didnt work either

    Any ideas how to carry the $classof over to the next page
    Last edited by soonerdm; May 18, 2003 at 16:40.
    The only function of business is to create customer value and to innovate. - Peter Drucker

  2. #2
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo '<a href="' $_SERVER['PHP_SELF'] . '?offset=' $newoffset '&classof=' $classof '">' .$i '</a>' 
    if you have more then one variable in the url you place & in between
    somepage.php?var1=value1&var2=value2 etc.
    also </a.> should not have the period

    hope that helps
    - website


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •