PageRenderTime 58ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/classes/split_page_results.php

https://github.com/yama/zencart13x-ja
PHP | 158 lines | 95 code | 34 blank | 29 comment | 46 complexity | cb4d6d5d066d28c969883d10495f150b MD5 | raw file
  1. <?php
  2. /**
  3. * split_page_results Class.
  4. *
  5. * @package classes
  6. * @copyright Copyright 2003-2006 Zen Cart Development Team
  7. * @copyright Portions Copyright 2003 osCommerce
  8. * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
  9. * @version $Id: split_page_results.php 3041 2006-02-15 21:56:45Z wilt $
  10. */
  11. if (!defined('IS_ADMIN_FLAG')) {
  12. die('Illegal Access');
  13. }
  14. /**
  15. * Split Page Result Class
  16. *
  17. * An sql paging class, that allows for sql reslt to be shown over a number of pages using simple navigation system
  18. * Overhaul scheduled for subsequent release
  19. *
  20. * @package classes
  21. */
  22. class splitPageResults extends base {
  23. var $sql_query, $number_of_rows, $current_page_number, $number_of_pages, $number_of_rows_per_page, $page_name;
  24. /* class constructor */
  25. function splitPageResults($query, $max_rows, $count_key = '*', $page_holder = 'page', $debug = false) {
  26. global $db;
  27. $this->sql_query = $query;
  28. $this->page_name = $page_holder;
  29. if ($debug) {
  30. echo 'original_query=' . $query . '<br /><br />';
  31. }
  32. if (isset($_GET[$page_holder])) {
  33. $page = $_GET[$page_holder];
  34. } elseif (isset($_POST[$page_holder])) {
  35. $page = $_POST[$page_holder];
  36. } else {
  37. $page = '';
  38. }
  39. if (empty($page) || !is_numeric($page)) $page = 1;
  40. $this->current_page_number = $page;
  41. $this->number_of_rows_per_page = $max_rows;
  42. $pos_to = strlen($this->sql_query);
  43. $query_lower = strtolower($this->sql_query);
  44. $pos_from = strpos($query_lower, ' from', 0);
  45. $pos_group_by = strpos($query_lower, ' group by', $pos_from);
  46. if (($pos_group_by < $pos_to) && ($pos_group_by != false)) $pos_to = $pos_group_by;
  47. $pos_having = strpos($query_lower, ' having', $pos_from);
  48. if (($pos_having < $pos_to) && ($pos_having != false)) $pos_to = $pos_having;
  49. $pos_order_by = strpos($query_lower, ' order by', $pos_from);
  50. if (($pos_order_by < $pos_to) && ($pos_order_by != false)) $pos_to = $pos_order_by;
  51. if (strpos($query_lower, 'distinct') || strpos($query_lower, 'group by')) {
  52. $count_string = 'distinct ' . zen_db_input($count_key);
  53. } else {
  54. $count_string = zen_db_input($count_key);
  55. }
  56. $count_query = "select count(" . $count_string . ") as total " .
  57. substr($this->sql_query, $pos_from, ($pos_to - $pos_from));
  58. if ($debug) {
  59. echo 'count_query=' . $count_query . '<br /><br />';
  60. }
  61. $count = $db->Execute($count_query);
  62. $this->number_of_rows = $count->fields['total'];
  63. $this->number_of_pages = ceil($this->number_of_rows / $this->number_of_rows_per_page);
  64. if ($this->current_page_number > $this->number_of_pages) {
  65. $this->current_page_number = $this->number_of_pages;
  66. }
  67. $offset = ($this->number_of_rows_per_page * ($this->current_page_number - 1));
  68. // fix offset error on some versions
  69. if ($offset < 0) { $offset = 0; }
  70. $this->sql_query .= " limit " . $offset . ", " . $this->number_of_rows_per_page;
  71. }
  72. /* class functions */
  73. // display split-page-number-links
  74. function display_links($max_page_links, $parameters = '') {
  75. global $request_type;
  76. $display_links_string = '';
  77. $class = '';
  78. if (zen_not_null($parameters) && (substr($parameters, -1) != '&')) $parameters .= '&';
  79. // previous button - not displayed on first page
  80. if ($this->current_page_number > 1) $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>&nbsp;&nbsp;';
  81. // check if number_of_pages > $max_page_links
  82. $cur_window_num = intval($this->current_page_number / $max_page_links);
  83. if ($this->current_page_number % $max_page_links) $cur_window_num++;
  84. $max_window_num = intval($this->number_of_pages / $max_page_links);
  85. if ($this->number_of_pages % $max_page_links) $max_window_num++;
  86. // previous window of pages
  87. 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>';
  88. // page nn button
  89. 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++) {
  90. if ($jump_to_page == $this->current_page_number) {
  91. $display_links_string .= '&nbsp;<strong class="current">' . $jump_to_page . '</strong>&nbsp;';
  92. } else {
  93. $display_links_string .= '&nbsp;<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>&nbsp;';
  94. }
  95. }
  96. // next window of pages
  97. 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>&nbsp;';
  98. // next button
  99. if (($this->current_page_number < $this->number_of_pages) && ($this->number_of_pages != 1)) $display_links_string .= '&nbsp;<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>&nbsp;';
  100. if ($display_links_string == '&nbsp;<strong class="current">1</strong>&nbsp;') {
  101. return '&nbsp;';
  102. } else {
  103. return $display_links_string;
  104. }
  105. }
  106. // display number of total products found
  107. function display_count($text_output) {
  108. $to_num = ($this->number_of_rows_per_page * $this->current_page_number);
  109. if ($to_num > $this->number_of_rows) $to_num = $this->number_of_rows;
  110. $from_num = ($this->number_of_rows_per_page * ($this->current_page_number - 1));
  111. if ($to_num == 0) {
  112. $from_num = 0;
  113. } else {
  114. $from_num++;
  115. }
  116. if ($to_num <= 1) {
  117. // don't show count when 1
  118. return '';
  119. } else {
  120. return sprintf($text_output, $from_num, $to_num, $this->number_of_rows);
  121. }
  122. }
  123. }
  124. ?>