Friday, 5 September 2014

CodeIgniter Pagination



The Controller Code:

  1. public function pagination_demo($page=1){
  2. $this->load->model("messagemodel");
  3. $this->load->library('pagination');
  4. $this->load->library('app/paginationlib');
  5. $this->load->language("message");
  6. try
  7. {
  8. $pagingConfig = $this->paginationlib->initPagination("/codeigniter/pagination-demo",$this->messagemodel->get_count());
  9. $this->data["pagination_helper"] = $this->pagination;
  10. $this->data["messages"] = $this->messagemodel->get_by_range((($page-1) * $pagingConfig['per_page']),$pagingConfig['per_page']);
  11. return $this->view();
  12. }
  13. catch (Exception $err)
  14. {
  15. log_message("error", $err->getMessage());
  16. return show_error($err->getMessage());
  17. }
  18. }

 The Library Code:

  1. /**
  2. *Initialize the pagination rules for cities page
  3. * @return Pagination
  4. */
  5. class Paginationlib {
  6. //put your code here
  7. function __construct() {
  8. $this->ci =& get_instance();
  9. }
  10.  
  11. public function initPagination($base_url,$total_rows){
  12. $config['per_page'] = 1;
  13. $config['uri_segment'] = 3;
  14. $config['base_url'] = base_url().$base_url;
  15. $config['total_rows'] = $total_rows;
  16. $config['use_page_numbers'] = TRUE;
  17. $config['first_tag_open'] = $config['last_tag_open']= $config['next_tag_open']= $config['prev_tag_open'] = $config['num_tag_open'] = '
  18. ';
  19. $config['first_tag_close'] = $config['last_tag_close']= $config['next_tag_close']= $config['prev_tag_close'] = $config['num_tag_close'] = '
  20. ';
  21. $config['cur_tag_open'] = "
  22. ";
  23. $config['cur_tag_close'] = "
  24. ";
  25. $this->ci->pagination->initialize($config);
  26. return $config;
  27. }
  28. }

 The View Code:

  1. <form action="{$base_url}admin/message/delete" method="POST">
  2. <table cellspacing="0" cellpadding="4" border="0" class="table">
  3. <thead>
  4. <tr>
  5. <th> <input type="checkbox" value=""/> </th>
  6. <th> Id </th>
  7. <th> Name </th>
  8. <th> Email </th>
  9. <th> Subject </th>
  10. <th> Message </th>
  11. <th> Sent </th>
  12. </tr>
  13. </thead>
  14. <tbody>
  15.  
  16. {foreach from=$messages item=message name="outer"}
  17. <tr>
  18. <td> <input type="checkbox" name="id[]" value="{$message->getId()}"/> </td>
  19. <td> {$message->getId()} </td>
  20. <td> {$message->getName()} </td>
  21. <td> {$message->getEmail()} </td>
  22. <td> {$message->getSubject()} </td>
  23. <td> {$message->getMessage()|truncate:80} </td>
  24. <td> {$message->getTime()|date_format} </td>
  25. </tr>
  26. {/foreach}
  27.  
  28. </tbody>
  29. </table>
  30. <br />
  31. <div class="clear pagination">
  32. <ul>
  33. {$pagination_helper->create_links()}
  34. </ul>
  35. </div>
  36. </form>

untuk demo sila lawari : http://demo.codesamplez.com/codeigniter/pagination-demo/3

~Banu P Ochatewan ~

0 comments:

Post a Comment