CodeIgniter Forums
pagination doesn't work - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 2.x (https://forum.codeigniter.com/forumdisplay.php?fid=18)
+--- Thread: pagination doesn't work (/showthread.php?tid=1560)



pagination doesn't work - chouwech - 03-21-2015

Hi guys.
what part is wrong? here is my code. and here is error

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined index: username

Filename: users/index.php

Line Number: 45

controller:
PHP Code:
public function index(){
        
$this->load->library('pagination');
        
$this->load->model('user_model');

        
$config['base_url'] = base_url().'index.php/admin/index.php/user/page';
        
$config['total_rows'] = $this->user_model->get_users_count();
        
$config['per_page'] = 1;
        
$config['uri_segment'] = 4;
        
$page = ($this->uri->segment(4))?$this->uri->segment(4):0;
        
$config['first_link'] = 'First';
        
$config['last_link'] = 'Last';
        
$this->pagination->initialize($config);
        
        
$data['result'] = $this->user_model->get_all($config['per_page'], $page);
        
$data['link'] = $this->pagination->create_links();

        
$this->template->load('users/index', array('users' => $data));
    } 

Model:
PHP Code:
public function get_all($limit,$start){
        return 
$this->db->get('users'$limit$start)->result_array();
    }

    public function 
get_users_count(){
        return 
$this->db->count_all('users');
    } 

view:
PHP Code:
<?php foreach($users as $key => $value):?>
                            <tr>
                                <td><?=$value['username']; ?></td>
                                <td><?=$value['email'];?></td>
                                <td><?=$value['status'];?></td>
                                <td><?=$value['access'];?></td>
                            </tr>
                            <?php endforeach; ?>

                            <?php
                                
echo $pagination;
                            
?>



RE: pagination doesn't work - RobertSF - 03-21-2015

(03-21-2015, 11:39 AM)chouwech Wrote: controller:

PHP Code:
$data['result'] = $this->user_model->get_all($config['per_page'], $page);
$data['link'] = $this->pagination->create_links();

$this->template->load('users/index', array('users' => $data)); 

view:

PHP Code:
<?php foreach($users as $key => $value):?>
    <tr>
        <td><?=$value['username']; ?></td>
        <td><?=$value['email'];?></td>
        <td><?=$value['status'];?></td>
        <td><?=$value['access'];?></td>
    </tr>
<?php endforeach; ?>

I think this is the problem. When you pass 'users => $data, the array $users contains
Code:
$users['result'] => array() of users
$users['link'] => array() of links

When you say foreach ($users as $key => $value), the first time, $key will contain 'result' and $value will contain the array of users, not a single user.

It may work if instead you just pass the $data array. Then your view will have two arrays, $result and $link. Then you can go foreach ($result as $key => $value).

Well, I hope that's the answer.


RE: pagination doesn't work - chouwech - 03-22-2015

(03-21-2015, 08:34 PM)RobertSF Wrote:
(03-21-2015, 11:39 AM)chouwech Wrote: controller:


PHP Code:
$data['result'] = $this->user_model->get_all($config['per_page'], $page);
$data['link'] = $this->pagination->create_links();

$this->template->load('users/index', array('users' => $data)); 

view:


PHP Code:
<?php foreach($users as $key => $value):?>
    <tr>
        <td><?=$value['username']; ?></td>
        <td><?=$value['email'];?></td>
        <td><?=$value['status'];?></td>
        <td><?=$value['access'];?></td>
    </tr>
<?php endforeach; ?>

I think this is the problem. When you pass 'users => $data, the array $users contains

Code:
$users['result'] => array() of users
$users['link'] => array() of links

When you say foreach ($users as $key => $value), the first time, $key will contain 'result' and $value will contain the array of users, not a single user.

It may work if instead you just pass the $data array. Then your view will have two arrays, $result and $link. Then you can go foreach ($result as $key => $value).

Well, I hope that's the answer.

hi
now this is new error:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: result

Filename: users/index.php

Line Number: 43

P.S. after setting up pagination library in controller, the foreach doesn't have any problem and show data in table.


RE: pagination doesn't work - RobertSF - 03-22-2015

(03-22-2015, 01:24 AM)chouwech Wrote: hi now this is new error:

Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined variable: result
Filename: users/index.php
Line Number: 43

P.S. after setting up pagination library in controller, the foreach doesn't have any problem and show data in table.

Hi. Does your last line mean that you solved your problem? If not, try using a var_dump to see what is in the array that you pass. If you are using
$this->template->load('users/index', array('users' => $data)); in your controller
then put in your view something like this
Code:
var_dump($users);
exit;
<?php foreach($users as $key => $value):?>

Also, $this->template->load is not documented for CI 2 or CI 3. The documentation refers to $this->template->parse. Are you using a third-party library of some kind? I'm just curious because using that is not giving you an error.


RE: pagination doesn't work - chouwech - 03-22-2015

(03-22-2015, 07:49 AM)RobertSF Wrote: Hi. Does your last line mean that you solved your problem? If not, try using a var_dump to see what is in the array that you pass. If you are using
$this->template->load('users/index', array('users' => $data)); in your controller
then put in your view something like this

Code:
var_dump($users);
exit;
<?php foreach($users as $key => $value):?>

Also, $this->template->load is not documented for CI 2 or CI 3. The documentation refers to $this->template->parse. Are you using a third-party library of some kind? I'm just curious because using that is not giving you an error.

no i still have the problem. i'm using a template library. could you remote to my pc please?

the result after using var_dump:
Quote:array (size=2)
'result' =>
array (size=0)
empty
'link' => string '&nbsp;<strong>1</strong>&nbsp;<a href="http://localhost/ci_pmanagement/admin/index.php/user/page/1">2</a>&nbsp;<a href="http://localhost/ci_pmanagement/admin/index.php/user/page/1">&gt;</a>&nbsp;' (length=195)



RE: pagination doesn't work - RobertSF - 03-22-2015

(03-22-2015, 10:22 AM)chouwech Wrote: no i still have the problem. i'm using a template library. could you remote to my pc please?

the result after using var_dump:


Quote:array (size=2)
 'result' =>
   array (size=0)
     empty
 'link' => string '&nbsp;<strong>1</strong>&nbsp;<a href="http://localhost/ci_pmanagement/admin/index.php/user/page/1">2</a>&nbsp;<a href="http://localhost/ci_pmanagement/admin/index.php/user/page/1">&gt;</a>&nbsp;' (length=195)
I can't remote to your PC, but from the var_dump you have, there is nothing in your result array. That means that your get_all() function in your model is not returning any rows.

Code:
$config['per_page'] = 1;
$page = ($this->uri->segment(4))?$this->uri->segment(4):0;

$data['result'] = $this->user_model->get_all($config['per_page'], $page);

public function get_all($limit,$start){
    return $this->db->get('users', $limit, $start)->result_array();
}
Use var_dump to see what's in the $page variable just before you call the get_all() function, and use var_dump to see what's in the $data['result'] array when the get_all() function returns.


RE: pagination doesn't work - chouwech - 03-22-2015

please check that

var_dump($users)

Quote:array (size=2)
'result' =>
array (size=1)
0 =>
array (size=6)
'id' => string '1' (length=1)
'username' => string 'admin' (length=5)
'password' => string 'e10adc3949ba59abbe56e057f20f883e' (length=32)
'email' => string 'admin@localhost.com' (length=19)
'status' => string '1' (length=1)
'access' => string '1' (length=1)
'link' => string '&nbsp;<strong>1</strong>&nbsp;<a href="http://localhost/ci_pmanagement/admin/index.php/user/page/1">2</a>&nbsp;<a href="http://localhost/ci_pmanagement/admin/index.php/user/page/2">3</a>&nbsp;<a href="http://localhost/ci_pmanagement/admin/index.php/user/page/1">&gt;</a>&nbsp;&nbsp;<a href="http://localhost/ci_pmanagement/admin/index.php/user/page/3">Last</a>' (length=360)


var_dump($result):

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: result

Filename: users/index.php

Line Number: 44



RE: pagination doesn't work - phoenixcoded - 03-25-2015

http://codecanyon.net/item/codeigniter-secure-contact-and-newsletter-form/10635431


RE: pagination doesn't work - RobertSF - 03-25-2015

You're right -- the result variable does contain data, so it shouldn't be undefined. What's happening is that variables in your controller are not visible within your view for some reason. I think the problem must be in the third-party template library. Could you try using Codeigniter's $this->load->view('users/index', $users) just to see if you can access the result variable that way?