CodeIgniter Forums

Full Version: it is not paginating me, humm, dunno why :(
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]number1chump[/eluser]
Hello


well the User guide, which I have thoroughly read several times, says pagination is straightforward. Yet, I dont know why I am not getting to make it. I have managed to display all of the 600 rows, but it is all in one page. I also managed to have the links (page 1, page 2 and upon clicking it just reloads again the 600 rows, so it does not separate them say in pages of 20 row display. I have tried all possible combinations, passing the extracting data from the Model to the Controller, and actually I manage to display the 600 rows without calling the View page. Maybe something in there is the key for failure, it seems as though View did not matter.

Here are the files

MODEL:

Code:
<?php

class Pilla_datos extends Model {
    
    
    
function Pilla_datos()
    {
        // Call the Model constructor
        parent::Model();
    }

        function damedatos ()
        
        {
            
        $this->load->database();  // LOADS THE CLASS DATABASE FROM THE CORE LIBRARY
    
    $query = $this->db->query('SELECT activity_provider, email FROM activity');
        
        foreach ($query->result() as $row)
        {
        echo $row->activity_provider;echo '<br>';
        echo $row->email;
        }

        echo 'Total Results: ' . $query ->num_rows();
    
          
        }

    
}              
                
     ?&gt;

CONTROLLER:

Code:
&lt;?php


class Datos_controller extends Controller {


        function Pilla_datos () {
  
    
  $this->load->model('Pilla_datos');  // IT LOADS THE MODEL
    
  $data =  $this->Pilla_datos->damedatos(); // IT GETS THE DATA FROM THE MODEL CALLING //THE FUNCTION INSIDE THE MODEL
   $this->load->library('pagination');//LOADS PAGINATION CLASS
   $config['base_url'] = 'http://localhost/site1/index.php/datos_controller/pilla_datos/';
   $config['total_rows'] = '200';
   $config['per_page'] = '20';

   $this->pagination->initialize($config);
   echo $this->pagination->create_links();//IT DOES ADD THE LINKS BUT RELOADS THE 600 ROWS
   $this->load->view('muestra_datos, $data');  // LOADS THE VIEW AND PASSES IT     //DYNAMICALLY THIS //DATA BUT ACTUALLY EVEN WITHOUT CALLING THE VIEW, THE ROWS ARE //DISPLAYED, THIS IS
//BECAUSE THEY ARE ALREADY ECHOED AT THE MODEL
    
                }  
            
    }

?&gt;

El Forum

[eluser]Atharva[/eluser]
Why you are echoing the data in model??? Forget about pagination, you need to get your MVC concept corrected.

El Forum

[eluser]number1chump[/eluser]
because even though I may load the model from the controller, then it says undefined variable $query or $data, I know very well the concepts of MVC but echoing them in the Model would at least display to me the rows, and I would try to paginate them and once I solved that, then I would try to actually get it all loaded from the Controller and echoed it at the View.

In fact, that echoing at the Model is part of an Example of the User Guide

El Forum

[eluser]Josh Holloway[/eluser]
Firstly, I'd stop what you're doing and setup the following:

a model:
Code:
class Posts_model extends Model {

function Posts_model()
{
  parent::Model();
}

function get_posts($limit = NULL, $offset = NULL)
{
  $this->db->limit($limit, $offset);
  return $this->db->get('posts');
}

function count_posts()
{
  return $this->db->count_all_results('posts');
}
}

a controller:
Code:
class Posts extends Controller {

function Posts()
{
  parent::Controller();
}

function all_posts()
{
   $this->load->model('posts_model');
   $this->load->library('pagination');

   $per_page = 5;
   $total = $this->Posts_model->count_posts();
   $url = site_url('posts/all_posts');

   $config['base_url'] = $url;
   $config['total_rows'] = $total;
   $config['per_page'] = $per_page;
   $config['uri_segment'] = '3';

   $data['posts'] = $this->Posts_model->get_posts($per_page, $this->uri->segment(3));

   $this->pagination->initialize($config);
   $this->load->view('posts/all_posts', $data);
}
}

a view:
Code:
foreach ($posts->results() as $post):
    echo $post->title, '<br />';
    echo $post->content;
endforeach;

&lt;?php echo $this->pagination->create_links(); ?&gt;

I'll even give you the SQL script to get this working Smile
Code:
CREATE TABLE IF NOT EXISTS `posts` (
  `post_id` int(11) NOT NULL auto_increment,
  `title` varchar(100) NOT NULL,
  `content` text NOT NULL,
  PRIMARY KEY  (`post_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Right, once done, add a few records to the table posts and see if pagination is working.

If so, you can start to substitute bits out to get it working as required.

El Forum

[eluser]cideveloper[/eluser]
josh_holloway is correct. Pagination seems to be a tough thing for people to figure out. You need to follow and example first and then write your own code.

Things to remember

1) $config['total_rows'] is the total rows of results from your database.
2) you need to have the offset and limit set when you are pulling results from the database.
3) Do not echo anything from your model.
Quote:In fact, that echoing at the Model is part of an Example of the User Guide
. Not in the pagination class user guide page or the model page.
4) be very carefull about the offset and limit. It is counter inuitive as to how you write it when you are writing out the SQL as opposed to Active Record.

Active Record:
Code:
$this->db->limit($limit, $offset);
  return $this->db->get('posts');

SQL:

Code:
strSQL = "select * from posts limit " . $offset . ", " . $limit . ";"

El Forum

[eluser]number1chump[/eluser]
Thank you a lot to both of you.

I am going to reflect word by word on the code.

Yes, as per $config[‘total_rows’] I know what it was because it is explained in the user guide section of pagination class. Actually in my example that was one issue, I first hard-coded the real number i had, putting 659 (even though there shows 200) I did try to make it dynamic by retrieving the

Code:
echo 'Total Results: ' . $query ->num_rows();

but it would tell me "undefined variable" because I was not passing it properly so I left it hardcoded at least to see if I could manage the pagination.

The code that Josh has written is more elaborate than what the example of the userguide shows, so I will need a couple of hours maybe to fully understand it since I have been with codeigniter for 3 days so far.

As per Active Records I am not very fond of it because it would force me to unlearn, unset, unlink the SQL that I know, so I prefer to not use Active Records for building SQL queries, although I will use it for pagination and other functions.

thank you very very much

Alvaro

El Forum

[eluser]Josh Holloway[/eluser]
Alvaro -> any issues just post them here Smile

I find active record very very good - simplifys a lot.

I will try to expand more on my post above later on to simply it and provide you with a bit more explanation of each section.

The best way to get total rows is as I've done above:
Code:
$this->db->count_all_results('posts');

El Forum

[eluser]number1chump[/eluser]
thank you a lot. I really love Code Igniter because a lot of work has been put on to make it a very clear and clean structure, yet I ll need my time to be able to use it to the fullest. I m looking forward to building my first web on MVC since my last procedural web had pages with around 1500 lines of code where jquery and css and sql and php and ajax was all on the same pages, so separating things will be a bless.

El Forum

[eluser]Josh Holloway[/eluser]
Might I suggest you look at using HMVC https://bitbucket.org/wiredesignz/codeig.../wiki/Home

takes some getting used to but is so much easier to work with

El Forum

[eluser]number1chump[/eluser]
Been looking now into your example with detail. Nice and insightful to me.

I can clearly see how you obtain from the controller the total number of rows having calculated that in the Models, by calling the model AND the function in the Model which makes that calculation which is basically a core function from Active Records.

You also use Active Records to set the Limit and Offset, although you dont hardcode the numbers but set them as variables.
Then when configuring the parameters for the pagination you are pointing to the controller page AND the function of the controller which contains the pagination functions.

Then to obtain the data you call the Model and the function within that Model and also set that the pagination link will be on the third segment of the URI.

Then once you have setup the parameters of the pagination you actually call the pagination class and finally haul all that data to the View passing it as the second parameter.

Then the view echoes it.

My stuff could not work because simply the echoing was being done before the pagination, it was made in the Model, although I did (and so I indicated it at the beginning of my Posting) try all combinations and that included echoing it in the Controller (which again should not be done because that is what the View is for).

Like I say, yes some functions of Active Records are handy, but I prefer to stick to native SQL particularly when it comes to complex joins. I am reluctant to replace also the most simple this

Code:
select * FROM table_name

by this: $this->db->get('table_name');
Pages: 1 2