Reputation: 21
am new to codeigniter and I want to develop todo app.now the problem is get data from database ,anyone findout and tell me what the error is,
get.php
<html>
<head>
<title></title>
</head>
<body>
<?php foreach($data as $row){?>
<tr>
<td><?php echo $row->task; ?></td>
<td><?php echo $row->status; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->date; ?></td>
</tr>
<?php }?>
</body>
</html>
Add_task.php(model)
public function get_user()
{
$this->load->database();
$data=$this->db->get('user');
return $data->result();
}
Welcome.php(controller)
public function get_task($data)
{
$this->load->model('Add_task');
$data=$this->Add_task->get_user();
$this->load->view('get',$data);
}
Am getting error like this
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/get.php
Line Number: 8
Backtrace:
File: C:\wamp\www\CodeIgniter-3.0.4\application\views\get.php Line: 8 Function: _error_handler
File: C:\wamp\www\CodeIgniter-3.0.4\application\controllers\Welcome.php Line: 43 Function: view
File: C:\wamp\www\CodeIgniter-3.0.4\application\controllers\Welcome.php Line: 33 Function: get_task
File: C:\wamp\www\CodeIgniter-3.0.4\index.php Line: 292 Function: require_once
Upvotes: 0
Views: 2097
Reputation: 1
You must pass an associative array that contain your database result.
See you the documentation page for views at "Creating Loops" paragrapher
Welcome.php (controller)
public function get_task($data)
{
$this->load->model('Add_task');
$data['users'] = $this->Add_task->get_user();
$this->load->view('get', $data);
}
And change the loop in your get.php (view) :
<?php foreach($users as $user) { ?>
<tr>
<td><?php echo $user->task; ?></td>
<td><?php echo $user->status; ?></td>
<td><?php echo $user->description; ?></td>
<td><?php echo $user->date; ?></td>
</tr>
<?php } ?>
Upvotes: 0
Reputation: 16117
Just change your controller as:
public function get_task($data) {
$this->load->model('Add_task');
$data["data"] =$this->Add_task->get_user();
$this->load->view('get',$data);
}
You need to pass associative array in load view $data["data"]
Upvotes: 1
Reputation: 1703
Just change
$this->load->view('get',$data);
to
$this->load->view('get', ['data' => $data]);
and all be fine.
If you want to pass a variable into a view, you need to create correspond array index for it (with the same name but without the $
sign). For example, if you want to have $var1
, $my_variable
, $myCustomVariable
, and $data
in the 'get' view, you need to pass it as:
$this->load->view('get', [
'var1' => 'value of $var1',
'my_variable' => 'value of $my_variable',
'myCustomVariable' => 'value of $myCustomVariable',
'data' => $data
]);
Upvotes: 0
Reputation: 7023
Codeigniter read your array by keys, so every variable you want pass to view you must put it as a key in your $data
array like this:
public function get_task()
{
$this->load->model('Add_task');
$rows=$this->Add_task->get_user();
$data['title'] = "page title";
$data['rows'] = $rows;
$this->load->view('get',$data);
}
and in your view, replace $data with $rows:
<?php foreach($rows as $row){?>
<tr>
<td><?php echo $row->task; ?></td>
<td><?php echo $row->status; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->date; ?></td>
</tr>
<?php }?>
Upvotes: 0