SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2009
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pagination - First page is great, next pages are blank

    Hello,

    I am very close to getting this pagination to work. The first page is fine, but then when I click on the hyperlinks for the next pages, the results are blank. After reading other threads where people have had the same problem, it appears that it is a result of a variable not being passed through to the next pages.

    However, I have the variable "find" as both a session variable and an addition to the URL of the next pages, and the next pages are still blank. Any ideas why it might not be working? Is "find" the right variable for me to be passing through to the next pages?

    Thanks,

    John

    Code:
    <?php
    session_start();
    $find = strip_tags($find);
    $find = trim ($find);
    $find = strtolower($find);
    $_SESSION['find'] = $find;
    
    ?>
    
    <?
    //This is only displayed if they have submitted the form
    if ($searching =="yes")
    {
    
    //If they did not enter a search term we give them an error
    if ($find == "")
    {
    echo "<p>You forgot to enter a search term";
    exit;
    unset($_SESSION['find']);	
    
    }
    
    // Otherwise we connect to our Database
    mysql_connect("mysqlv3", "username", "password") or die(mysql_error());
    mysql_select_db("sand2") or die(mysql_error());
    
    // We preform a bit of filtering
    
    
    $find = strip_tags($find);
    $find = trim ($find);
    $find = strtolower($find);
    
    
    $result=mysql_query("SHOW TABLES FROM sand2 LIKE '&#37;$find%'")
    or die(mysql_error());
    
    
    
    if(mysql_num_rows($result)>0){
    while($table=mysql_fetch_row($result)){
    
    $presult = mysql_query("SELECT COUNT(*) FROM `$table[0]`") or die(mysql_error());
    
    $rr = mysql_fetch_row($presult);  
    $numrows = $rr[0]; 
    $rowsperpage = 20; 
    $totalpages = ceil($numrows / $rowsperpage);
    
    // get the current page or set a default  
    if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {  
       // cast var as int  
       $currentpage = (int) $_GET['currentpage'];  
    } else {  
       // default page num  
       $currentpage = 1;  
    } // end if  
    
    // if current page is greater than total pages...  
    if ($currentpage > $totalpages) {  
       // set current page to last page  
       $currentpage = $totalpages;  
    } // end if  
    // if current page is less than first page...  
    if ($currentpage < 1) {  
       // set current page to first page  
       $currentpage = 1;  
    } // end if  
    
    // the offset of the list, based on current page   
    $offset = ($currentpage - 1) * $rowsperpage; 
    
    
    print "<p class=\"topic\">$table[0]</p>\n";
    $r=mysql_query("SELECT * , votes_up - votes_down AS effective_vote FROM `$table[0]` ORDER BY effective_vote DESC LIMIT $offset, $rowsperpage");
    
    
    
    print "<table class=\"navbar\">\n";
    while($row=mysql_fetch_array($r)){
    
    
    $effective_vote = $row['votes_up'] - $row['votes_down']; 
    
    print "<tr>";
    
    print "<td>".'<a href="'.$row['site'].'" class="links2">'.$row['site'].'</a>'."</td>";
    print "<td class='votes'>".'<span class="votes_count" id="votes_count'.$row['id'].'">'.number_format($effective_vote).'</span>'."</td>";
    print "<td class='ballot'>".'<span class="button" id="button'.$row['id'].'">'.'<a href="javascript:;" class="cell1" id="'.$row['id'].'">'.Vote.'</a>'.'</span>'."</td>";
    }
    print "</tr>\n";
    }
    print "</table>\n";
    
    
    $range = 3;  
      
    /******  build the pagination links ******/  
    // range of num links to show    
      
    // if not on page 1, don't show back links  
    if ($currentpage > 1) {  
       // show << link to go back to page 1  
       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1&find={$find}'><<</a> ";  
       // get previous page num  
       $prevpage = $currentpage - 1;  
       // show < link to go back to 1 page  
       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage&find={$find}'><</a> ";  
    } // end if   
      
    // loop to show links to range of pages around current page  
    for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {  
       // if it's a valid page number...  
       if (($x > 0) && ($x <= $totalpages)) {  
          // if we're on current page...  
          if ($x == $currentpage) {  
             // 'highlight' it but don't make a link  
             echo " [<b>$x</b>] ";  
          // if not current page...  
          } else {  
             // make it a link  
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x&find={$find}'>$x</a> ";  
          } // end else  
       } // end if   
    } // end for  
               
    // if not on last page, show forward and last page links      
    if ($currentpage != $totalpages) {   
       // get next page  
       $nextpage = $currentpage + 1;  
        // echo forward link for next page   
       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage&find={$find}'>></a> ";  
       // echo forward link for lastpage  
       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages&find={$find}'>>></a> ";  
    } // end if  
    /****** end build pagination links ******/  
    
    
    }
    
    
    else{
    print "None found";
    }
    
    
    
    //This counts the number or results - and if there wasn't any it gives them a little message explaining that
    $anymatches=mysql_num_rows($result);
    if ($anymatches == 0)
    {
    echo "Sorry, but we can not find an entry to match your query<br><br>";
    unset($_SESSION['find']);	
    
    }
    
    
    }
    ?>

  2. #2
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a feeling that you are asuming that register_globlas is on.
    If you are using this code on a site where register globals is off it won't work.

    try to change the 3th line:

    PHP Code:
    <?php
    $find 
    strip_tags($_GET['find']);
    ?>
    coding with register_globals is considered bad practice.

    more info:
    http://pl2.php.net/manual/en/ini.cor...gister-globals
    http://pl2.php.net/manual/en/security.globals.php
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2009
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi fristi,

    I appreciate the response-- I'm getting to a point where I'm willing to try anything.

    I don't know much about "register globals," but I tried changing the code as you suggested and it seemed to make my site not recognize the "find" variable when submitted from the end user.

    If it helps explain anything, "find" is coming from another page with an HTML form.

    -John

  4. #4
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ArizonaJohn View Post
    Hi fristi,

    I appreciate the response-- I'm getting to a point where I'm willing to try anything.

    I don't know much about "register globals," but I tried changing the code as you suggested and it seemed to make my site not recognize the "find" variable when submitted from the end user.

    If it helps explain anything, "find" is coming from another page with an HTML form.

    -John

    So it is passed through a post variable then:
    PHP Code:
    <?php
    $find 
    strip_tags($_POST['find']);
    ?>
    In your code you will need to make a difference between the $find comming from the form -> $_POST and the $find you pass through the url...

    In the place where you access the url passed $find, use the $_GET['find'] to get that value.
    In the place where you want to get the value of a form, use the $_POST['find'].

    This will make your code more readable. At this point I don't know which $find is passed through form or url...

    you could always use this universal part at the beginning of the code:

    PHP Code:
    <?php
    $find 
    = (empty($_POST['find'])) ? $_GET['find'] : $_POST['find'];
    ?>
    but is is better to make a difference between the two finds.
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User


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
  •