Paginate记录客户端问题
我正在使用php,mysql进行搜索,过滤操作。
我的分页课程是
class Paginator{ var $items_per_page; var $items_total; var $current_page; var $num_pages; var $mid_range; var $low; var $limit; var $return; var $default_ipp; //var $querystring; var $ipp_array; function Paginator() { $this->current_page = 1; $this->mid_range = 2; $this->ipp_array = array(2,4,6,8,10,'All'); $this->items_per_page = (!empty($_GET['ipp'])) ? $_GET['ipp']:$this->default_ipp; } function paginate() { if(!isset($this->default_ipp)) $this->default_ipp='2'; if($_GET['ipp'] == 'All') { $this->num_pages = 1; //$this->items_per_page = $this->default_ipp; } else { if(!is_numeric($this->items_per_page) OR $this->items_per_page items_per_page = $this->default_ipp; $this->num_pages = ceil($this->items_total/$this->items_per_page); } $this->current_page = (isset($_GET['page'])) ? (int) $_GET['page'] : 1 ; // must be numeric > 0 $prev_page = $this->current_page-1; $next_page = $this->current_page+1; if($_GET) { $args = explode("&",$_SERVER['QUERY_STRING']); foreach($args as $arg) { $keyval = explode("=",$arg); if($keyval[0] != "page" And $keyval[0] != "ipp") $this->querystring .= "&" . $arg; } } if($_POST) { foreach($_POST as $key=>$val) { if($key != "page" And $key != "ipp") $this->querystring .= "&$key=$val"; } } if($this->num_pages > 4) { $this->return = ($this->current_page > 1 And $this->items_total >= 10) ? "items_per_page$this->querystring\">« Previous ":"« Previous "; $this->start_range = $this->current_page - floor($this->mid_range/2); $this->end_range = $this->current_page + floor($this->mid_range/2); if($this->start_range end_range += abs($this->start_range)+1; $this->start_range = 1; } if($this->end_range > $this->num_pages) { $this->start_range -= $this->end_range-$this->num_pages; $this->end_range = $this->num_pages; } $this->range = range($this->start_range,$this->end_range); for($i=1;$inum_pages;$i++) { if($this->range[0] > 2 And $i == $this->range[0]) $this->return .= " ... "; // loop through all pages. if first, last, or in range, display if($i==1 Or $i==$this->num_pages Or in_array($i,$this->range)) { $this->return .= ($i == $this->current_page And $_GET['page'] != 'All') ? "num_pages\" class=\"current\" href=\"#\">$i ":"num_pages\" href=https://stackoverflow.com/questions/10928368/paginate-records-on-client-side-issue/\"$_SERVER[PHP_SELF]?page=$i&ipp=$this->items_per_page$this->querystring\">$i "; } if($this->range[$this->mid_range-1] num_pages-1 And $i == $this->range[$this->mid_range-1]) $this->return .= " ... "; } $this->return .= (($this->current_page num_pages And $this->items_total >= 10) And ($_GET['page'] != 'All') And $this->current_page > 0) ? "items_per_page$this->querystring\">Next »\n":"» Next\n"; $this->return .= ($_GET['page'] == 'All') ? "All \n":"querystring\">All \n"; } else { for($i=1;$inum_pages;$i++) { $this->return .= ($i == $this->current_page) ? "$i ":"items_per_page$this->querystring\">$i "; } $this->return .= "querystring\">All \n"; } $this->low = ($this->current_page current_page-1) * $this->items_per_page; if($this->current_page items_per_page = 0; $this->limit = ($_GET['ipp'] == 'All') ? "":" LIMIT $this->low,$this->items_per_page"; } function display_items_per_page() { $items = ''; if(!isset($_GET[ipp])) $this->items_per_page = $this->default_ipp; foreach($this->ipp_array as $ipp_opt) $items .= ($ipp_opt == $this->items_per_page) ? "$ipp_opt\n":"$ipp_opt\n"; return "Items per page:querystring';return false\">$items\n"; } function display_jump_menu() { for($i=1;$inum_pages;$i++) { $option .= ($i==$this->current_page) ? "$i\n":"$i\n"; } return "Page:items_per_page$this->querystring';return false\">$option\n"; } function display_pages() { return $this->return; } } ?>
我使用如下复选框执行过滤:
<input type="checkbox" id="checkbox1" class="checkbox1" value="" name="Suburb_check[]" onClick="changeResults();" onChange="" > ()
filter的Javascript / ajax代码
function changeResults(){ var data = { 'venue[]' : []}; $("input:checked").each(function() { var chck1 = $(this).val(); //alert(chck1); data['venue[]'].push($(this).val()); }); $.ajax({ type : 'POST', url : 'process.php', data : data, success : function(data){ $('#project_section').html(data); // replace the contents coming from php file } }); }
process.php文件
include_once("includes/classes/db_connect.php"); include_once("pagination/paginator.class.php"); $pages = new Paginator(); $value= array(); foreach($_POST as $key=>$value) { $value[] = $venue[$key][$value]; } $countArray = count($value); //echo $countArray; /*echo "
"; print_r($value); echo ""; exit;*/ if($countArray=='2') { $where = "cities.city_name like '%".$_SESSION['$cityName']."%' and suburbs.suburb_name ='".$value[0]."' "; } else if($countArray=='3') { $where = "cities.city_name like '%".$_SESSION['$cityName']."%' and (suburbs.suburb_name ='".$value[0]."' or suburbs.suburb_name ='".$value[1]."')"; } else if($countArray=='4') { $where = "cities.city_name like '%".$_SESSION['$cityName']."%' and (suburbs.suburb_name ='".$value[0]."' or suburbs.suburb_name ='".$value[1]."' or suburbs.suburb_name ='".$value[2]."')"; } else if($countArray=='5') { $where = "cities.city_name like '%".$_SESSION['$cityName']."%' and (suburbs.suburb_name ='".$value[0]."' or suburbs.suburb_name ='".$value[1]."' or suburbs.suburb_name ='".$value[2]."' or suburbs.suburb_name ='".$value[3]."')"; } else{ $where = "cities.city_name like '%".$_SESSION['$cityName']."%' " ; } $countSql="SELECT count(*) as total from properties inner join cities on properties.city_id=cities.city_id inner join suburbs on properties.suburb_id=suburbs.suburb_id where ".$where." "; //echo $countSql; $resultCount = mysql_query($countSql); $data = mysql_fetch_row($resultCount); $pages->items_total = $data[0]; $pages->mid_range = 2; $pages->paginate(); echo $pages->display_pages(); echo $pages->display_items_per_page(); $sql="SELECT * from properties inner join cities on properties.city_id=cities.city_id inner join suburbs on properties.suburb_id=suburbs.suburb_id where ".$where." $pages->limit"; //echo $sql; $result = mysql_query($sql); //$data = mysql_fetch_assoc($result); /*echo "
" ; print_r($data); echo "" ; */ while($row=mysql_fetch_assoc($result)) { ?>
我返回分页结果,但是当我点击页码时,它将我带到process.php,因为分页类使用$ _SERVER [PHP_SELF]如何在不更改页面URL的情况下对结果进行分页,即使用ajax进行分页实现。 我无法进行太多更改,如果对此进行修改使其有效,那将是有用的。(这就是为什么我必须添加尽可能多的代码。)抱歉,如果它太多了。
谢谢编辑 @Gavin这是我的process.php
processRegion.php与HTML
Results for properties for sale in .Lacs - LacsPrice on Requestdisplay_pages(); echo $pages->display_items_per_page(); ?>正如我的评论中所述。
您可以执行以下操作:
$(document).ready(function() { $('.paginate').live('click', function(e) { e.preventDefault(); var btnPage = $(this); $.ajax( { url : btnPage.attr('href'), success : function(resp) { // replace current results with new results. $('#project_section').html(resp); }, error : function() { window.location.href = btnPage.attr('href'); } }); }); });
以上内容将复制您点击每个分页链接。
我建议接下来要做的是将生成“结果”列表的PHP代码和HTML分离到一个单独的文件中。
这样,在显示结果的页面上,您可以简单地使用
include('path-to-results-file.php');
这将适用于非Ajax请求,然后你可以这样做:Process.php
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { include('path-to-results-file.php'); die(); }
以上将检测是否已经发出ajax请求,如果是,则不是显示包括结果的整个页面,而是仅显示结果和分页。
更新以包含更好的解释
以下是我的意思的一个非常简单的例子。
当前的process.php
// currently contains all of the code required // to query the database etc. ?> ...
// currently contains all of the code required to display // the results table and pagination links. ?>
新的process.php
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { include('path-to-results-file.php'); die(); } ?> ... include('path-to-results-file.php'); ?>
新的path-to-results-file.php
// currently contains all of the code required // to query the database etc. ?>
// currently contains all of the code required to display // the results table and pagination links. ?>
现在……当你通常通过浏览器或者禁用javascript时,你可以去
process.php
。 它的工作方式与现在没有Javascript的方式相同。当您转到
process.php
然后单击其中一个分页链接(启用了javascript)时,process.php
将检测到您正在使用Ajax并仅发送回结果表。