如何在 php mysql 中对过滤后的结果进行分页?
我的分页只显示存储在数据库中的所有文件,但是当我想搜索例如所有采访的评论记录时,它会显示所有采访评论的文件,然后在第二页它仍然显示过滤结果。
我的问题是当我单击分页时,第二页将再次检索存储在数据库中的所有文件。我想要的第二页是过滤后的结果。
`<?php
include 'functions.php';
ob_start();
if(!loggedin())
{ header("Location:login.php");
exit();
}
if(isset($_POST['edit']))
{
$_SESSION['id']=$_POST['id'];
header("Location:edit.php");
}
if(isset($_POST['BOOK']))
{
$_SESSION['id']=$_POST['id'];
header("Location:booking_edit.php");}
if($_POST['types'] == 'name' || $_POST['types'] == 'mobile' || $_POST['types'] == 'OccasionType')
{
$_SESSION['filter']=$_POST['filter'];
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>Manorama Party Plot</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="css/flexigrid.css" media="all" />
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"> </script>
<script type="text/javascript">
$(function(){
// Datepicker
$('#From,#To').datepicker({
inline: true
});
//hover states on the static widgets
$('#dialog_link, ul#icons li').hover(
function() { $(this).addClass('ui-state-hover'); },
function() { $(this).removeClass('ui-state-hover'); }
);
});
</script>
<script type="text/javascript">
$(document).ready(function() {
$('#types').change(function(){
if($('#types').val() === 'InquiryDate' || $('#types').val() === 'OccasionDate')
{
$('#From').show();
$('#To').show();
$('#filter').hide();
}
else
{
$('#filter').show();
$('#From').hide();
$('#To').hide();
}
});
});
</script>
<script type="text/javascript" src="js/flexigrid.js"></script>
</head>
<body>
<div class="wrapper filter_wrapper">
<div id="header">
<?php include 'header.php'; ?>
</div>
<div id="content"></div></div>
<div class="fillter_con">
<div class="flexigrid fillter">
<div class="pDiv">
<div class="pDiv2 axport">
<h2>Filter :</h2>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post' name='form_filter'>
<select class="filter_select" id="types" name="types">
<option value="none">none</option>
<option value="name">name</option>
<option value="mobile">mobile</option>
<option value="OccasionType">OccasionType</option>
<option value="InquiryDate">InquiryDate</option>
<option value="OccasionDate">OccasionDate</option>
</select>
<input class="filter_input" name="filter" id="filter" type="text" >
<input type="text" class="filter_input" id="From" name="From" style="display: none;" va lue="From" onblur="this.value==''?this.value='From':this.value=this.value" onfocus="this.value=='From'?this.value='':this.value=this.value" />
<input type="text" class="filter_input" id="To" name="To" style="display: none;" value="To" onblur="this.value==''?this.value='To':this.value=this.value" onfocus="this.value=='To'?this.value='':this.value=this.value" />
<input class="filter_button" type='submit' value = 'Go'>
</form>
</div>
</div>
</div>
<?php
$tbl_name="visitor_detail"; $adjacents = 3;
if($_POST['types'] == 'name') {
$query = "SELECT COUNT(*) as num FROM $tbl_name WHERE name='".$_SESSION['filter']."' ORDER BY id DESC";
}
elseif($_POST['types'] == 'mobile') {
$query = "SELECT COUNT(*) as num FROM $tbl_name WHERE mobile='".$_SESSION['filter']."' ORDER BY id DESC";
}
elseif($_POST['types'] == 'OccasionType') {
$query = "SELECT COUNT(*) as num FROM $tbl_name WHERE OccasionType='".$_SESSION['filter']."' ORDER BY id DESC";
}
elseif($_POST['types'] == 'InquiryDate') {
$query = "SELECT COUNT(*) as num FROM $tbl_name WHERE TodayDate between '".$_POST['From']."' and '".$_POST['To']."' ORDER BY id DESC";
}
elseif($_POST['types'] == 'OccasionDate') {
$query = "SELECT COUNT(*) as num FROM $tbl_name WHERE date between '".$_POST['From']."' and '".$_POST['To']."' ORDER BY id DESC";
}
else {
$query = "SELECT COUNT(*) as num FROM $tbl_name ORDER BY id DESC";
}
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];
/* Setup vars for query. */
$targetpage = "all_inquiry.php"; //your file name (the name of this file)
$limit = 10;
$page = $_GET['page'];
if($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0; //if no page var is given, set start to 0
if($_POST['types'] == 'name') {
$que = "SELECT * FROM visitor_detail WHERE name='".$_SESSION['filter']."' ORDER BY id DESC LIMIT $start, $limit";
}
elseif($_POST['types'] == 'mobile') {
$que = "SELECT * FROM visitor_detail WHERE mobile='".$_SESSION['filter']."' ORDER BY id DESC LIMIT $start, $limit ";
}
elseif($_POST['types'] == 'OccasionType') {
$que = "SELECT * FROM visitor_detail WHERE OccasionType='".$_SESSION['filter']."' ORDER BY id DESC LIMIT $start, $limit";
}
elseif($_POST['types'] == 'InquiryDate') {
$que="SELECT * FROM visitor_detail WHERE TodayDate between '".$_POST['From']."' and '".$_POST['To']."' ORDER BY id DESC LIMIT $start, $limit" ;
}
elseif($_POST['types'] == 'OccasionDate') {
$que="SELECT * FROM visitor_detail WHERE date between '".$_POST['From']."' and '".$_POST['To']."' ORDER BY id DESC LIMIT $start, $limit" ;
}
else {
$que = "SELECT * FROM visitor_detail ORDER BY id DESC LIMIT $start, $limit";
}
$sql = mysql_query($que);
if ($page == 0) $page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
//previous button
if ($page > 1)
$pagination.= "<a href=\"$targetpage?page=$prev\"><div class=\"pPrev pButton pGroup\">
<span></span>
</div></a><div class=\"btnseparator\"></div>";
else
$pagination.= "<div class=\"pPrev pButton pGroup\">
<span></span>
</div><div class=\"btnseparator\"></div>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<div class=\"pGroup\">$counter</div>";
else
$pagination.= "<a href=\"$targetpage? page=$counter\"><div class=\"pGroup\">$counter</div></a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<div class=\"pGroup\">$counter</div>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\"><div class=\"pGroup\">$counter</div></a>";
}
$pagination.= "<div class=\"pGroup\">...</div>";
$pagination.= "<div class=\"pGroup\"><a href=\"$targetpage? page=$lpm1\">$lpm1</a></div>";
$pagination.= "<div class=\"pGroup\"><a href=\"$targetpage? page=$lastpage\">$lastpage</a></div>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<div class=\"pGroup\"><a href=\"$targetpage? page=1\">1</a></div>";
$pagination.= "<div class=\"pGroup\"><a href=\"$targetpage?page=2\">2</a></div>";
$pagination.= "<div class=\"pGroup\">...</div>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= " <div class=\"pGroup\">$counter</div>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\"><div class=\"pGroup\">$counter</div></a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "<div class=\"pGroup\">...</div>";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<div class=\"pGroup\">$counter</div>";
else
$pagination.= "<a href=\"$targetpage? page=$counter\"><div class=\"pGroup\">$counter</div></a>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?page=$next\"><div class=\"pNext pButton pGroup\">
</div></a><div class=\"btnseparator\"></div>";
else
$pagination.= "<div class=\"pNext pButton pGroup\"></div><div class=\"btnseparator\"></div>";
$pagination.= "</div>\n";
}
if ($page == 0) $page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
//previous button
if ($page > 1)
$pagination.= "<a href=\"$targetpage?page=$prev\"><div class=\"pPrev pButton pGroup\">
<span></span>
</div></a><div class=\"btnseparator\"></div>";
else
$pagination.= "<div class=\"pPrev pButton pGroup\">
<span></span>
</div><div class=\"btnseparator\"></div>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<div class=\"pGroup\">$counter</div>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\"><div class=\"pGroup\">$counter</div></a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<div class=\"pGroup\">$counter</div>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\"><div class=\"pGroup\">$counter</div></a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a><div class=\"btnseparator\"></div>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= " <div class=\"pGroup\">$counter</div>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\"><div class=\"pGroup\">$counter</div></a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a><div class=\"btnseparator\"></div>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<div class=\"pGroup\">$counter</div>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\"><div class=\"pGroup\">$counter</div></a>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?page=$next\"><div class=\"pNext pButton pGroup\">
</div></a><div class=\"btnseparator\"></div>";
else
$pagination.= "<div class=\"pNext pButton pGroup\"></div><div class=\"btnseparator\"></div>";
$pagination.= "</div>\n";
}?>
<div id="flexme1"><table>
<thead>
<tr>
<th width="100"><strong>Name</strong></th>
<th width="90"><strong>Mobile</strong></th>
<th width="75"><strong>Occasion Type</strong></th>
<th width="80"><strong>Occasion Date</strong></th>
<th width="70"><strong>Approx Gust</strong></th>
<th width="150"><strong>Description</strong></th>
<th width="150"><strong>Address</strong></th>
<th width="120"><strong>Email</strong></th>
<th width="75"><strong>Other Contact</strong></th>
<th width="70"><strong>Enquiry Date</strong></th>
<th width="80"><strong>Author</strong></th>
<th width="100"><strong>Action</strong></th>
</tr>
</thead></table>
<?php while($data = mysql_fetch_array($sql)){?>
<form name="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<table border="0" cellspacing="0" cellpadding="0" >
<tbody>
<tr> <input name="id" type="hidden" value="<?php echo $data['0']; ?>" >
<td><?php echo $data['2']; ?></td>
<td><?php echo $data['3']; ?></td>
<td><?php echo $data['8']; ?></td>
<td><?php echo $data['9']; ?></td>
<td><?php echo $data['10']; ?></td>
<td><?php echo $data['11']; ?></td>
<td><?php echo $data['4']; ?></td>
<td><?php echo $data['5']; ?></td>
<td><?php echo $data['6']; ?></td>
<td><?php echo $data['12']; ?></td>
<td><?php echo $data['13']; ?></td>
<td><input name="edit" type="submit" value="Edit">
<input name="BOOK" type="submit" value="Book" style="color:#d90f0f">
</td>
</tr>
</tbody>
</table>
</form>
<?php } ?></div>
<div class="flexigrid">
<div class="pDiv">
<div class="pDiv2">
<div class="pGroup">
<select name="rp" id="rp" >
<option value="10">10 </option>
<option value="15">15 </option>
<option value="20">20 </option>
<option value="30">30 </option>
<option value="50">50 </option>
</select>
</div>
<div class="btnseparator"></div>
<?=$pagination?>
<div class="axport">
<h2>Export</h2>
<div class="pGroup">
<div class="pdf_button pButton">PDF</div>
</div>
<div class="btnseparator"></div>
<div class="pGroup">
<?php
$types = $_POST['types'];
$filter = $_POST['filter'];
$from = $_POST['From'];
$to = $_POST['To'];
?>
<div class="excel_button pButton">
<form action="excel.php" method="POST">
<input name="types" type="hidden" value="<?php echo $types;?>">
<input name="filter" type="hidden" value="<?php echo $filter;?>">
<input name="from" type="hidden" value="<?php echo $from;?>">
<input name="to" type="hidden" value="<?php echo $to;?>">
<input type="submit" value="Excel">
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
//<![CDATA[
jQuery(document).ready(function($) {
$('#flexme1').flexigrid();
});
//]]>
</script>
</div>
</div>
<div id="footer">
<p>Copyright © <span><a href="#">Aum Digital Marketing</a></span> , All rights reserved</p>
</div>
</body>
</html>`