Welcome Guest, Not a member yet? Register   Sign In
Pagination Problem
#1

[eluser]haris244808[/eluser]
HI

I have a model function here where i select data and set pagination settings:
Code:
function select_files_based_on_user(){

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

  $config['base_url'] = base_url()."home";
  $config['total_rows'] = $this->db->get('files')->num_rows();
  $config['per_page'] = 5; //records to be displayed per page
  $config['num_links'] = 20; //how many page numbers to be shown between previous and next

  $this->pagination->initialize($config);

  $sql = "SELECT users.first_name, users.last_name, files.* FROM users INNER JOIN files ON users.id = files.user_id WHERE user_id = ? LIMIT ? OFFSET ?";
  $files_query = $this->db->query($sql, array($this->session->userdata('id'),
             $config['per_page'],
             $this->uri->segment(3)
             )
          );
  
   return $files_query->result();
}

i use this code in controller to pass data to the view:
Code:
$this->load->model('files_model');
  $data['files_query'] = $this->files_model->select_files_based_on_user();

  $this->load->view('includes/template', $data);

and here is the view:
Code:
<?php
<table
        if(isset($files_query)){
         foreach($files_query as $results){
          echo "<tr>";
          echo "<td>&lt;input type=\"checkbox\" class=\"checkbox\" /&gt;&lt;/td>";
          echo "<td>".$results->case_nr."</td>";
          echo "<td><a >".$results->subject."</a></td>";
          echo "<td>".substr($results->description,-50)."</td>";
          echo "<td>".$results->first_name." ".$results->last_name."</td>";
          echo "<td>".$results->date_created."</td>";
          echo "<td> Came From(with Session)</td>";
          echo "<td>".substr($results->remarks, -30)."</td>";
          echo "<td><a >Accept</a><a >Reject</a></td>";
         }
        }
       else{
        $files_query = array();
       }
       ?&gt;
      </table>
      
      
      &lt;!-- Pagging --&gt;
      <div class="pagging">
       <div class="right">
        &lt;?php echo $this->pagination->create_links(); ?&gt;
       </div>
      </div>
      &lt;!-- End Pagging --&gt;

However the numbers are displayed in this order 2,3 > 1 ... the first page is displayed as the last one (as in the picture i uploaded)
and also when i click the 2nd and 3rd page...it shows that the :404 Page Not Found

Can anyone help to solve this???
#2

[eluser]TheFuzzy0ne[/eluser]
You have a few problems that I can see.

1) Using $this->db->get('files')->num_rows() is insanity. You're returning every row in the database. If you had a million rows in your database, that will consume some serious resources. You would be much better off using $this->db->count_all();
2) You have set up a query bind expecting three parameters, but you are only passing it one.
3) You're counting every row in your files table, but technically, you should only be counting the ones that the user has access to (yes, this does mean you have to perform an almost identical query twice. Once for counting, and once for getting the rows you want to display).

Have you changed the HTML that the pagination library uses? I suspect your problem is there.

Please could you post the code for the entire controller method, and perhaps a snippet of the HTML code being generated by the pagination library? That way we can see what's going on more clearly.
#3

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1363022547"]You have a few problems that I can see.

1) Using $this->db->get('files')->num_rows() is insanity. You're returning every row in the database. If you had a million rows in your database, that will consume some serious resources. You would be much better off using $this->db->count_all();
2) You have set up a query bind expecting three parameters, but you are only passing it one.
3) You're counting every row in your files table, but technically, you should only be counting the ones that the user has access to (yes, this does mean you have to perform an almost identical query twice. Once for counting, and once for getting the rows you want to display).

Have you changed the HTML that the pagination library uses? I suspect your problem is there.

Please could you post the code for the entire controller method, and perhaps a snippet of the HTML code being generated by the pagination library? That way we can see what's going on more clearly.[/quote]


you mean like this?:
Code:
function select_files_based_on_user(){

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

  $this->db->select('users.first_name, users.last_name, files.*');
  $this->db->from('users');
  $this->db->join('files', 'users.id = files.user_id');
  $this->db->where('user_id', $this->session->userdata('id'));

  $config['total_rows'] = $this->db->count_all_results();

  $config['base_url'] = base_url()."home";
  $config['per_page'] = 5; //records to be displayed per page
  $config['num_links'] = 20; //how many page numbers to be shown between previous and next

  $this->pagination->initialize($config);

  $sql = "SELECT users.first_name, users.last_name, files.* FROM users INNER JOIN files ON users.id = files.user_id WHERE user_id = ? LIMIT ? OFFSET ?";
  $files_query = $this->db->query($sql, array($this->session->userdata('id'),
             $config['per_page'],
             $this->uri->segment(3)
             )
          );
  
   return $files_query->result();
}

i actually posted the view (html snippet)
#4

[eluser]TheFuzzy0ne[/eluser]
Isn't that your model method? I'm interested in the controller method. Smile

I'm not interested in the view (since it's posted above), what I want to see if the HTML the pagination library is creating. You'll need to view the page source to find it.
#5

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1363023978"]Isn't that your model method? I'm interested in the controller method. Smile

I'm not interested in the view (since it's posted above), what I want to see if the HTML the pagination library is creating. You'll need to view the page source to find it.[/quote]

this is the controller:
Code:
function index(){
  
  $data['page_title'] = 'Home Page'; //define the page title to be displayed
  $data['content'] = 'home_view'; //assign login_form to $content variable of template

  $this->load->model('files_model');
  $data['files_query'] = $this->files_model->select_files_based_on_user();

  $this->load->view('includes/template', $data); //pass the data to the include/template.php and load it
}

this is the model:
Code:
function select_files_based_on_user(){

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

  $this->db->select('users.first_name, users.last_name, files.*');
  $this->db->from('users');
  $this->db->join('files', 'users.id = files.user_id');
  $this->db->where('user_id', $this->session->userdata('id'));

  $config['total_rows'] = $this->db->count_all_results();

  $config['base_url'] = base_url()."home";
  $config['per_page'] = 2; //records to be displayed per page
  $config['num_links'] = 10; //how many page numbers to be shown between previous and next

  $this->pagination->initialize($config);

  $sql = "SELECT users.first_name, users.last_name, files.* FROM users INNER JOIN files ON users.id = files.user_id WHERE user_id = ? LIMIT ?";
   $files_query = $this->db->query($sql, array($this->session->userdata('id'), $config['per_page']));
  
   return $files_query->result();
}

and the html code is shown in the picture below:
#6

[eluser]Aken[/eluser]
Your CSS is the problem. The pagination output is fine.
#7

[eluser]haris244808[/eluser]
[quote author="Aken" date="1363042448"]Your CSS is the problem. The pagination output is fine.[/quote]

Even if the css its fine...when i click the 2nd page its says: 404 Erorr; Page not found...
#8

[eluser]Aken[/eluser]
Then you don't have your app set up to handle that kind of URL. Probably need a route.
#9

[eluser]haris244808[/eluser]
Ok i changed the way how i paginate:

Here is dhe whole model:
Code:
class Files_pagination_model extends CI_Model
{

function __construct()
{
  parent::__construct();
}

function count_all_files(){

  $this->db->select('users.first_name, users.last_name, files.*');
  $this->db->from('users');
  $this->db->join('files', 'users.id = files.user_id');
  $this->db->where('user_id', $this->session->userdata('id'));

  return $this->db->count_all_results();
}

function pagination($limit, $offset){

  $sql = "SELECT users.first_name, users.last_name, files.* FROM users INNER JOIN files ON users.id = files.user_id WHERE user_id = ? LIMIT ?, ?";
  $pagination_query = $this->db->query($sql, array($this->session->userdata('id'), $limit, $offset));

  if ($pagination_query->num_rows() > 0) {

            foreach ($pagination_query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }

        return false;
}

} //end of files_pagination_model class

Here is the Controller:
Code:
class Home extends CI_Controller
{

function __construct()
{
  parent::__construct();

  $this->load->model('login_model');
  $this->login_model->is_logged_in();

  $this->load->library('pagination');
  //$this->output->enable_profiler(TRUE);
}

function index(){
  
  $data['page_title'] = 'Home Page'; //define the page title to be displayed
  $data['content'] = 'home_view'; //assign login_form to $content variable of template

  $this->load->model('files_model');
  $data['files_query'] = $this->files_model->select_files_based_on_user();

  $this->files_pagination();

  $this->load->view('includes/template', $data); //pass the data to the include/template.php and load it
}

function files_pagination(){

  $config = array();

  $this->load->model('files_pagination_model');
  $config['base_url'] = base_url()."home";
  $config['total_rows'] = $this->files_pagination_model->count_all_files();
  $config['per_page'] = 2; //records to be displayed per page
  $config['num_links'] = 10; //how many page numbers to be shown between previous and next

  $this->pagination->initialize($config);

  $this->files_pagination_model->pagination($config['per_page'], $this->uri->segment(3));

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

  return $data['links'];
}


}//end Site class

and when i echo $links...its says that its undefined (undefined variable:links)
i tried to var_dump($data)... it gets the pagination correct...put it doesnt pass the variable links. Why??

and to make data change when 2nd, 3rd...page is clicked...what i should do exactly?? Because even i get the 3rd segment ($this->uri->segment(3)) from url, it doesnt work :S
#10

[eluser]TheFuzzy0ne[/eluser]
That's because you're not assigning the return value of files_pagination() to a variable, and then passing it into a view.




Theme © iAndrew 2016 - Forum software by © MyBB