Pagination Class – Codeigniter – Imashups ‐ WordPress Themes …

September 4, 2009 · Leave a Comment
Filed under: CodeIgniter, Featured 

CodeIgniter’s Pagination class is very easy to use. I set up following code.

Create this file Pagination.php in the system/libraries/Pagination.php folder

system/libraries/Pagination.php

001.<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
002./**
003. * CodeIgniter
004. *
005. * An open source application development framework for PHP 4.3.2 or newer
006. *
007. * @package     CodeIgniter
008. * @author      ExpressionEngine Dev Team
009. * @copyright   Copyright (c) 2006, EllisLab, Inc.
011. * @link        http://codeigniter.com
012. * @since       Version 1.0
013. * @filesource
014. */
015.
016.// ------------------------------------------------------------------------
017.
018./**
019. * Pagination Class
020. *
021. * @package     CodeIgniter
022. * @subpackage  Libraries
023. * @category    Pagination
024. * @author      ExpressionEngine Dev Team
026. */
027.class CI_Pagination {
028.
029. var $base_url = ''; // The page we are linking to
030. var $total_rows = ''; // Total number of items (database results)
031. var $per_page = 10; // Max number of items you want shown per page
032. var $num_links =  2; // Number of "digit" links to show before/after the currently viewed page
033. var $cur_page =  0; // The current page being viewed
034. var $first_link = '&lsaquo; Start';
035. var $next_link = '&gt;';
036. var $prev_link = '&lt;';
037. var $last_link = 'End &rsaquo;';
038. var $uri_segment = 3;
039. var $full_tag_open = '';
040. var $full_tag_close = '';
041. var $first_tag_open = '';
042. var $first_tag_close = '&nbsp;';
043. var $last_tag_open = '&nbsp;';
044. var $last_tag_close = '';
045. var $cur_tag_open = '&nbsp;<b>';
046. var $cur_tag_close = '</b>';
047. var $next_tag_open = '&nbsp;';
048. var $next_tag_close = '&nbsp;';
049. var $prev_tag_open = '&nbsp;';
050. var $prev_tag_close = '';
051. var $num_tag_open = '&nbsp;';
052. var $num_tag_close = '';
053. var $page_query_string = FALSE;
054. var $query_string_segment = 'per_page';
055.
056. /**
057. * Constructor
058. *
059. * @access  public
060. * @param   array   initialization parameters
061. */
062. function CI_Pagination($params = array())
063. {
064. if (count($params) > 0)
065. {
066. $this->initialize($params);
067. }
068.
069. log_message('debug', "Pagination Class Initialized");
070. }
071.
072. // --------------------------------------------------------------------
073.
074. /**
075. * Initialize Preferences
076. *
077. * @access  public
078. * @param   array   initialization parameters
079. * @return  void
080. */
081. function initialize($params = array())
082. {
083. if (count($params) > 0)
084. {
085. foreach ($params as $key => $val)
086. {
087. if (isset($this->$key))
088. {
089. $this->$key = $val;
090. }
091. }
092. }
093. }
094.
095. // --------------------------------------------------------------------
096.
097. /**
098. * Generate the pagination links
099. *
100. * @access  public
101. * @return  string
102. */
103.
104. function create_links()
105. {
106. // If our item count or per-page total is zero there is no need to continue.
107. if ($this->total_rows == 0 OR $this->per_page == 0)
108. {
109. return '';
110. }
111.
112. // Calculate the total number of pages
113. $num_pages = ceil($this->total_rows / $this->per_page);
114.
115. // Is there only one page? Hm... nothing more to do here then.
116. if ($num_pages == 1)
117. {
118. return '';
119. }
120.
121. // Determine the current page number.
122. $CI =& get_instance();
123.
124. if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
125. {
126. if ($CI->input->get($this->query_string_segment) != 0)
127. {
128. $this->cur_page = $CI->input->get($this->query_string_segment);
129.
130. // Prep the current page - no funny business!
131. $this->cur_page = (int) $this->cur_page;
132. }
133. }
134. else
135. {
136. if ($CI->uri->segment($this->uri_segment) != 0)
137. {
138. $this->cur_page = $CI->uri->segment($this->uri_segment);
139.
140. // Prep the current page - no funny business!
141. $this->cur_page = (int) $this->cur_page;
142. }
143. }
144.
145. $this->num_links = (int)$this->num_links;
146.
147. if ($this->num_links < 1)
148. {
149. show_error('Your number of links must be a positive number.');
150. }
151.
152. if ( ! is_numeric($this->cur_page))
153. {
154. $this->cur_page = 0;
155. }
156.
157. // Is the page number beyond the result range?
158. // If so we show the last page
159. if ($this->cur_page > $this->total_rows)
160. {
161. $this->cur_page = ($num_pages - 1) * $this->per_page;
162. }
163.
164. $uri_page_number = $this->cur_page;
165. $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
166.
167. // Calculate the start and end numbers. These determine
168. // which number to start and end the digit links with
169. $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
170. $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
171.
172. // Is pagination being used over GET or POST?  If get, add a per_page query
173. // string. If post, add a trailing slash to the base URL if needed
174. if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
175. {
176. $this->base_url = rtrim($this->base_url).AMP.$this->query_string_segment.'=';
177. }
178. else
179. {
180. $this->base_url = rtrim($this->base_url, '/') .'/';
181. }
182.
183. // And here we go...
184. $output = '';
185.
186. // Render the "First" link
187. if ($this->cur_page > $this->num_links)
188. {
189. $output .= $this->first_tag_open.'<a href="'.$this->base_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
190. }
191.
192. // Render the "previous" link
193. if ($this->cur_page != 1)
194. {
195. $i = $uri_page_number - $this->per_page;
196. if ($i == 0) $i = '';
197. $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
198. }
199.
200. // Write the digit links
201. for ($loop = $start -1; $loop <= $end; $loop++)
202. {
203. $i = ($loop * $this->per_page) - $this->per_page;
204.
205. if ($i >= 0)
206. {
207. if ($this->cur_page == $loop)
208. {
209. $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
210. }
211. else
212. {
213. $n = ($i == 0) ? '' : $i;
214. $output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
215. }
216. }
217. }
218.
219. // Render the "next" link
220. if ($this->cur_page < $num_pages)
221. {
222. $output .= $this->next_tag_open.'<a href="'.$this->base_url.($this->cur_page * $this->per_page).'">'.$this->next_link.'</a>'.$this->next_tag_close;
223. }
224.
225. // Render the "Last" link
226. if (($this->cur_page + $this->num_links) < $num_pages)
227. {
228. $i = (($num_pages * $this->per_page) - $this->per_page);
229. $output .= $this->last_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->last_link.'</a>'.$this->last_tag_close;
230. }
231.
232. // Kill double slashes.  Note: Sometimes we can end up with a double slash
233. // in the penultimate link so we'll kill all double slashes.
234. $output = preg_replace("#([^:])//+#", "\1/", $output);
235.
236. // Add the wrapper HTML if exists
237. $output = $this->full_tag_open.$output.$this->full_tag_close;
238.
239. return $output;
240. }
241.
242. function create_links_left()
243. {
244. // If our item count or per-page total is zero there is no need to continue.
245. if ($this->total_rows == 0 OR $this->per_page == 0)
246. {
247. return '';
248. }
249.
250. // Calculate the total number of pages
251. $num_pages = ceil($this->total_rows / $this->per_page);
252.
253. // Is there only one page? Hm... nothing more to do here then.
254. if ($num_pages == 1)
255. {
256. return '';
257. }
258.
259. // Determine the current page number.
260. $CI =& get_instance();
261.
262. if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
263. {
264. if ($CI->input->get($this->query_string_segment) != 0)
265. {
266. $this->cur_page = $CI->input->get($this->query_string_segment);
267.
268. // Prep the current page - no funny business!
269. $this->cur_page = (int) $this->cur_page;
270. }
271. }
272. else
273. {
274. if ($CI->uri->segment($this->uri_segment) != 0)
275. {
276. $this->cur_page = $CI->uri->segment($this->uri_segment);
277.
278. // Prep the current page - no funny business!
279. $this->cur_page = (int) $this->cur_page;
280. }
281. }
282.
283. $this->num_links = (int)$this->num_links;
284.
285. if ($this->num_links < 1)
286. {
287. show_error('Your number of links must be a positive number.');
288. }
289.
290. if ( ! is_numeric($this->cur_page))
291. {
292. $this->cur_page = 0;
293. }
294.
295. // Is the page number beyond the result range?
296. // If so we show the last page
297. if ($this->cur_page > $this->total_rows)
298. {
299. $this->cur_page = ($num_pages - 1) * $this->per_page;
300. }
301.
302. $uri_page_number = $this->cur_page;
303. $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
304.
305. // Calculate the start and end numbers. These determine
306. // which number to start and end the digit links with
307. $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
308. $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
309.
310. // Is pagination being used over GET or POST?  If get, add a per_page query
311. // string. If post, add a trailing slash to the base URL if needed
312. if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
313. {
314. $this->base_url = rtrim($this->base_url).AMP.$this->query_string_segment.'=';
315. }
316. else
317. {
318. $this->base_url = rtrim($this->base_url, '/') .'/';
319. }
320.
321. // And here we go...
322. $output = '';
323.
324. // Render the "First" link
325. if ($this->cur_page > $this->num_links)
326. {
327. $output .= $this->first_tag_open.'<a href="'.$this->base_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
328. }
329.
330. // Render the "previous" link
331. if ($this->cur_page != 1)
332. {
333. $i = $uri_page_number - $this->per_page;
334. if ($i == 0) $i = '';
335. $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
336. }
337.
338. // Write the digit links
339. for ($loop = $start -1; $loop <= $end; $loop++)
340. {
341. $i = ($loop * $this->per_page) - $this->per_page;
342.
343. if ($i >= 0)
344. {
345. if ($this->cur_page == $loop)
346. {
347. $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
348. }
349. else
350. {
351. $n = ($i == 0) ? '' : $i;
352. $output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
353. }
354. }
355. }
356.
357. // Render the "next" link
358. if ($this->cur_page < $num_pages)
359. {
360. $output .= $this->next_tag_open.'<a href="'.$this->base_url.($this->cur_page * $this->per_page).'">'.$this->next_link.'</a>'.$this->next_tag_close;
361. }
362.
363. // Render the "Last" link
364. if (($this->cur_page + $this->num_links) < $num_pages)
365. {
366. $i = (($num_pages * $this->per_page) - $this->per_page);
367. $output .= $this->last_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->last_link.'</a>'.$this->last_tag_close;
368. }
369.
370. // Kill double slashes.  Note: Sometimes we can end up with a double slash
371. // in the penultimate link so we'll kill all double slashes.
372. $output = preg_replace("#([^:])//+#", "\1/", $output);
373.
374. // Add the wrapper HTML if exists
375. $output = $this->full_tag_open.$output.$this->full_tag_close;
376.
377. return $output;
378. }
379.
380. function create_links_right()
381. {
382. // If our item count or per-page total is zero there is no need to continue.
383. if ($this->total_rows == 0 OR $this->per_page == 0)
384. {
385. return '';
386. }
387.
388. // Calculate the total number of pages
389. $num_pages = ceil($this->total_rows / $this->per_page);
390.
391. // Is there only one page? Hm... nothing more to do here then.
392. if ($num_pages == 1)
393. {
394. return '';
395. }
396.
397. // Determine the current page number.
398. $CI =& get_instance();
399.
400. if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
401. {
402. if ($CI->input->get($this->query_string_segment) != 0)
403. {
404. $this->cur_page = $CI->input->get($this->query_string_segment);
405.
406. // Prep the current page - no funny business!
407. $this->cur_page = (int) $this->cur_page;
408. }
409. }
410. else
411. {
412. if ($CI->uri->segment($this->uri_segment) != 0)
413. {
414. $this->cur_page = $CI->uri->segment($this->uri_segment);
415.
416. // Prep the current page - no funny business!
417. $this->cur_page = (int) $this->cur_page;
418. }
419. }
420.
421. $this->num_links = (int)$this->num_links;
422.
423. if ($this->num_links < 1)
424. {
425. show_error('Your number of links must be a positive number.');
426. }
427.
428. if ( ! is_numeric($this->cur_page))
429. {
430. $this->cur_page = 0;
431. }
432.
433. // Is the page number beyond the result range?
434. // If so we show the last page
435. if ($this->cur_page > $this->total_rows)
436. {
437. $this->cur_page = ($num_pages - 1) * $this->per_page;
438. }
439.
440. $uri_page_number = $this->cur_page;
441. $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
442.
443. // Calculate the start and end numbers. These determine
444. // which number to start and end the digit links with
445. $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
446. $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
447.
448. // Is pagination being used over GET or POST?  If get, add a per_page query
449. // string. If post, add a trailing slash to the base URL if needed
450. if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
451. {
452. $this->base_url = rtrim($this->base_url).AMP.$this->query_string_segment.'=';
453. }
454. else
455. {
456. $this->base_url = rtrim($this->base_url, '/') .'/';
457. }
458.
459. // And here we go...
460. $output = '';
461.
462. // Render the "First" link
463. if ($this->cur_page > $this->num_links)
464. {
465. $output .= $this->first_tag_open.'<a href="'.$this->base_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
466. }
467.
468. // Render the "previous" link
469. if ($this->cur_page != 1)
470. {
471. $i = $uri_page_number - $this->per_page;
472. if ($i == 0) $i = '';
473. $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
474. }
475.
476. // Write the digit links
477. for ($loop = $start -1; $loop <= $end; $loop++)
478. {
479. $i = ($loop * $this->per_page) - $this->per_page;
480.
481. if ($i >= 0)
482. {
483. if ($this->cur_page == $loop)
484. {
485. $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
486. }
487. else
488. {
489. $n = ($i == 0) ? '' : $i;
490. $output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
491. }
492. }
493. }
494.
495. // Render the "next" link
496. if ($this->cur_page < $num_pages)
497. {
498. $output .= $this->next_tag_open.'<a href="'.$this->base_url.($this->cur_page * $this->per_page).'">'.$this->next_link.'</a>'.$this->next_tag_close;
499. }
500.
501. // Render the "Last" link
502. if (($this->cur_page + $this->num_links) < $num_pages)
503. {
504. $i = (($num_pages * $this->per_page) - $this->per_page);
505. $output .= $this->last_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->last_link.'</a>'.$this->last_tag_close;
506. }
507.
508. // Kill double slashes.  Note: Sometimes we can end up with a double slash
509. // in the penultimate link so we'll kill all double slashes.
510. $output = preg_replace("#([^:])//+#", "\1/", $output);
511.
512. // Add the wrapper HTML if exists
513. $output = $this->full_tag_open.$output.$this->full_tag_close;
514.
515. return $output;
516. }
517.
518.}
519.// END Pagination Class
520.
521./* End of file Pagination.php */
522./* Location: ./system/libraries/Pagination.php */

Here’s the controller:

view source




print?

01.function index()
02. {
03. //load model class
04. $this->load->model('mdl_tests', 'tests');
05.
06. //load pagination class
07.
08. $this->load->library('pagination');
09.
10. //The $config array contains your configuration variables.
11.
12. $config['base_url'] = base_url().'tests/index/';
13. $config['total_rows'] = $this->db->count_all('ms_tests');
14.
15. $config['per_page'] = '10';
16. //The number of items you intend to show per page. In the above example, you would be showing 10 items per page.
17.
18. $config['full_tag_open'] = '<p align="center">';
19. $config['full_tag_close'] = '</p>';
20.
21. $config['first_link'] = 'First';
22. $config['last_link'] = 'Last';
23.
24. $config['next_link'] = '&nbsp;&nbsp; NEXT 4 ISSUE >>';
25. $config['prev_link'] = '<< PREVIOUS 4 ISSUE &nbsp; &nbsp;';
26.
27. $config['num_tag_open'] = '<span>&nbsp;<u>';
28. $config['num_tag_close'] = '</u>&nbsp;</span>';
29.
30. $this->pagination->initialize($config);
31.
32. $this->data['pagination'] = $this->pagination->create_links();
33. $this->data['results'] = $this->tests->get_tests_issue($config['per_page'],$this->uri->segment(3));
34.
35. $this->load->view('pagination_sample', $this->data);
36.
37. }

Here’s the model:

1.function get_tests_issue($num, $offset) {
2. $query = $this->db->get('ms_tests', $num, $offset);
3. return $query;
4.}

The view:

01. <div class="clearfix">
02. <?=$pagination?>
03. </div>
04.<?php
05. foreach ($tests as $test) :
06. echo $test['title'];
07. endforeach
08.?>
09.
10. <div class="clearfix">
11. <?=$pagination?>
12. </div>

Codeigniter error, undefined error offset 0 » Guisho's SafeBox

September 3, 2009 · Leave a Comment
Filed under: CodeIgniter, Featured 

When using codeigniter, and this error appears:

A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 0

Filename: libraries/Router.php

Line Number: 190

Just change in config.php
$config[‘uri_protocol’] = “AUTO”;
whith this
$config[‘uri_protocol’] = PATH_INFO”;

Random thoughts on software development: CodeIgniter .htaccess file

September 1, 2009 · 1 Comment
Filed under: CodeIgniter, Featured, PHP 

Note to self. Here is a CodeIgniter .htaccess template file. <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /intranet/ #Removes access to the system folder by users. #Additionally this will allow you to create a System.php

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /intranet/

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#’system’ can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn’t true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file
#Submitted by Michael Radlmaier (mradlmaier)
RewriteCond $1 !^(index\.php|images|robots\.txt|css|js)
RewriteRule ^(.*)$ index.php?/$1 [L]

# If we don’t have mod_rewrite installed, all 404’s
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin

ErrorDocument 404 /index.php
</IfModule>

NETTUTS.com: CodeIgniter From Scratch: Day 5 '” CRUD | Webs Developer

August 31, 2009 · Leave a Comment
Filed under: CodeIgniter, Featured 

NETTUTS.com picks back up their “CodeIgniter From Scratch” series today with day five, a look at the CRUD (create, read, update and delete) features that the framework comes with.

The most requested tutorial for day five of our CodeIgniter screencast series was for an introduction to CRUD operations. We’ll review how to easily create, read, update, and delete records using CI’s active-records class.

You can get the source code here and check out the full screencast in the new post.

« Previous Page