Welcome Guest, Not a member yet? Register   Sign In
Pagination is returning blank page
#1

[eluser]NateL[/eluser]
Following Day 7 of Jeff Way's CI From Scratch Tutorials, Pagination. I've followed everything almost down exactly, and my page is still showing up blank. What am I doing wrong?

Controller: portfolio.php
Code:
class Portfolio extends Controller
{
    function index()
    {
        $this->load->library('pagination');
        
        $config['base_url'] = 'http://mytester.example.com/portfolio/';
        $config['total_rows'] = $this->db->get('portfolio')->num_rows();
        $config['per_page'] = 5;
        $config['num_links'] = 10;
        
        $this->pagination->initialize($config);
        
        $data['records'] = $this->db->get('portfolio', $config['per_page'], $this->uri->segment(2));
        
        $this->load->view('portfolio_view', $data);
    }
}

View: portfolio_view.php
Code:
<?php
echo $this->pagination->create_links();

Pretty straight forward, right?
#2

[eluser]NateL[/eluser]
OK. tinkin around some more...

It works when I hard code the total_rows:
Code:
$config['total_rows'] = 30;

However, this doesn't work.
Code:
$config['total_rows'] = $this->db->get('portfolio')->num_rows();

mk. sooo how can I dynamically retrieve the number of rows that exist? and why is num_rows() not working for me - even though it clearly works in the video ?
#3

[eluser]NateL[/eluser]
Fast approaching deadline...really need help with this.

Now I've got these settings:

Code:
$config['per_page'] = 5;
        $config['num_links'] = 10;

and my page is showing 7 results....I don't even have 7 results IN the database

edit:

OK - looks like those 7 results are being generated by this array...

Code:
array(1) {
  ["records"]=>
  object(CI_DB_mysql_result)#14 (7) {
    ["conn_id"]=>
    resource(27) of type (mysql link)
    ["result_id"]=>
    resource(30) of type (mysql result)
    ["result_array"]=>
    array(0) {
    }
    ["result_object"]=>
    array(0) {
    }
    ["current_row"]=>
    int(0)
    ["num_rows"]=>
    int(3)
    ["row_data"]=>
    NULL
  }
}

what is going on here?
#4

[eluser]NateL[/eluser]
Well, I finally figured it out after a few hours of trial and error. It aint pretty, but damn it...it works.

Code:
function index()
    {
        $this->load->library('pagination');
        $this->load->library('table');
        $config['base_url'] = 'http://mytester.example.com/portfolio/index';
        $config['total_rows'] = $this->db->get('portfolio')->num_rows();
        $config['per_page'] = 3;
        $config['num_links'] = 5;
        $config['full_tag_open'] = '<div class="pagination">';
        $config['full_tag_close'] = '</div>';
        
        $this->pagination->initialize($config);
        
        $this->db->order_by('id', 'DESC');
        $query = $this->db->get('portfolio', $config['per_page'], $this->uri->segment(3));
        foreach($query->result() as $row)
        {
            $data['records'][] = $row;
        }

        $this->load->view('portfolio_view', $data);
    }

I will work on cleaning all that up and getting it into Models...but at least I'm getting the results I was after Smile
#5

[eluser]Zeeshan Rasool[/eluser]
I think this type of learning is much better ;-) it helps you a lot
#6

[eluser]Jamie Rumbelow[/eluser]
Just a quickie, you can optimise this code and speed it up by quite a big amount by running a SELECT COUNT(*) query rather then pulling the entire table down then counting the rows. It's really easy to do with CodeIgniter's ActiveRecord:

Code:
$config['total_rows'] = $this->db->count_all('portfolio');
#7

[eluser]NateL[/eluser]
[quote author="Jamie Rumbelow" date="1257549340"]Just a quickie, you can optimise this code and speed it up by quite a big amount by running a SELECT COUNT(*) query rather then pulling the entire table down then counting the rows. It's really easy to do with CodeIgniter's ActiveRecord:

Code:
$config['total_rows'] = $this->db->count_all('portfolio');
[/quote]

Thanks Jamie.

Do me a favor and crack the whip on Elliot - Been waiting around on the 4th Code Igniter video on BinaryCake :-P
#8

[eluser]Jamie Rumbelow[/eluser]
Hah, it'll be out very soon, we've both been hectic!

*special* Smile




Theme © iAndrew 2016 - Forum software by © MyBB