I have a cart table call tbl_cart . I would like to display all the tbl_cart data to the view file (usercart.php) .Below shows the table (tbl_cart). I tried a lot ..i am new to codeigniter please help me to slove it.
id username useremail .... .......... ........... 01 abc a@gmail.com 02 xyz z@gmail.com
i need to display id,username,useremail values to the page usercart.php(view)
cart.php(controller)
public function cartview{
$query = $this->db->query('SELECT username,useremail FROM tbl_cart');
$resultdata['results'] = $query->result_array();
redirect('users/oneusercart'$resultdata)
}
usercart.php(view)
<?php
foreach($results as $result)
{
echo $result['username'],' ',$result['useremail'];
}
?>
public function cartview{
$query = $this->db->query('SELECT username,useremail FROM tbl_cart');
$resultdata['results'] = $query->result_array();
$this->load->view('usercart', $resultdata);
}
You can also read ci user guid for more details about how to load a view
why the redirect in your cart.php controller? call the view right there with
$this->load->view('one/usercart', $resultdata);
the results in your view you get with
foreach($results as $result)
{
echo $result['username']; //etc,...
}
read "generating Query Results" https://codeigniter.com/user_guide/database/results.html and about loading views: https://codeigniter.com/user_guide/general/views.html
Here's how you can do :
You do not need to redirect. You need to replace the line
redirect('users/oneusercart', $resultdata);
with
$this->load->view('usercart', $resultdata);
and in the view your code seems fine and it should work. Let me know if there is any issue or error.
first step... the queries you should put in the model, no in controller. Codeigniter is a MVC framework.
With you example replace:
$this->load->view('$folder/$folder/...more.../file', $resultdata);
instead redirect
redirect('users/oneusercart'$resultdata)
"$folder/$folder/..ect/file" on the structure folder views in your codeigniter, example:
view in folder application/views/users/oneusercart.php
then:
$this->load->view('users/oneusercart', $resultdata);
Other example: view in application/views/oneusercart.php
then:
$this->load->view('oneusercart', $resultdata);
Other example: view in application/views/users/cart/oneusercart.php
then:
$this->load->view('users/cart/oneusercart', $resultdata);
Redirect method redirects the url to the specified url which is another controller or router
you should be using Views to load a particular view File
My way of doing it ..
My controller :
public function cartview{
$this->db->select('username')
->select('useremail')
->from('tbl_cart');
$resultdata['results'] = $this->db->get()->result(); //result returns stdObjects
$this->load->view('myview',$resultdata);
}
My View :
<?php
foreach($results as $result)
{
echo $result->username.' \t '.$result->useremail;
}
?>
first get data from table
public function cartview
{
$query = $this->db->query('SELECT username,useremail FROM tbl_cart');
$resultdata['results'] = $query->result_array();
$this->load->view('usercart', $resultdata);
}
and then display it on usercart view like this
<table>
<thead>
<tr class="bg-success">
<th>ID</th>
<th>Username</th>
<th>Email</th>
</tr>
</thead>
<?php $i=1;foreach($resultdata as
$res){?>
<tr>
<td><?php echo $i;?></td>
<td><?php echo $res->usename;?></td>
<td><?php echo $res->useremail;?></td>
</tbody>
</table>