user1504287
user1504287

Reputation:

Ajax Pagination Error in codeigniter

i have the following code which i modiefied from a tutorial

but the problem is that, i cant display the result properly in my view page..

if i display the array in the controller then its working properly, but in the view, its throwing the error..

<?php
class test extends CI_Controller {

    public function index($start = 0) {
            $this->load->model('pages_model');
            $data_page=$this->pages_model->get_pages();


    $this->load->helper('url');
    $this->load->library('pagination');

    $config['base_url']   = base_url().'test/index';
    $config['total_rows'] = count($data_page);
    $config['per_page']   = 5;

    $data['user'] = array();

    for($i=$start; $i<$start+$config['per_page']; $i++) 
    {
      if (isset($data_page[$i])) {
        $data['user'] = $data_page[$i];





      }
    }
    //print_r($data['user']['page_id']); // this line displays 3, since the tupple with id 3 has maximum priority 5

    $this->pagination->initialize($config);
    $data['pagination'] = $this->pagination->create_links();

    if ($this->input->post('ajax')) {
      $this->load->view('test', $data);
    } else {
      $this->load->view('test', $data);
    }
  }



}

?>

now comes the model

<?php
class pages_model extends CI_Model{
    function __construct() {
        parent::__construct();
    }
function get_pages()
    {
        return $this->db->query("SELECT * FROM td_pages ORDER BY page_priority ASC")->result_array();
    }
}
?>

and finally the view

<!DOCTYPE html>
<html>
<header>
  <title>CodeIgniter Ajax Jquery Pagination</title>
  <script src="<?php echo base_url(); ?>assets/front_assets/js/jquery-1.7.1.min.js"></script>
  <script>
  $(function() {
    applyPagination();

    function applyPagination() {
      $("#ajax_paging a").click(function() {
        var url = $(this).attr("href");
        $.ajax({
          type: "POST",
          data: "ajax=1",
          url: url,
          beforeSend: function() {
            $("#content").html("");
          },
          success: function(msg) {
            $("#content").html(msg);
            applyPagination();
          }
        });
        return false;
      });
    }
  });
  </script>
</header>
<body>

<div id="content">
  <div id="data">
    <?php foreach($page_frag as $ut)
    {?>
        <div><?php echo $ut['page_slug'];?></div>
    <?php } ?>
  </data>

  <div id="ajax_paging">
    <?php echo $pagination; ?>
  </div>
</div>
</div>
</body>
</html>

the problem is that the view is displaying the results in wrong way, but if i display in controller, then it shows that the array in the controller is working perfectly...

please help me solving the prob

Upvotes: 1

Views: 1238

Answers (2)

ahmad
ahmad

Reputation: 2729

What you're doing is WRONG

Pagination is meant to off-load processing power/ram from the server not only from the client side.

You're pulling off all your result then paginate them while you should be asking MySQL to paginate it for you & have a second query to give you the number of results to use it.

If you have index.php in your links then you must be using site_url() instead of base_url() & please note that both accepts an argument so you do not need to concatenate it the way you did it would be like this:

site_url('test/index');

codeigniter uses GET method, while in your javascript code you're using POST.

An easier method would be to catch the pagination li > a tags for the pagination & process it inside a container.

Upvotes: 0

Code Prank
Code Prank

Reputation: 4250

You are loading the entire view in your ajax request which also contains the body and head text, Create a seperate view with the following content and load it in your ajax request.

<div id="data">
    <?php foreach($page_frag as $ut)
    {?>
        <div><?php echo $ut['page_slug'];?></div>
    <?php } ?>
</div>

Upvotes: 1

Related Questions