分页在第二页和后续页面中不起作用

时间:2017-03-11 11:16:01

标签: php mysql pagination

关于初始页面,我的分页工作正常。但是在第二页和后续页面中它不起作用。我认为变量没有正确传递。如果我将变量'$ name'替换为'name',则没有问题。任何提示都将受到高度赞赏。

**$name = $_POST['name'];**
   // how many rows to show per page
$rowsPerPage = 5;

// by default we show first page
$page_num = 1;

// if $_GET['page'] defined, use it as page number, $_GET gets the page number out of the url
//set by the $page_pagination below
if(isset($_GET['page'])){$page_num = $_GET['page'];}

//the point to start for the limit query
$offset = ($page_num - 1) * $rowsPerPage;


// Zero is an incorrect page, so switch the zero with 1, mainly because it will cause an error with the SQL
if($page_num == 0) {$page_num = 1;}

// counting the offset
$sql = "SELECT * FROM xyz WHERE name='$name' ORDER BY info_id ASC  LIMIT $offset, $rowsPerPage ";
$res = mysql_query($sql) or die(mysql_error());

// how many rows we have in database
$sql2  = "SELECT COUNT(id) AS numrows FROM xyz WHERE name= '$name' ";
$res2  = mysql_query($sql2) or die(mysql_error());
$row2  = mysql_fetch_array($res2);
$numrows = $row2['numrows'];

// print the random numbers
while($row = mysql_fetch_array($res))
{
extract ($row);
//Echo out your table contents here.




echo "<tr class=\"alt\"  onmouseover=\"ChangeColor(this, true);\" \n"; 
 echo "style=\"cursor: pointer;\"\n";
echo " onmouseout=\"ChangeColor(this, false);\" \n"; 
echo " onclick=\"DoNav('http://www.example.com/searched_page.php?info_id={$info_id}');\">\n"; 
echo "\n";
$info_id = $row['info_id'];


echo "</td>";
echo "<td>". $info_id . "</td>";
echo "<td>". $bname . "</td>";

echo "<td>". $text . "</td>";




echo "</tr>";
}
?>
<?php
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);

$self = "http://www.example.com/search_category.php";

// creating 'previous' and 'next' link
// plus 'first page' and 'last page' link

// print 'previous' link only if we're not
// on page one
if ( $page_num > 1 ) {
    $page = $page_num - 1;
    $prev = " <a href=\"$self?page=$page\">[Prev]</a> ";

    $first = " <a href=\"$self?page=1\">[First Page]</a> ";
} else {
    $prev  = ' [Prev] ';       // we're on page one, don't enable 'previous'    link
    $first = ' [First Page] '; // nor 'first page' link
}

// print 'next' link only if we're not
// on the last page
if ( $page_num < $maxPage ) {
    $page = $page_num + 1;
    $next = " <a href=\"$self?page=$page\">[Next]</a> ";

    $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
} else {
    $next = ' [Next] ';      // we're on the last page, don't enable 'next' link
    $last = ' [Last Page] '; // nor 'last page' link
}

// print the page navigation link
//echo $first . $prev . " Showing page <strong>$page_num</strong> of       <strong>$maxPage</strong> pages " . $next . $last;



?>
</table>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td align="center" class="smaller">   
            <?php 

            // print the page navigation link
            echo '<br>';
            echo $first . $prev . " Page <strong>$page_num</strong> of    <strong>$maxPage</strong>" . $next . $last;  

            ?>
</table>

1 个答案:

答案 0 :(得分:0)

我已经解决了。是的,它是$ _GET ['name']。我改变了它,现在工作正常。

$ name = $ _ GET ['name'];