[eluser]sith4life[/eluser]
Hi,
Just to begin I am fairly new to Codeigniter and MVC Programming. That being said I am having an issue and I was wondering if anyone had any ideas on how to fix it.
Background: The application is an online store users can visit to purchase items etc.
Problem: when i add items to the cart an ID is assigned to the current item in the database. however when I try and pull the information from the database the IDs are incorrect and therefore I cannot update quantities or delete them from the cart. (I hope that made sense).
function inside controller:
Code:
/*
* Cart Page for displaying the users cart contents inside the store controller
*
*/
public function cart()
{
$this->load->model('Navigation_model');
$this->load->model('Products_model');
if($this->input->post('update'))
{
$this->Products_model->update_cart($this->input->post('cart_id'),$this->input->post('qty'));
}
$data['carts'] = $this->Products_model->view_cart($this->session->userdata('email'));
$data['links'] = $this->Navigation_model->get_navigation();
$data['current'] = 0;
$this->load->view('header_view');
$this->load->view('cart_view',$data);
$this->load->view('nav_view',$data);
$this->load->view('footer_view');
}
view:
Code:
<?php
$total = 0; // initialize the total so it can be calulated properly later
foreach($carts as $cart):
?>
<div class="prod_info">
<h4><?php echo $cart->name; ?></h4>
<?php
$image = $cart->image;
?>
<img src="<?php echo base_url() . $image; ?>"
height="75" width="75"
alt="<?php echo $cart->name; ?>" />
<?php
echo $cart->description;
?><br />
<?php
echo '$' . $cart->price;
echo form_open('store/cart');
$options = array('name' => 'qty','value' => $cart->qty, 'maxlength' => '3', 'size' => '2');
echo form_input($options);
echo form_hidden('cart_id',$cart->id);
echo form_submit('update','Update Quantity');
echo form_close();
?>
</div>
<?php
$total += ($cart->price * $cart->qty); // add the price to each item to create the sub total
endforeach;
echo '<p>Total: ' . $total . '</p>';
?>
Cart functions inside Products_model:
Code:
function view_cart($email)
{
$query = $this->db->query('SELECT carts.*, products.* FROM carts, products WHERE carts.product_id = products.id');
return $query->result();
}
}
function update_cart($id,$qty,$delete = false)
{
if($delete === false)
{
$query = '
UPDATE carts
SET carts.qty = "'.$qty.'"
WHERE carts.id = "'.$id.'"
';
}
else
{
$query = '
DELETE FROM carts
WHERE carts.id = "'.$id.'"
';
}
$this->db->query($query);
}
Application Flow:
1) user visits /store/products
2) clicks add to cart
3) redirected to /store/cart
4) user tries to update item quantity in the cart (page just refreshes with no changes successfully applied)
I would be happy to elaborate in places if necessary
Any help would be greatly appreciated.
Thanks in advance!