Im quiet new to codeigniter and i am having some trouble setting up my shopping cart. plzz help!!
This is my model:
class shop_model extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function get_all() {
$results = $this->db->get('product')->result();
return $results;
}
function get($id) {
$results = $this->db->get_where('product', array('id' => $id))->row_array();
$result = $results[0];
//result()
return $result;
}
}
my controller:
class shop extends CI_Controller {
public function __construct()
{
parent::__construct();
//$this->load->library(array('form_validation', 'email','pagination'));
$this->load->database();
$this->load->model('shop_model');
}
public function index()
{
$data['products'] = $this->shop_model->get_all();
//print_r($data['products']);
$this->load->view('template/header');
$this->load->view('watch_stop/vprod_list', $data);
$this->load->view('template/footer');
}
public function prod_cart(){
$data['title'] = 'Shopping Cart | Watch Stop';
$this->load->view('template/header');
$this->load->view('watch_stop/vcart', $data);
$this->load->view('template/footer');
}
function add(){
$prod_list = $this->shop_model->get($this->input->post('id'));
$id = $this->input->post('id');
$insert = array(
'id' => $this->input->post('id'),
'qty' => 1,
'price' => $prod_list->price,
'name' => $prod_list->title
);
$this->cart->insert($insert);
print_r($this->cart->contents());
//redirect('shop/prod_cart');
}
}
my cart (view) page:
<div class="row">
<div class="col-md-9">
<?php if ($cart = $this->cart->contents()): ?>
<div id="cart">
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>Item Name</th>
<th>Option</th>
<th>Price</th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach ($cart as $item): ?>
<tr>
<td><?php echo heading($item['brand'].' - '.$item['gender'], 5).br().$item['title'];?></td>
<td><?php ?></td>
<td><?php echo $item['subtotal']; ?></td>
<td class="remove"><?php anchor('shop/remove'.$item['rowid'], '<i class="fa fa-times fa-2x"></i>'); ?> </td>
<tr>
<?php endforeach; ?>
<tr class="total">
<td colspan="2"><strong>Total:</strong></td>
<td>LKR <?php echo $this->cart->total(); ?></td>
</tr>
</tbody>
</table>
</div> <!--table-responsive end-->
</div> <!--cart end-->
<?php endif; ?>
</div> <!--col-md-9 end-->
</div> <!--.row end-->
my products list (view) page (this page is populated via another controller and model and works perfectly):
<div class="row na_top ws-brand-head">
<!--column 1 start-->
<div class="col-md-12">
<?php
echo heading($sub_head, 2);
echo '<hr>';
?>
</div> <!--column 1 end-->
</div> <!--row end-->
<div class="row">
<?php echo $this->session->flashdata('msg'); ?>
<?php foreach($products as $prod_list): ?>
<!--Column 2 start-->
<div class="col-md-3 ws-brand">
<div id="products" class="naprod naprod-mar">
<?php
$na_1 = array(
'src' => base_url().$prod_list['image'],
'height' => '150',
'alt' => $prod_list['title']
);
echo form_open('shop/add');
echo heading($prod_list['stock'], 5);
echo '<div class="na-img">'.img($na_1).'</div>';
echo '<div class="nacont">';
echo anchor(site_url('product/'.$prod_funct.'/'.$prod_list['id']),'<p class="na-btn">VIEW</p>');
?>
<!--<input type="text" id="id" name="id" value="<?php //echo $prod_list['id'] ; ?>" style=" display:none;"/>-->
<input type="hidden" name="id" value="<?php echo $prod_list['id'] ; ?>" />
<button type="submit" name="action" class="na-btn"><i class='fa fa-shopping-cart fa-lg'></i></button>
<?php
echo '</div>';
echo br();
echo heading('LKR '.$prod_list['price'], 5);
echo '<div id="'.$prod_list['brand'].'_'.$prod_list['gender'].'_'.$prod_list['id'].'">'.$prod_list['title'].'</div>';
echo form_close();
?>
</div> <!--naprod naprod-mar end-->
</div> <!--column 2 end-->
<?php endforeach; ?>
</div> <!--row end-->
When i try to load the shop/add method im getting the following errors: 1)Trying to get property of non-object in my controller shop: line 40 and 41. That is the line where i have created the $insert array 'price' and 'name'.
You have to verify if there is catched $prod_list after you try to catch it with your shop model.. (I am not quite sure what your method get is returning but false check should be enough)
function add(){
$prod_list = $this->shop_model->get($this->input->post('id'));
if( ! $prod_list ) {
echo "Product doesn't exists";
exit;
}
$id = $this->input->post('id');
$insert = array(
'id' => $this->input->post('id'),
'qty' => 1,
'price' => $prod_list->price,
'name' => $prod_list->title
);
$this->cart->insert($insert);
print_r($this->cart->contents());
//redirect('shop/prod_cart');
}