blogNuAzul
give great support achieve great satisfaction
give great support achieve great satisfaction
After you succed create product listing page on the previous post now this is real action now, Next step we create cart controller, this controller contain important method which will provide some action in shopping cart. Those methode are add(), view_cart(), update(), delete(), etc. Let’s take a look to this code.
<?php
class cart extends Controller{
//
function cart(){
parent::Controller();
$this->load->library('cart');
}
//
function index(){
$this->view_cart();
}
function add(){
$pid = $this->input->post('product_id');
//
$q = $this->db->get_where('product',array('product_id'=>$pid),1);
if($q->num_rows() > 0){
$item = $q->row();
//
$data = array('id' => $item->product_id,
'qty' => 1,
'price' => $item->product_price,
'name' => $item->product_sku."".$item->product_name
);
$this->cart->insert($data);
}
redirect('cart/view_cart');
}
////
function view_cart(){
$data['custom_jquery'] = '
$("input[name=\'delete\']").click(function(){
var status = $(this).val();
location.href = "'.site_url('cart/delete').'/" + status;
})';
$data['ptitle'] = 'CoderShop | View Cart';
$this->load->view('view_cart',$data);
}
function update(){
//Get number of items in cart
$count = $this->cart->total_items();
//Get info from POST
$item = $this->input->post('rowid');
$qty = $this->input->post('qty');
//Step through items
for($i=0;$i < $count;$i++)
{
$data = array(
'rowid' => $item[$i],
'qty' => $qty[$i]
);
$this->cart->update($data);
}
redirect('cart/view_cart');
}
function delete()
{
$row_id = $this->uri->segment(3,FALSE);
$data = array('rowid'=>$row_id,
'qty' => 0);
$this->cart->update($data);
redirect('cart/view_cart');
}
}
?>
Of course then create cart view called view_cart
<? $this->load->view('header');?>
<div class="content">
<h3>Shopping Cart</h3>
<p><b>Item Count:</b> <? echo $this->cart->total_items();?></p>
<? echo form_open('cart/update'); ?>
<table class="listbox" cellpadding="6" cellspacing="0" style="width:100%" border="0">
<tr>
<th class="tdtop">QTY</th>
<th class="tdtop">Item Description</th>
<th class="tdtop">Delete</th>
<th class="tdtop" style="text-align:right">Item Price</th>
<th class="tdtop" style="text-align:right">Subtotal</th>
</tr>
<? if($this->cart->total_items() > 0): ?>
<? $i = 1; ?>
<? foreach($this->cart->contents() as $items): ?>
<tr>
<td align="right">
<? echo form_hidden('rowid[]', $items['rowid']); ?>
<? echo form_input(array('name' => 'qty[]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?> </td>
<td>
<?=$items['name'];?>
<? if ($this->cart->has_options($items['rowid']) == TRUE): ?>
<p>
<? foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value): ?>
<strong><?=$option_name?>:</strong> <?=$option_value?><br />
<? endforeach; ?>
</p>
<? endif; ?>
</td>
<td>
<input type="checkbox" name="delete" value="<? echo $items['rowid'];?>" />
</td>
<td style="text-align:right"><? echo $this->cart->format_number($items['price']); ?></td>
<td style="text-align:right">Rp <? echo $this->cart->format_number($items['subtotal']); ?></td>
</tr>
<? $i++; ?>
<? endforeach; ?>
<tr>
<td colspan="3"> </td>
<td class="right"><b>Total</b></td>
<td class="right"><b>Rp <? echo $this->cart->format_number($this->cart->total()); ?></b></td>
</tr>
<? endif;
if($this->cart->total_items() < 1):
?>
<tr>
<td colspan="4">No item in your cart.</td>
</tr>
<? endif;?>
</table>
<p><?=form_submit('submit', 'Update'); ?> or <?=anchor('products','Continue Shopping');?></p>
<?=form_close();?>
</div>
<?=$this->load->view('footer');?>
Time for checking up your shopping cart, accsess in browser http://[yourapppath]/index.php/cart, if your cart still empty try to buy an item. This is a preview of my shopping cart; 
Ok your simple shopping cart is running now
if you have any suggestion or problem please write comment here :)
GHTime Code(s): b8578 ab557 7392d nc 1b5f0 7366b nc 7d08a nc 9a18e c0050 3cce7
2009/11/27 - 03:04:21
Really nice series on Codeigniter.
Are you planing on continue with more tutorials on Codeigniter?
Thankyou
2009/11/27 - 07:22:12
Thanks for u’re comment.
yes i believe i will have more nice idea on next codeigniter tutorial..
2009/11/28 - 16:28:33
In truth, immediately i didn’t understand the essence. But after re-reading all at once became clear.
2009/12/01 - 14:42:18
gan tolong kirimin source htmlnya postingan ini, di save ke .txt aja gan, trus ma url plugin buat nampilin code phpnya… habis itu kirim ke email ane gan…. mau ane coba2 dulu di localhost..
sorry gan.. ngrepoti
2009/12/02 - 07:40:39
Check your inbox bro..
2009/12/02 - 09:29:18
gan belum masuk tuh… dah gw cek di spam/bulk juga ga ada…
btw emailnya ke nark…@yahoo.com kan ?
2009/12/03 - 06:03:28
Coba dicek lagi bro..
2009/12/04 - 11:34:16
gan… kaskus emoticonnya dah ada update tuh di wordpress.org
salah satunya bug fixing masalah syntaxhighlighter plugin..
2009/12/05 - 01:06:33
mantep bro, uda bener tuh.
karakter dari kode jquery ga dianggap lagi sbg smiley apanya yg diperbaiki?
2009/12/05 - 13:54:22
syntaxhighlighter kebetulan pembuka dan penutupnya menggunakan tag pre, semua content di dalam tag pre ngga ane ubah ke smiley.. gitu gan…
2010/01/12 - 15:06:04
Kerenzz brooo
2010/01/12 - 19:15:05
makasi bro komennya, klo ada proyekan share ya
2010/01/12 - 15:06:53
2010/01/28 - 09:42:42
Hi. Thank you for the nice tutorial.
Can you suggest something or write a tutorial on how to connect member database to this shopping cart? How can we have member users to use shopping cart in a website?
2010/01/29 - 07:35:48
Thanks for your comment and suggestion about membership on shopping cart, i will write more about this when i have a plenty of time
2010/01/29 - 15:33:11
Thanks tutorialnya mas..btw bisa kirim ke email saya ga mas codingnya yg tutorial satu dan duanya. Thq Sebelumnya
2010/02/01 - 20:13:39
Ane uda update tutorial nya mas dikasi link donlod source codenya
Ditunggu feedbacknya lagi ya