PHP - Pagination Picks Correct Number of Pages, But Doesn't Divide Up Rows
<p>The code below is an attempt at pagination.  It doesn't quite work.  I am playing around with a result that has 49 rows.  If I use $rowsperpage = 10;, the code below will correctly have hyperlinks to 4 pages at the bottom.  However, it still displays all 49 rows on page one, and clicking on the subsequent pages returns a blank page.</p>

<p>So my question is: how do I get the code below to correctly divide and allocate the rows amongst the pages following page 1?</p>

<p>Thanks in advance,</p>

<p>John     </p>

<pre><code>&lt;?

    //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 == "")
    {
    session_write_close();
    header("Location:http://www.example.com/index.php");
    exit;
    unset($_SESSION['find']);   

    }

    // Otherwise we connect to our Database
    mysql_connect("mysqlv10", "username", "password") or die(mysql_error());
    mysql_select_db("database") or die(mysql_error());

    // We preform a bit of filtering

    $find = strip_tags($find);
    $find = trim ($find);
    $find = strtolower($find);
    $find = mysql_real_escape_string($find);




    $result=mysql_query("SHOW TABLES FROM database LIKE '$find'")
    or die(mysql_error());

    $_GET['result'] = $result;


    if(mysql_num_rows($result)&gt;0){
    while($table=mysql_fetch_row($result)){

    $_GET[`$table[0]`] = $table;
    $_SESSION['table']= $table;

    $presult = mysql_query("SELECT COUNT(*) FROM `$table[0]`") or die(mysql_error());

    $rr = mysql_fetch_row($presult);  
    $numrows = $rr[0]; 
    $rowsperpage = 50; 
    $totalpages = ceil($numrows / $rowsperpage);

    // get the current page or set a default  
    if (isset($_GET['currentpage']) &amp;&amp; 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 &gt; $totalpages) {  
       // set current page to last page  
       $currentpage = $totalpages;  
    } // end if  
    // if current page is less than first page...  
    if ($currentpage &lt; 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 "&lt;p class=\"topic\"&gt;$table[0]&lt;/p&gt;\n";
    $r=mysql_query("SELECT * , votes_up - votes_down AS effective_vote FROM `$table[0]` ORDER BY effective_vote DESC");

    $_GET[`$r[0]`] = $r;

    print "&lt;table class=\"navbar\"&gt;\n";
    while($row=mysql_fetch_array($r)){

    $_GET[`$row[0]`] = $row;
    $_SESSION['row']= $row;

    $effective_vote = $row['votes_up'] - $row['votes_down']; 

    print "&lt;tr&gt;";

    print "&lt;td class='sitename'&gt;".'&lt;a href="http://'.$row['site'].'" class="links2"&gt;'.$row['site'].'&lt;/a&gt;'."&lt;/td&gt;";
    print "&lt;td class='votes'&gt;".'&lt;span class="selection_count" id="selection_count'.$row['id'].'"&gt;'.number_format($effective_vote).'&lt;/span&gt;'."&lt;/td&gt;";
    print "&lt;td class='ballot'&gt;".'&lt;span class="button" id="button'.$row['id'].'"&gt;'.'&lt;a href="javascript:;" class="cell1" id="'.$row['id'].'"&gt;'.Select.'&lt;/a&gt;'.'&lt;/span&gt;'."&lt;/td&gt;";
    }
    print "&lt;/tr&gt;\n";
    }
    print "&lt;/table&gt;\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 &gt; 1) {  
       // show &lt;&lt; link to go back to page 1  
       echo " &lt;a href='{$_SERVER['PHP_SELF']}?currentpage=1&amp;find={$_SESSION['find']}'&gt;&lt;&lt;&lt;/a&gt; ";  
       // get previous page num  
       $prevpage = $currentpage - 1;  
       // show &lt; link to go back to 1 page  
       echo " &lt;a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage&amp;find={$_SESSION['find']}'&gt;&lt;&lt;/a&gt; ";  
    } // end if   

    // loop to show links to range of pages around current page  
    for ($x = ($currentpage - $range); $x &lt; (($currentpage + $range) + 1); $x++) {  
       // if it's a valid page number...  
       if (($x &gt; 0) &amp;&amp; ($x &lt;= $totalpages)) {  
          // if we're on current page...  
          if ($x == $currentpage) {  
             // 'highlight' it but don't make a link  
             echo " [&lt;b&gt;$x&lt;/b&gt;] ";  
          // if not current page...  
          } else {  
             // make it a link  
         echo " &lt;a href='{$_SERVER['PHP_SELF']}?currentpage=$x&amp;find={$_SESSION['find']}'&gt;$x&lt;/a&gt; ";  
          } // 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 " &lt;a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage&amp;find={$_SESSION['find']}'&gt;&gt;&lt;/a&gt; ";  
       // echo forward link for lastpage  
       echo " &lt;a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages&amp;find={$_SESSION['find']}'&gt;&gt;&gt;&lt;/a&gt; ";  
    } // end if  
    /****** end build pagination links ******/




    }
    else{
    print "";
    }


    //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)
    {


    $find = urlencode($find);


    session_write_close();
    header("Location:http://www.example.com/search2.php?find=$find");
    exit;

    }


    }
    ?&gt;
</code></pre>
