[ Index ]

PHP Cross Reference of osCMax 2.0.4

title

Body

[close]

/includes/classes/ -> split_page_results.php (source)

   1  <?php
   2  /*

   3  $Id: split_page_results.php 14 2006-07-28 17:42:07Z user $

   4  

   5    osCMax Power E-Commerce

   6    http://oscdox.com

   7  

   8    Copyright 2006 osCMax

   9  

  10    Released under the GNU General Public License

  11  */
  12  
  13    class splitPageResults {
  14      var $sql_query, $number_of_rows, $current_page_number, $number_of_pages, $number_of_rows_per_page, $page_name;
  15  
  16  /* class constructor */

  17      function splitPageResults($query, $max_rows, $count_key = '*', $page_holder = 'page') {
  18        global $HTTP_GET_VARS, $HTTP_POST_VARS;
  19  
  20        $this->sql_query = $query;
  21        $this->page_name = $page_holder;
  22  
  23        if (isset($HTTP_GET_VARS[$page_holder])) {
  24          $page = $HTTP_GET_VARS[$page_holder];
  25        } elseif (isset($HTTP_POST_VARS[$page_holder])) {
  26          $page = $HTTP_POST_VARS[$page_holder];
  27        } else {
  28          $page = '';
  29        }
  30  
  31        if (empty($page) || !is_numeric($page)) $page = 1;
  32        $this->current_page_number = $page;
  33  
  34        $this->number_of_rows_per_page = $max_rows;
  35  
  36        $pos_to = strlen($this->sql_query);
  37        $pos_from = strpos($this->sql_query, ' from', 0);
  38  
  39        $pos_group_by = strpos($this->sql_query, ' group by', $pos_from);
  40        if (($pos_group_by < $pos_to) && ($pos_group_by != false)) $pos_to = $pos_group_by;
  41  
  42        $pos_having = strpos($this->sql_query, ' having', $pos_from);
  43        if (($pos_having < $pos_to) && ($pos_having != false)) $pos_to = $pos_having;
  44  
  45        $pos_order_by = strpos($this->sql_query, ' order by', $pos_from);
  46        if (($pos_order_by < $pos_to) && ($pos_order_by != false)) $pos_to = $pos_order_by;
  47  
  48        if (strpos($this->sql_query, 'distinct') || strpos($this->sql_query, 'group by')) {
  49          $count_string = 'distinct ' . tep_db_input($count_key);
  50        } else {
  51          $count_string = tep_db_input($count_key);
  52        }
  53  
  54        $count_query = tep_db_query("select count(" . $count_string . ") as total " . substr($this->sql_query, $pos_from, ($pos_to - $pos_from)));
  55        $count = tep_db_fetch_array($count_query);
  56  
  57        $this->number_of_rows = $count['total'];
  58  
  59        $this->number_of_pages = ceil($this->number_of_rows / $this->number_of_rows_per_page);
  60  
  61        if ($this->current_page_number > $this->number_of_pages) {
  62          $this->current_page_number = $this->number_of_pages;
  63        }
  64  
  65        $offset = ($this->number_of_rows_per_page * ($this->current_page_number - 1));
  66  // LINE ADDED

  67        if($offset <0 ) $offset = 0;
  68  // LINE CHANGED: MS2 update 501112 - Added: max(...)

  69        $this->sql_query .= " limit " . max($offset, 0) . ", " . $this->number_of_rows_per_page;
  70      }
  71  
  72  /* class functions */

  73  
  74  // display split-page-number-links

  75      function display_links($max_page_links, $parameters = '') {
  76        global $PHP_SELF, $request_type;
  77  
  78        $display_links_string = '';
  79  
  80        $class = 'class="pageResults"';
  81  
  82        if (tep_not_null($parameters) && (substr($parameters, -1) != '&')) $parameters .= '&';
  83  
  84  // previous button - not displayed on first page

  85        if ($this->current_page_number > 1) $display_links_string .= '<a href="' . tep_href_link(basename($PHP_SELF), $parameters . $this->page_name . '=' . ($this->current_page_number - 1), $request_type) . '" class="pageResults" title=" ' . PREVNEXT_TITLE_PREVIOUS_PAGE . ' "><u>' . PREVNEXT_BUTTON_PREV . '</u></a>&nbsp;&nbsp;';
  86  
  87  // check if number_of_pages > $max_page_links

  88        $cur_window_num = intval($this->current_page_number / $max_page_links);
  89        if ($this->current_page_number % $max_page_links) $cur_window_num++;
  90  
  91        $max_window_num = intval($this->number_of_pages / $max_page_links);
  92        if ($this->number_of_pages % $max_page_links) $max_window_num++;
  93  
  94  // previous window of pages

  95        if ($cur_window_num > 1) $display_links_string .= '<a href="' . tep_href_link(basename($PHP_SELF), $parameters . $this->page_name . '=' . (($cur_window_num - 1) * $max_page_links), $request_type) . '" class="pageResults" title=" ' . sprintf(PREVNEXT_TITLE_PREV_SET_OF_NO_PAGE, $max_page_links) . ' ">...</a>';
  96  
  97  // page nn button

  98        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++) {
  99          if ($jump_to_page == $this->current_page_number) {
 100            $display_links_string .= '&nbsp;<b>' . $jump_to_page . '</b>&nbsp;';
 101          } else {
 102            $display_links_string .= '&nbsp;<a href="' . tep_href_link(basename($PHP_SELF), $parameters . $this->page_name . '=' . $jump_to_page, $request_type) . '" class="pageResults" title=" ' . sprintf(PREVNEXT_TITLE_PAGE_NO, $jump_to_page) . ' "><u>' . $jump_to_page . '</u></a>&nbsp;';
 103          }
 104        }
 105  
 106  // next window of pages

 107        if ($cur_window_num < $max_window_num) $display_links_string .= '<a href="' . tep_href_link(basename($PHP_SELF), $parameters . $this->page_name . '=' . (($cur_window_num) * $max_page_links + 1), $request_type) . '" class="pageResults" title=" ' . sprintf(PREVNEXT_TITLE_NEXT_SET_OF_NO_PAGE, $max_page_links) . ' ">...</a>&nbsp;';
 108  
 109  // next button

 110        if (($this->current_page_number < $this->number_of_pages) && ($this->number_of_pages != 1)) $display_links_string .= '&nbsp;<a href="' . tep_href_link(basename($PHP_SELF), $parameters . 'page=' . ($this->current_page_number + 1), $request_type) . '" class="pageResults" title=" ' . PREVNEXT_TITLE_NEXT_PAGE . ' "><u>' . PREVNEXT_BUTTON_NEXT . '</u></a>&nbsp;';
 111  
 112        return $display_links_string;
 113      }
 114  
 115  // display number of total products found

 116      function display_count($text_output) {
 117        $to_num = ($this->number_of_rows_per_page * $this->current_page_number);
 118        if ($to_num > $this->number_of_rows) $to_num = $this->number_of_rows;
 119  
 120        $from_num = ($this->number_of_rows_per_page * ($this->current_page_number - 1));
 121  
 122        if ($to_num == 0) {
 123          $from_num = 0;
 124        } else {
 125          $from_num++;
 126        }
 127  
 128        return sprintf($text_output, $from_num, $to_num, $this->number_of_rows);
 129      }
 130    }
 131  ?>


Generated: Fri Jan 1 13:43:16 2010 Cross-referenced by PHPXref 0.7