Welcome Guest, Not a member yet? Register   Sign In
Pagination not performing proparly
#1

[eluser]ede196620[/eluser]
i am working on a pagination for my website its first time i am trying to make this work the problem is that the pagination dose not turn pages when the number of the link is clicked. i very new to codeigniter and php and cant figure this out i tout this would be a great place to start to get some advice. Here is my code :

Controller

Code:
<?php

class Result_controller extends CI_Controller{
    
    function getall(){
        
        $this->load->model('result_model');
        $data['query'] = $this->result_model->result_getall();
        // print_r($data['query']); die();


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

        $config['base_url'] = 'http://localhost/Surva/index.php/result_controller/getall';
        $config['total_rows'] = $this->db->get('tblanswers, credentials')->num_rows();
        $config['per_page'] = 1;
        $config['num_links'] = 10;
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        
        $data['records'] = $this->db->get('tblanswers, credentials', $config['per_page'], $this->uri->segment(1, 0))->result_array();
        $data['pagination'] = $this->pagination->create_links();
        $this->load->view('result_view', $data);
                
            
            
            
        }

    
}

?>

model

Code:
<?php

class Result_model extends CI_Model{
    
    function result_getall(){

  $this->db->select('tblanswers.*,credentials.*');
  $this->db->from('tblanswers');
  $this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'LEFT');
  //$this->db->limit($perPage, $page);
  $query = $this->db->get();
  return $query->result();

  
    }
  
}
?>


and my view

Code:
<html>
    <head>
     <title>Results</title>

    </head>
    <body>
        
        <center>
        
     <div>          
         <table border="1">
          <tr>
             <th>Name</th>
             <th>Second Name</th>
             <th>Phone</th>
             <th>Email</th>
             <th>Answer</th>
             <th>Comment</th>
            
         </tr>
          &lt;?php foreach ($query as $row): ?&gt;
         <tr>      
             <td>&lt;?php echo $row->name; ?&gt;</td>
             <td>&lt;?php echo $row->second_name; ?&gt;</td>
             <td>&lt;?php echo $row->phone; ?&gt;</td>
             <td>&lt;?php echo $row->email; ?&gt;</td>
              <td> &lt;?php echo $row->answerA;?&gt;
            &lt;?php echo $row->answerB;?&gt;
            &lt;?php echo $row->answerC;?&gt;</td>
             <td>&lt;?php echo $row->comment;?&gt;<br></td>    
            
         </tr>
        
          &lt;?php endforeach; ?&gt;
        
         </table>  
         &lt;?php if (isset($pagination))
          {
           echo $pagination;
          // echo "<pre>"; var_dump($query);
           }
          ?&gt;      
   </div>
        </center>
    &lt;?php echo anchor('http://localhost/Surva/index.php/home', 'Home'); ?&gt;
    &lt;/body&gt;
    &lt;/html&gt;
#2

[eluser]TheFuzzy0ne[/eluser]
You are not passing your page number into the controller method, and then onto your model, so your model doesn't know what page to get. In fact, it has no concept of pages at the moment. It just returns all results.
#3

[eluser]ede196620[/eluser]
Tnx for posting, can u explain more to me about this i daunt really understand this is all new to me. Maybe u can give me some sort of example that shows how to select pages ?
#4

[eluser]TheFuzzy0ne[/eluser]
Sure, this is untested, and for illustration purposes only, but:

Controller
Code:
class Result_controller extends CI_Controller{
    
    function page($page_num=1){
        
        $results_per_page = 25;
        $this->load->model('result_model');
        $this->load->library('pagination');

        $config['base_url'] = 'http://localhost/Surva/index.php/result_controller/page/';
        $config['total_rows'] = $this->result_model->count_all();
        $config['per_page'] = $results_per_page;
        $config['use_page_numbers'] = TRUE
        $config['num_links'] = 10;
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        
        $data['records'] = $this->result_model->get_page($page_num, results_per_page);
        $data['pagination'] = $this->pagination->create_links();
        $this->load->view('result_view', $data);
    }
}

Model:
Code:
class Result_model extends CI_Model{
    
    function get_page($page_num=1, $results_per_page=25) {
        // Make sure we have a positive page number.
        if ($page_num < 1)
        {
            $page_num = 1;;
        }
        
        $this->db->select('tblanswers.*,credentials.*');
        $this->db->from('tblanswers');
        $this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'LEFT');
        $this->db->limit(($page_num - 1) * $results_per_page, $results_per_page);
        $query = $this->db->get();
        
        return $query->result_array();
    }
    
    function count_all()
    {
        $this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'LEFT');
        return $this->db->count_all('tblanswers);
    }
  
}

You might even find it works, but I suggest you clean it up a little. I just wanted to try and clarify how this works.

Hope this helps.
#5

[eluser]ede196620[/eluser]
Hi tnx for the help i used the code but when i changed the parts it returns empty array for me now do u have any suggestions how to fix this or what is wrong ?
#6

[eluser]TheFuzzy0ne[/eluser]
I've meditated for hours over this, but the force was unable to show me your code. Any chance you could just post it in here for me? Wink

Try enabling the profilers with $this->output->enable_profiler(TRUE), and then you can see exactly what query is being fired at the database, and make sure it's right.
#7

[eluser]ede196620[/eluser]
Sorry i forgot to post the code Smile I fixed the code up with the best of my knowledge and the links work fine the pages are turning but instead of returning 2 records on a page like i want it keeps adding 2 records to every page that is turned. i think its something in the controller but i will post everything in case i am wrong Smile

i think its something to do with this

Code:
$this->db->limit(($page_num - 1) * $results_per_page, $results_per_page);

but i am not sure about it


controller
Code:
&lt;?php

class Result_controller extends CI_Controller{

    function getall($page_num=1){


        $results_per_page = 1;
        $this->load->model('result_model');
        $this->load->library('pagination');





        $config['base_url'] = 'http://localhost/Surva/index.php/result_controller/getall';
        $config['total_rows'] = $this->result_model->count_all();
         $config['per_page'] = $results_per_page;
         $config['use_page_numbers'] = TRUE;
        $config['num_links'] = 10;
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);

        $data['query'] = $this->result_model->get_page($page_num, $results_per_page);
        $data['pagination'] = $this->pagination->create_links();
        $this->load->view('result_view', $data);




        }


}
?&gt;

model

Code:
&lt;?php
class Result_model extends CI_Model{

    function get_page($page_num=1, $results_per_page=25){

         if ($page_num < 1)
        {
            $page_num = 1;;
        }

  $this->db->select('tblanswers.*,credentials.*');
  $this->db->from('tblanswers');
  $this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'LEFT');
  $this->db->limit(($page_num - 0) * $results_per_page, $results_per_page);
  $query = $this->db->get();

        return $query->result();
    }

    function count_all()
    {
        $this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'LEFT');
        return $this->db->count_all('tblanswers');
    }

}

?&gt;


view

Code:
&lt;html&gt;
    &lt;head&gt;
     &lt;title&gt;Results&lt;/title&gt;

    &lt;/head&gt;
    &lt;body&gt;
        
        <center>
         &lt;?php echo anchor('http://localhost/Surva/index.php/home', 'Home'); ?&gt;
     <div>          
         <table border="1">
          <tr>
             <th>Name</th>
             <th>Second Name</th>
             <th>Phone</th>
             <th>Email</th>
             <th>Answer</th>
             <th>Comment</th>
            
         </tr>
          &lt;?php foreach ($query as $row): ?&gt;
         <tr>      
             <td>&lt;?php echo $row->name; ?&gt;</td>
             <td>&lt;?php echo $row->second_name; ?&gt;</td>
             <td>&lt;?php echo $row->phone; ?&gt;</td>
             <td>&lt;?php echo $row->email; ?&gt;</td>
              <td> &lt;?php echo $row->answerA;?&gt;
            &lt;?php echo $row->answerB;?&gt;
            &lt;?php echo $row->answerC;?&gt;</td>
             <td>&lt;?php echo $row->comment;?&gt;<br></td>    
            
         </tr>
        
          &lt;?php endforeach; ?&gt;
        
         </table>  
         &lt;?php if (isset($pagination))
          {
           echo $pagination;
           echo "<pre>"; var_dump($query);
           }
          ?&gt;      
   </div>
        </center>
  
    &lt;/body&gt;
    &lt;/html&gt;
#8

[eluser]TheFuzzy0ne[/eluser]
Sorry, I got the parameters for $this->db->limit() back-to-front.

That should be:
Code:
$this->db->limit($results_per_page, ($page_num - 1) * $results_per_page);
#9

[eluser]ede196620[/eluser]
its no problem tnx it works great Smile codeigniter user guide makes pagination look so simple spent 2 days on this tnx for your help saved me a lot of time Smile
#10

[eluser]Aken[/eluser]
[quote author="ede196620" date="1362744632"]its no problem tnx it works great Smile codeigniter user guide makes pagination look so simple spent 2 days on this tnx for your help saved me a lot of time Smile[/quote]

It is simple. Your problem is unrelated to the Pagination library. All the library does is generate HTML -- it has no impact on how you retrieve records from your DB.




Theme © iAndrew 2016 - Forum software by © MyBB