Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter Pagination not working correctly?
#1

[eluser]Katsune[/eluser]
Hi,

I'm stucked on pagination, to be exact:
1. All my data is still displayed in one page
2. Whenever I click the second page. it says I have an undefined variable.

Here's my code:

Controller:
Code:
function index()
        {  
         $data['contents'] = $this->content_model->get_content();
            $data['title'] = 'Contents';
            $this->load->view('template/header', $data);  
            $this->load->view('pages/index', $data);
            $this->load->view('template/footer');
            
            $this->load->library('pagination');
            $config['base_url'] = 'http://localhost/AnimeCoffee/index/';
            $config['total_rows'] = 7;
            $config['per_page'] = 2;
            $config['uri_segment'] = 2;
            $this->pagination->initialize($config);
            echo $this->pagination->create_links();
         }

Model:
Code:
public function get_content($ContentID = FALSE)
        {
                if ($ContentID === FALSE)
                {      
                    $this->db->select('ut.Username, ct.Title, ct.ContentID');
                    $this->db->from('username_table AS ut, content_table AS ct');
                    $this->db->where('ut.UserID = ct.UserID');
                    $query = $this->db->get();
                    return $query->result_array();
                }
                $query = $this->db->get_where('content_table', array('ContentID' => $ContentID));
                return $query->row_array();
         }

View:
Code:
<h1>CodeIgniter 2 Tutorial From Header</h1>
    &lt;?php
foreach ($contents as $contents_item):
?&gt;
    <h2>&lt;?php echo $contents_item['Title'] ?&gt;</h2>
    <div id="main">
        &lt;?php echo $contents_item['Username'] ?&gt;
    </div>
    <p><a href="contents/&lt;?php echo $contents_item['ContentID'] ?&gt;">View article</a></p>
&lt;?php endforeach ?&gt;

Maybe someone can shed some light on how to really use the pagination?
#2

[eluser]Tpojka[/eluser]
Try with:
Code:
$config['uri_segment'] = 3;
#3

[eluser]Katsune[/eluser]
[quote author="Tpojka" date="1396228018"]Try with:
Code:
$config['uri_segment'] = 3;
[/quote]

I already tried this, and I did it again just for sure, I'm still having the same problem.
#4

[eluser]Katsune[/eluser]
Anyone who really understand pagination here? I'm still trying to figure this out and still no progress.
#5

[eluser]Tpojka[/eluser]
Here you can check 10 minutes long video and figure out where is mistake.
#6

[eluser]Katsune[/eluser]
[quote author="Tpojka" date="1396271533"]Here you can check 10 minutes long video and figure out where is mistake.[/quote]

I also checked that before, my problem with that is he directly get the 'states' table which is easier, but my sql block selects 2 sql table and join some of it's columns so I need to find a way to use a variable to represent the 2 tables and pass them to the view,
#7

[eluser]Tpojka[/eluser]
It is same thing. You have array of data in controller that is pulled out from model. In other words: you make data array in your model and pass it to the controller. Same thing.
Second is that your controller structure is not similar to his. He loads view after initializing pagination part and also check how he use variable data['query']. That should be your data['contents']. It is on 6:57.
Third one is that you should split logic if you have one row data or have few/more rows data. If you have one or no one row from model you shouldn't use pagination at all.

Your base_url in config array seems that your CI application is in the root of localhost. Is taht correct?
#8

[eluser]InsiteFX[/eluser]
Why do you have index in your base_url()? The index method is the default for any controller if it exists.

controller/method
#9

[eluser]Katsune[/eluser]
[quote author="Tpojka" date="1396302761"]It is same thing. You have array of data in controller that is pulled out from model. In other words: you make data array in your model and pass it to the controller. Same thing.
Second is that your controller structure is not similar to his. He loads view after initializing pagination part and also check how he use variable data['query']. That should be your data['contents']. It is on 6:57.
Third one is that you should split logic if you have one row data or have few/more rows data. If you have one or no one row from model you shouldn't use pagination at all.

Your base_url in config array seems that your CI application is in the root of localhost. Is taht correct?[/quote]

Thanks, I restructured my code the same with his and change my routes. It worked, although I felt that Id just copy his work T_T,. oh well, it least it worked,.
#10

[eluser]Katsune[/eluser]
[quote author="InsiteFX" date="1396357091"]Why do you have index in your base_url()? The index method is the default for any controller if it exists.

controller/method
[/quote]

I created a route to redirect my controller_name/index to index because I'm using the index as my homepage. Now that you noticed it, is it a good practice? I only did it for convenience of testing actually, hehe.




Theme © iAndrew 2016 - Forum software by © MyBB