我有一个文件example.com/search_category.php。数据库中有不同的子类别。我不想为每个类别打开文件,而是想要使用单个文件进行分页。如果我打开每个类别的文件,例如example.com/search_category/subcategory1.php,example.com/search_category/subcategory2.php等,那么没有问题。如何使用单个文件进行分页。我使用以下分页代码。提前谢谢。
// 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 subcategory='$subcategory' 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(subcategory) AS numrows FROM xyz WHERE subcategory= '$subcategory' ";
$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>