SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with PREV / NEXT code

    Hello!

    When I use the code below, I don't see anything except the PREV and NEXT buttons.
    Also, the PREV goes to the negatives.
    What's wrong?
    Thank you!

    PHP Code:
    <?php

    $limit
    =20// rows to return
    $numresults=mysql_query("select * from gtex where ID>1000 order by ID");
    $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 * from gtex where ID>1000 order by ID limit $offset,$limit");

    // now you can display the results returned
    $i 1;
    echo (
    ' <div align="center"> <table border="0" cellpadding="4" cellspacing="0" width="100%"> ');
    while ( 
    $row mysql_fetch_array$result ) )


    extract($row);


    echo (
    '<tr>');
    echo (
    "<td width='33%'><a href=../print_resume.php?ID=$ID target='_blank' style='color: #000080'><font face='Verdana' size='1'>$name</font></a></td>
    "
    );
    echo (
    '<td width="45%"><font face="Verdana" size="1">'.$row["description"].'</font></td>');
    echo (
    "<td width='22%' align='right'><a href=../mycoverletter.php?ID=$ID target='_blank' style='color: #CC3300'><font face='Verdana' size='1'>See
                  Cover Letter</font></a></td>"
    );
    echo (
    '</tr>');

    $i++;
    }
    echo 
    "</table>";

    // 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";
    }

    ?>
    Ginie G.

  2. #2
    And your point is.... redgoals's Avatar
    Join Date
    Feb 2002
    Location
    Birmingham, UK
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    couple of things...

    change...

    PHP Code:
    if ($offset==1) { // bypass PREV link if offset is 0
        
    $prevoffset=$offset-20;
        print 
    "<a href=\"$PHP_SELF?offset=$prevoffset\">PREV</a> &nbsp; \n";

    to

    PHP Code:
    if($offset 1) { // bypass PREV link if offset is 1
    $prevoffset=$offset-20;
    print 
    "<a href=\"$PHP_SELF?offset=$prevoffset\">PREV</a> &nbsp; \n";

    and

    PHP Code:
    if (!(($offset/$limit)==$pages) && $pages!=1) {
        
    // not last page so give NEXT link
        
    $newoffset=$offset+$limit
    to

    PHP Code:
    if($numrows-$offset $limit){ 
    $newoffset=$offset+$limit
    http://redgoals.com ... my site ... nuff said

  3. #3
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, it worked!
    Ginie G.

  4. #4
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    More questions about PREV / NEXT

    OK, I have one more question about this issue.
    The code works fine when I use this query :

    PHP Code:
    select from gtex where ID>1000 order by ID 
    But I want to use this query: (Before displaying the results, I want to give my users the possibility to choose search criteria : $category and $state)

    PHP Code:
    if($category<>"0" and $state<>"0"){
      
    $condition "state='$state' and category='$category'";
    }

    if(
    $category<>"0" and $state =="0" ){
      
    $condition "category='$category'" ;
    }

    // My Query
    $result=mysql_query("SELECT * FROM gtex WHERE $condition limit $offset,$limit"); 
    When I use the PREV / NEXT code with this query, it works for page #1, but the query is not passed on the other pages.
    What should I do to make it work for all the pages?
    Thank you !
    Ginie G.

  5. #5
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you need to pass on your extra variables (category and state) in the prev/next links as well...
    so change
    PHP Code:
    print "<a href=\"$PHP_SELF?offset=$prevoffset\">PREV</a> &nbsp; \n"
    to
    PHP Code:
    print "<a href=\"$PHP_SELF?offset=$prevoffset&category=$category&state=$state\">PREV</a> &nbsp; \n"
    and the same for your NEXT link...basically you're adding &category=$category&state=$state to your links...
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com


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
  •