Php 使用单个文件对多个搜索查询进行分页
我有一个文件example.com/search\u category.php。数据库中有不同的子类别。我不想为每个类别打开文件,而是希望使用单个文件分页。如果我打开每个类别的文件,例如example.com/search\u category/subcategory1.php、example.com/search\u category/subcategory2.php等,那么就没有问题。如何使用单个文件分页。我正在使用以下分页代码。提前谢谢Php 使用单个文件对多个搜索查询进行分页,php,pagination,Php,Pagination,我有一个文件example.com/search\u category.php。数据库中有不同的子类别。我不想为每个类别打开文件,而是希望使用单个文件分页。如果我打开每个类别的文件,例如example.com/search\u category/subcategory1.php、example.com/search\u category/subcategory2.php等,那么就没有问题。如何使用单个文件分页。我正在使用以下分页代码。提前谢谢 // how many rows to show p
// 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>
//每页显示多少行
$rowsPerPage=5;
//默认情况下,我们显示第一页
$page_num=1;
//如果定义了$\u GET['page'],则将其用作页码,$\u GET从url中获取页码
//由下面的$page_分页设置
if(isset($_GET['page']){$page_num=$_GET['page'];}
//限制查询的起始点
$offset=($page_num-1)*$rowsPerPage;
//零是一个不正确的页面,所以将零切换为1,主要是因为它会导致SQL错误
如果($page_num==0){$page_num=1;}
//计算偏移量
$sql=“从xyz中选择*,其中子类别=“$subcategory”按信息排序\u id ASC LIMIT$offset,$rowsPerPage”;
$res=mysql\u query($sql)或die(mysql\u error());
//数据库中有多少行
$sql2=“从xyz中选择计数(子类别)作为numrows,其中子类别='$subcategory';
$res2=mysql\u query($sql2)或die(mysql\u error());
$row2=mysql\u fetch\u数组($res2);
$numrows=$row2['numrows'];
//打印随机数
while($row=mysql\u fetch\u数组($res))
{
摘录(行);
//在这里回显您的表格内容。
回音“\n”;
回音“\n”;
$info_id=$row['info_id'];
回声“;
回显“$info_id.”;
回显“$bname.”;
回显“$text.”;
回声“;
}
?>
我认为我在传递变量时犯了一些错误。有人能解决我的问题吗?我想我在传递变量时犯了一些错误。谁能解决我的问题?