[eluser]SitesByJoe[/eluser]
Controller Code:
Code:
class Shop extends Controller {
function __construct()
{
parent::Controller();
$this->load->library('cart');
}
function index()
{
// show a couple products
$products = $this->db->get('products');
foreach ($products->result() as $product)
{
echo '<h3>' . anchor('shop/detail/' . $product->product_id, $product->product_name) . '</h3>';
echo '<p><b>' . $product->product_price . '</b></p>';
echo '<p>' . $product->product_short_desc . '</p>';
echo '<hr>';
}
}
function detail()
{
// show the product and an "add to cart" link
$product_id = $this->uri->segment(3);
$product = $this->db->where('product_id', $product_id)->get('products')->row();
echo '<h1>' . $product->product_name . '</h1>';
echo '<p>' . $product->product_long_description . '</p>';
echo '<p><b>' . $product->product_price . '</b></p>';
echo form_open('shop/add_to_cart');
echo form_hidden('product_id',$product->product_id);
echo form_hidden('product_name', $product->product_name);
echo form_hidden('product_price', $product->product_price);
echo form_input('qty', '1');
echo form_submit('', 'Add to Cart');
echo form_close();
}
function add_to_cart()
{
// add the selected product to the cart
$data = array(
'id' => $this->input->post('product_id'),
'qty' => $this->input->post('qty'),
'price' => $this->input->post('product_price'),
'name' => $this->input->post('product_name'),
'options' => array()
);
$this->cart->insert($data);
redirect('shop/show_cart');
}
function update_cart()
{
//print_r($this->input->post('1'));
for ($i = 1; $i <= $this->cart->total_items(); $i++)
{
$item = $this->input->post($i);
$data = array(
'rowid' => $item['rowid'],
'qty' => $item['qty']
);
$this->cart->update($data);
}
redirect('shop/show_cart');
}
function show_cart()
{
//$this->load->view('public/shop/cart');
?>
<?php echo form_open('shop/update_cart'); ?>
<table cellpadding="6" cellspacing="1" style="width:100%" border="0">
<p><?php echo anchor('shop', 'Continue Shopping'); ?></p>
<tr>
<th>QTY</th>
<th>Item Description</th>
<th style="text-align:right">Item Price</th>
<th style="text-align:right">Sub-Total</th>
</tr>
<?php $i = 1; ?>
<?php foreach($this->cart->contents() as $items): ?>
<?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>
<tr>
<td><?php echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?></td>
<td>
<?php echo $items['name']; ?>
<?php if ($this->cart->has_options($items['rowid']) == TRUE): ?>
<p>
<?php foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>
<strong><?php echo $option_name; ?>:</strong> <?php echo $option_value; ?><br />
<?php endforeach; ?>
</p>
<?php endif; ?>
</td>
<td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
<td style="text-align:right">$<?php echo $this->cart->format_number($items['subtotal']); ?></td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
<tr>
<td colspan="2"> </td>
<td class="right"><strong>Total</strong></td>
<td class="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
</table>
<p><?php echo form_submit('', 'Update your Cart'); ?></p>
<?php
}
function clear_cart()
{
$this->cart->destroy();
redirect('shop');
}
}