-
chouwech
Junior Member
-
Posts: 14
Threads: 4
Joined: Feb 2015
Reputation:
0
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; ?>
-
RobertSF
Member
-
Posts: 239
Threads: 7
Joined: Oct 2014
Reputation:
16
(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.
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
-
chouwech
Junior Member
-
Posts: 14
Threads: 4
Joined: Feb 2015
Reputation:
0
(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.
-
RobertSF
Member
-
Posts: 239
Threads: 7
Joined: Oct 2014
Reputation:
16
(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.
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
-
chouwech
Junior Member
-
Posts: 14
Threads: 4
Joined: Feb 2015
Reputation:
0
(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 ' <strong>1</strong> <a href="http://localhost/ci_pmanagement/admin/index.php/user/page/1">2</a> <a href="http://localhost/ci_pmanagement/admin/index.php/user/page/1">></a> ' (length=195)
-
RobertSF
Member
-
Posts: 239
Threads: 7
Joined: Oct 2014
Reputation:
16
(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 ' <strong>1</strong> <a href="http://localhost/ci_pmanagement/admin/index.php/user/page/1">2</a> <a href="http://localhost/ci_pmanagement/admin/index.php/user/page/1">></a> ' (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.
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
-
chouwech
Junior Member
-
Posts: 14
Threads: 4
Joined: Feb 2015
Reputation:
0
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 ' <strong>1</strong> <a href="http://localhost/ci_pmanagement/admin/index.php/user/page/1">2</a> <a href="http://localhost/ci_pmanagement/admin/index.php/user/page/2">3</a> <a href="http://localhost/ci_pmanagement/admin/index.php/user/page/1">></a> <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
-
phoenixcoded
Newbie
-
Posts: 1
Threads: 0
Joined: Mar 2015
Reputation:
0
-
RobertSF
Member
-
Posts: 239
Threads: 7
Joined: Oct 2014
Reputation:
16
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?
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
|