我有一个页面显示表中MySQL数据库的记录。在此页面上有多个过滤器,用户可以单击以显示所需的结果。我最近添加了分页,工作正常。
首次加载页面时,它将显示所有结果。现在它显示9个结果。出于测试目的,我每页仅允许6个结果。我的问题是让用户首先加载页面,然后点击分页的第2页。在第二页,他们决定添加一个过滤器。我有我的过滤器设置与PHP if语句来更改查询。如果他们点击第一个选项,只允许2个结果,这样做,但它不会自动将我带回到第1页以显示那些结果。它只停留在第2页,用户必须单击第1页才能获得正确的结果。
如果点击过滤器,如何更改分页脚本以转到第1页?
// Pagination Function
function pagination($query,$per_page=6,$page=1,$url='?'){
global $conDB;
$query = "SELECT COUNT(*) as `num` FROM {$query}";
$row = mysqli_fetch_array(mysqli_query($conDB,$query));
$total = $row['num'];
$adjacents = "2";
$prevlabel = "‹ Prev";
$nextlabel = "Next ›";
$lastlabel = "Last ››";
$page = ($page == 0 ? 1 : $page);
$start = ($page - 1) * $per_page;
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total/$per_page);
$lpm1 = $lastpage - 1; // //last page minus 1
$pagination = "";
if($lastpage > 1){
$pagination .= "<ul class='pagination'>";
$pagination .= "<li class='page_info'>Page {$page} of {$lastpage}</li>";
if ($page > 1) $pagination.= "<li><a href='{$url}page={$prev}'>{$prevlabel}</a></li>";
if ($lastpage < 7 + ($adjacents * 2)){
for ($counter = 1; $counter <= $lastpage; $counter++){
if ($counter == $page)
$pagination.= "<li><a class='current'>{$counter}</a></li>";
else
$pagination.= "<li><a href='{$url}page={$counter}'>{$counter}</a></li>";
}
} elseif($lastpage > 5 + ($adjacents * 2)){
if($page < 1 + ($adjacents * 2)) {
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){
if ($counter == $page)
$pagination.= "<li><a class='current'>{$counter}</a></li>";
else
$pagination.= "<li><a href='{$url}page={$counter}'>{$counter}</a></li>";
}
$pagination.= "<li class='dot'>...</li>";
$pagination.= "<li><a href='{$url}page={$lpm1}'>{$lpm1}</a></li>";
$pagination.= "<li><a href='{$url}page={$lastpage}'>{$lastpage}</a></li>";
} elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) {
$pagination.= "<li><a href='{$url}page=1'>1</a></li>";
$pagination.= "<li><a href='{$url}page=2'>2</a></li>";
$pagination.= "<li class='dot'>...</li>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) {
if ($counter == $page)
$pagination.= "<li><a class='current'>{$counter}</a></li>";
else
$pagination.= "<li><a href='{$url}page={$counter}'>{$counter}</a></li>";
}
$pagination.= "<li class='dot'>..</li>";
$pagination.= "<li><a href='{$url}page={$lpm1}'>{$lpm1}</a></li>";
$pagination.= "<li><a href='{$url}page={$lastpage}'>{$lastpage}</a></li>";
} else {
$pagination.= "<li><a href='{$url}page=1'>1</a></li>";
$pagination.= "<li><a href='{$url}page=2'>2</a></li>";
$pagination.= "<li class='dot'>..</li>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) {
if ($counter == $page)
$pagination.= "<li><a class='current'>{$counter}</a></li>";
else
$pagination.= "<li><a href='{$url}page={$counter}'>{$counter}</a></li>";
}
}
}
if ($page < $counter - 1) {
$pagination.= "<li><a href='{$url}page={$next}'>{$nextlabel}</a></li>";
$pagination.= "<li><a href='{$url}page=$lastpage'>{$lastlabel}</a></li>";
}
$pagination.= "</ul>";
}
return $pagination;
}
?>
<?php
$page = (int)(!isset($_GET["page"]) ? 1 : $_GET["page"]);
if ($page <= 0) $page = 1;
$per_page = 6; // Set how many records do you want to display per page.
$startpoint = ($page * $per_page) - $per_page;
$statement = "`vendor_ads` WHERE `showingads` = 'yes' AND `admin` = 'enabled' AND `vendortype` = 'venue' AND (" . $str . ") ORDER BY cpc DESC"; // Change `records` according to your table name.
$results = mysqli_query($conDB,"SELECT * FROM {$statement} LIMIT {$startpoint} , {$per_page}");
if (mysqli_num_rows($results) != 0) {
// displaying records.
while ($row = mysqli_fetch_array($results)) {
echo "<table class='tablebox' width='1000' border='0' cellspacing='0'>
<tr><td><a href='post_click.php?url=".$row["url"]."&username=".$row["username"]."&id=".$row["id"]."&cpc=".$row["cpc"]."'> <img src='".$row["imageurl"]."'></a>
<a href='favorite_upload_script.php?favoriteid=".$row["favorite_id"]."&vendorname=".$row["vendorname"]."&imageurl=".$row["imageurl"]."&urlmainpage=".$row["url"]."&redirecturl=http://www.mybudgetbride.com/ad-display-template.php'><img src='/images/favorite_button.jpg' /></a>
</td>
</tr>
<tr>
<td><center><strong>".$row["vendorname"]."</strong></center></td>
</tr>
</table>";
}
}
else {
$statement = "`vendor_ads` WHERE `showingads` = 'yes' AND `admin` = 'enabled' AND `vendortype` = 'venue' ORDER BY cpc DESC"; // Change `records` according to your table name.
$results = mysqli_query($conDB,"SELECT * FROM {$statement} LIMIT {$startpoint} , {$per_page}");
if (mysqli_num_rows($results) != 0) {
// displaying records.
while ($row = mysqli_fetch_array($results)) {
echo "<table class='tablebox' width='1000' border='0' cellspacing='0'>
<tr><td><a href='post_click.php?url=".$row["url"]."&username=".$row["username"]."&id=".$row["id"]."&cpc=".$row["cpc"]."'> <img src='".$row["imageurl"]."'></a>
<a href='favorite_upload_script.php?favoriteid=".$row["favorite_id"]."&vendorname=".$row["vendorname"]."&imageurl=".$row["imageurl"]."&urlmainpage=".$row["url"]."&redirecturl=http://www.mybudgetbride.com/ad-display-template.php'><img src='/images/favorite_button.jpg' /></a>
</td>
</tr>
<tr>
<td><center><strong>".$row["vendorname"]."</strong></center></td>
</tr>
</table>";
}
}
}
?>
</td>
</tr>
<tr>
<td><?php echo pagination($statement,$per_page,$page,$url='?');?></td>
</tr>
</table>
</div><!-- .wrap -->