Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Kohana]Paginacja - Skocz do strony.
Forum PHP.pl > Forum > PHP > Frameworki
dominick
Witam serdecznie, proszę o naprowadzenie o dopisanie do paginacji "skocz do strony" czyli wpsujemy konkretnie jaką stronę chcemy.
kod kontrolera
  1. class Kohana_Pagination {
  2.  
  3. // Merged configuration settings
  4. protected $config = array(
  5. 'current_page' => array('source' => 'query_string', 'key' => 'page'),
  6. 'total_items' => 0,
  7. 'items_per_page' => 10,
  8. 'view' => 'pagination/basic',
  9. 'auto_hide' => TRUE,
  10. 'first_page_in_url' => FALSE,
  11. );
  12.  
  13. // Current page number
  14. protected $current_page;
  15.  
  16. // Total item count
  17. protected $total_items;
  18.  
  19. // How many items to show per page
  20. protected $items_per_page;
  21.  
  22. // Total page count
  23. protected $total_pages;
  24.  
  25. // Item offset for the first item displayed on the current page
  26. protected $current_first_item;
  27.  
  28. // Item offset for the last item displayed on the current page
  29. protected $current_last_item;
  30.  
  31. // Previous page number; FALSE if the current page is the first one
  32. protected $previous_page;
  33.  
  34. // Next page number; FALSE if the current page is the last one
  35. protected $next_page;
  36.  
  37. // First page number; FALSE if the current page is the first one
  38. protected $first_page;
  39.  
  40. // Last page number; FALSE if the current page is the last one
  41. protected $last_page;
  42.  
  43. // Query offset
  44. protected $offset;
  45.  
  46. /**
  47. * Creates a new Pagination object.
  48. *
  49. * @param array configuration
  50. * @return Pagination
  51. */
  52. public static function factory(array $config = array())
  53. {
  54. return new Pagination($config);
  55. }
  56.  
  57. /**
  58. * Creates a new Pagination object.
  59. *
  60. * @param array configuration
  61. * @return void
  62. */
  63. public function __construct(array $config = array())
  64. {
  65. // Overwrite system defaults with application defaults
  66. $this->config = $this->config_group() + $this->config;
  67.  
  68. // Pagination setup
  69. $this->setup($config);
  70. }
  71.  
  72. /**
  73. * Retrieves a pagination config group from the config file. One config group can
  74. * refer to another as its parent, which will be recursively loaded.
  75. *
  76. * @param string pagination config group; "default" if none given
  77. * @return array config settings
  78. */
  79. public function config_group($group = 'default')
  80. {
  81. // Load the pagination config file
  82. $config_file = Kohana::$config->load('pagination');
  83.  
  84. // Initialize the $config array
  85. $config['group'] = (string) $group;
  86.  
  87. // Recursively load requested config groups
  88. while (isset($config['group']) AND isset($config_file->$config['group']))
  89. {
  90. // Temporarily store config group name
  91. $group = $config['group'];
  92. unset($config['group']);
  93.  
  94. // Add config group values, not overwriting existing keys
  95. $config += $config_file->$group;
  96. }
  97.  
  98. // Get rid of possible stray config group names
  99. unset($config['group']);
  100.  
  101. // Return the merged config group settings
  102. return $config;
  103. }
  104.  
  105. /**
  106. * Loads configuration settings into the object and (re)calculates pagination if needed.
  107. * Allows you to update config settings after a Pagination object has been constructed.
  108. *
  109. * @param array configuration
  110. * @return object Pagination
  111. */
  112. public function setup(array $config = array())
  113. {
  114. if (isset($config['group']))
  115. {
  116. // Recursively load requested config groups
  117. $config += $this->config_group($config['group']);
  118. }
  119.  
  120. // Overwrite the current config settings
  121. $this->config = $config + $this->config;
  122.  
  123. // Only (re)calculate pagination when needed
  124. if ($this->current_page === NULL
  125. OR isset($config['current_page'])
  126. OR isset($config['total_items'])
  127. OR isset($config['items_per_page']))
  128. {
  129. // Retrieve the current page number
  130. if ( ! empty($this->config['current_page']['page']))
  131. {
  132. // The current page number has been set manually
  133. $this->current_page = (int) $this->config['current_page']['page'];
  134. }
  135. else
  136. {
  137. switch ($this->config['current_page']['source'])
  138. {
  139. case 'query_string':
  140. $this->current_page = isset($_GET[$this->config['current_page']['key']])
  141. ? (int) $_GET[$this->config['current_page']['key']]
  142. : 1;
  143. break;
  144.  
  145. case 'route':
  146. $this->current_page = (int) Request::current()->param($this->config['current_page']['key'], 1);
  147. break;
  148. }
  149. }
  150.  
  151. // Calculate and clean all pagination variables
  152. $this->total_items = (int) max(0, $this->config['total_items']);
  153. $this->items_per_page = (int) max(1, $this->config['items_per_page']);
  154. $this->total_pages = (int) ceil($this->total_items / $this->items_per_page);
  155. $this->current_page = (int) min(max(1, $this->current_page), max(1, $this->total_pages));
  156. $this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items);
  157. $this->current_last_item = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items);
  158. $this->previous_page = ($this->current_page > 1) ? $this->current_page - 1 : FALSE;
  159. $this->next_page = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE;
  160. $this->first_page = ($this->current_page === 1) ? FALSE : 1;
  161. $this->last_page = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages;
  162. $this->offset = (int) (($this->current_page - 1) * $this->items_per_page);
  163. }
  164.  
  165. // Chainable method
  166. return $this;
  167. }
  168.  
  169. /**
  170. * Generates the full URL for a certain page.
  171. *
  172. * @param integer page number
  173. * @return string page URL
  174. */
  175. public function url($page = 1)
  176. {
  177. // Clean the page number
  178. $page = max(1, (int) $page);
  179.  
  180. // No page number in URLs to first page
  181. if ($page === 1 AND ! $this->config['first_page_in_url'])
  182. {
  183. $page = NULL;
  184. }
  185.  
  186. switch ($this->config['current_page']['source'])
  187. {
  188. case 'query_string':
  189. return URL::site(Request::current()->uri(), FALSE, TRUE, FALSE).URL::query(array($this->config['current_page']['key'] => $page));
  190.  
  191. case 'route':
  192. return URL::site(Request::current()->uri(array($this->config['current_page']['key'] => $page)), FALSE, TRUE, FALSE).URL::query();
  193. }
  194.  
  195. return '#';
  196. }
  197.  
  198. /**
  199. * Checks whether the given page number exists.
  200. *
  201. * @param integer page number
  202. * @return boolean
  203. * @since 3.0.7
  204. */
  205. public function valid_page($page)
  206. {
  207. // Page number has to be a clean integer
  208. if ( ! Validate::digit($page))
  209. return FALSE;
  210.  
  211. return $page > 0 AND $page <= $this->total_pages;
  212. }
  213.  
  214. /**
  215. * Renders the pagination links.
  216. *
  217. * @param mixed string of the view to use, or a Kohana_View object
  218. * @return string pagination output (HTML)
  219. */
  220. public function render($view = NULL)
  221. {
  222. // Automatically hide pagination whenever it is superfluous
  223. if ($this->config['auto_hide'] === TRUE AND $this->total_pages <= 1)
  224. return '';
  225.  
  226. if ($view === NULL)
  227. {
  228. // Use the view from config
  229. $view = $this->config['view'];
  230. }
  231.  
  232. if ( ! $view instanceof Kohana_View)
  233. {
  234. // Load the view file
  235. $view = View::factory($view);
  236. }
  237.  
  238. // Pass on the whole Pagination object
  239. return $view->set(get_object_vars($this))->set('page', $this)->render();
  240. }
  241.  
  242. /**
  243. * Renders the pagination links.
  244. *
  245. * @return string pagination output (HTML)
  246. */
  247. public function __toString()
  248. {
  249. return $this->render();
  250. }
  251.  
  252. /**
  253. * Returns a Pagination property.
  254. *
  255. * @param string URI of the request
  256. * @return mixed Pagination property; NULL if not found
  257. */
  258. public function __get($key)
  259. {
  260. return isset($this->$key) ? $this->$key : NULL;
  261. }
  262.  
  263. /**
  264. * Updates a single config setting, and recalculates pagination if needed.
  265. *
  266. * @param string config key
  267. * @param mixed config value
  268. * @return void
  269. */
  270. public function __set($key, $value)
  271. {
  272. $this->setup(array($key => $value));
  273. }
  274.  
  275. } // End Pagination


widok wrzucę jak ktoś odpisze bo post za długi.
wujek2009
Jeśli to ma być input to bardziej zadanie po stronie html+js - tworzysz okienko do wpisywania i później pobisz przekierowanie na podstronę podając dodatkowo parametr (np. "page=wpisana wartość" (zakładając, że ustawisz paginacje jako "query string")
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.