zencart分页功能函数function splitPageResults()详细说明

来源:互联网 发布:侧铣头编程 编辑:程序博客网 时间:2024/05/09 21:11
split_page_results.php,实现分页
文件路径:\includes\classes\split_page_results.php
// 这是一个用于分页的类,其实分页的只是在依赖mysql的limit参数,其实这个分页是无法满足定制要求的,通常得修改class splitPageResults extends base {  var $sql_query, $number_of_rows, $current_page_number, $number_of_pages, $number_of_rows_per_page, $page_name;  /* class constructor */    // 这个构造函数,对$query传入的查询语句进行分析,$max_rows指定了每页显示的最大行数,利用$_GET[$page_holder]获取当前的页号,初始化current_page_number, number_of_rows_per_page, number_of_rows(全部总行数), number_of_pages, sql_query(这个是原来SQL查询语句添加了limit限制的SQL语句)  function splitPageResults($query, $max_rows, $count_key = '*', $page_holder = 'page', $debug = false) {}    // 显示分页链接,$max_page_links指定了显示多少个链接  function display_links($max_page_links, $parameters = '') {}    // 显示分页总数, $text_output传入格式化输出串,方便定制输出  function display_count($text_output) {}}

分页功能函数详细说明:
<?php/** * split_page_results Class. * * @package classes * @copyright Copyright 2003-2009 Zen Cart Development Team * @copyright Portions Copyright 2003 osCommerce * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0 * @version $Id: split_page_results.php 17066 2010-07-29 19:18:14Z wilt $ */if (!defined('IS_ADMIN_FLAG')) {  die('Illegal Access');}/** * Split Page Result Class * * An sql paging class, that allows for sql reslt to be shown over a number of pages using  simple navigation system * Overhaul scheduled for subsequent release * * @package classes */class splitPageResults extends base {  var $sql_query, $number_of_rows, $current_page_number, $number_of_pages, $number_of_rows_per_page, $page_name;  /* class constructor */  function splitPageResults($query, $max_rows, $count_key = '*', $page_holder = 'page', $debug = false) {    global $db;    $max_rows = ($max_rows == '' || $max_rows == 0) ? 20 : $max_rows;    $this->sql_query = preg_replace("/\n\r|\r\n|\n|\r/", " ", $query);    $this->page_name = $page_holder;    if ($debug) {      echo 'original_query=' . $query . '<br /><br />';    }    if (isset($_GET[$page_holder])) {      $page = $_GET[$page_holder];    } elseif (isset($_POST[$page_holder])) {      $page = $_POST[$page_holder];    } else {      $page = '';    }    if (empty($page) || !is_numeric($page)) $page = 1;    $this->current_page_number = $page; //当前页码    $this->number_of_rows_per_page = $max_rows;  //每页显示数量$this->sql_query;    $pos_to = strlen($this->sql_query);   //895计算sql语句字节总长度    $query_lower = strtolower($this->sql_query);      $pos_from = strpos($query_lower, ' from', 0);  //497    $pos_group_by = strpos($query_lower, ' group by', $pos_from);    if (($pos_group_by < $pos_to) && ($pos_group_by != false)) $pos_to = $pos_group_by;    $pos_having = strpos($query_lower, ' having', $pos_from);    if (($pos_having < $pos_to) && ($pos_having != false)) $pos_to = $pos_having;    $pos_order_by = strpos($query_lower, ' order by', $pos_from); //841查询sql语句中sorder by出现的位置,从$pos_form处开始查找    if (($pos_order_by < $pos_to) && ($pos_order_by != false)) $pos_to = $pos_order_by; //841sql语句截取order by之前的部分,用于limit int,int页面查询    if (strpos($query_lower, 'distinct') || strpos($query_lower, 'group by')) {      $count_string = 'distinct ' . zen_db_input($count_key);    } else {      $count_string = zen_db_input($count_key); //* 函数传值,要查询的语句 此处为*    }    $count_query = "select count(" . $count_string . ") as total " .    substr($this->sql_query, $pos_from, ($pos_to - $pos_from));  //查询结果总长度查询语句    if ($debug) {      echo 'count_query=' . $count_query . '<br /><br />';    }    $count = $db->Execute($count_query); //执行结果总数查询    $this->number_of_rows = $count->fields['total'];  //查询结果总数160    $this->number_of_pages = ceil($this->number_of_rows / $this->number_of_rows_per_page);  //页面总数14    if ($this->current_page_number > $this->number_of_pages) {      $this->current_page_number = $this->number_of_pages;    }    $offset = ($this->number_of_rows_per_page * ($this->current_page_number - 1)); //已经查询的产品数    // fix offset error on some versions    if ($offset <= 0) { $offset = 0; }    $this->sql_query .= " limit " . ($offset > 0 ? $offset . ", " : '') . $this->number_of_rows_per_page; //页面查询语句  }  /* class functions */  // display split-page-number-links  function display_links($max_page_links, $parameters = '') {  //$max_page_links导航链接显示的数量,后台控制    global $request_type;    $display_links_string = '';    $class = '';    if (zen_not_null($parameters) && (substr($parameters, -1) != '&')) $parameters .= '&';  //disp_order=6&,若最后一个字符不是& 则替换成&字符    // previous button - not displayed on first page    if ($this->current_page_number > 1) { //若当前页不为第一页,则显示First页面和Previous页面$display_links_string .= '<a href="' . zen_href_link($_GET['main_page'], $parameters . $this->page_name . '=1', $request_type) . '" title=" ' . PREVNEXT_TITLE_FIRST_PAGE . ' ">' . PREVNEXT_BUTTON_FIRST . '</a>  ';$display_links_string .= '<a href="' . zen_href_link($_GET['main_page'], $parameters . $this->page_name . '=' . ($this->current_page_number - 1), $request_type) . '" title=" ' . PREVNEXT_TITLE_PREVIOUS_PAGE . ' ">' . PREVNEXT_BUTTON_PREV . '</a>  ';}    // check if number_of_pages > $max_page_links   $cur_window_num = intval($this->current_page_number / $max_page_links);   //当前循环计数=当前页面/导航链接显示数量取整    if ($this->current_page_number % $max_page_links) $cur_window_num++;  //当前循环计数+1    $max_window_num = intval($this->number_of_pages / $max_page_links);  //总循环计数页面    if ($this->number_of_pages % $max_page_links) $max_window_num++;  //总循环计数页面+1    // previous window of pages    if ($cur_window_num > 1) $display_links_string .= '<a href="' . zen_href_link($_GET['main_page'], $parameters . $this->page_name . '=' . (($cur_window_num - 1) * $max_page_links), $request_type) . '" title=" ' . sprintf(PREVNEXT_TITLE_PREV_SET_OF_NO_PAGE, $max_page_links) . ' ">...</a>';  //显示向前一页循环链接    // page nn button    for ($jump_to_page = 1 + (($cur_window_num - 1) * $max_page_links); ($jump_to_page <= ($cur_window_num * $max_page_links)) && ($jump_to_page <= $this->number_of_pages); $jump_to_page++) { //循环输出页面      if ($jump_to_page == $this->current_page_number) {        $display_links_string .= ' <strong class="current">' . $jump_to_page . '</strong> ';      } else {        $display_links_string .= ' <a href="' . zen_href_link($_GET['main_page'], $parameters . $this->page_name . '=' . $jump_to_page, $request_type) . '" title=" ' . sprintf(PREVNEXT_TITLE_PAGE_NO, $jump_to_page) . ' ">' . $jump_to_page . '</a> ';      }    }    // next window of pages    if ($cur_window_num < $max_window_num) {$display_links_string .= '<a href="' . zen_href_link($_GET['main_page'], $parameters . $this->page_name . '=' . (($cur_window_num) * $max_page_links + 1), $request_type) . '" title=" ' . sprintf(PREVNEXT_TITLE_NEXT_SET_OF_NO_PAGE, $max_page_links) . ' ">...</a> '; ////显示向后一页循环链接}    // next button    if (($this->current_page_number < $this->number_of_pages) && ($this->number_of_pages != 1)) { //若当前页不为第一页,则显示Next页面和Last页面$display_links_string .= ' <a href="' . zen_href_link($_GET['main_page'], $parameters . 'page=' . ($this->current_page_number + 1), $request_type) . '" title=" ' . PREVNEXT_TITLE_NEXT_PAGE . ' ">' . PREVNEXT_BUTTON_NEXT . '</a> ';$display_links_string .=' <a href="' . zen_href_link($_GET['main_page'], $parameters . 'page=' . $this->number_of_pages, $request_type) . '" title=" ' . PREVNEXT_TITLE_LAST_PAGE . ' ">' . PREVNEXT_BUTTON_LAST . '</a> ';}    if ($display_links_string == ' <strong class="current">1</strong> ') {      return ' ';    } else {      return $display_links_string;    }  }  // display number of total products found  function display_count($text_output) {    $to_num = ($this->number_of_rows_per_page * $this->current_page_number); //当前页面显示的产品编号    if ($to_num > $this->number_of_rows) $to_num = $this->number_of_rows; //若当前页面计算出来的产品编号大于总的产品数,则取较小值    $from_num = ($this->number_of_rows_per_page * ($this->current_page_number - 1)); //上一页查询结束的产品编号    if ($to_num == 0) {      $from_num = 0;    } else {      $from_num++; //当前页面查询开始产品编号,即上一页查询结束产品编号+1    }    if ($to_num <= 1) {        // don't show count when 1      return '';    } else {      return sprintf($text_output, $from_num, $to_num, $this->number_of_rows);  //以传入的字符串格式输出页码参数    }  }}?>


原创粉丝点击