web developer's note
Shopping Cart using Codeigniter 1.7.2 Cart Library [part-2]
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
Related posts:
| Print article | This entry was posted by azul on 25, November 2009 at 12:38:49, and is filed under Codeigniter, PHP. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 8 months ago
Really nice series on Codeigniter.
Are you planing on continue with more tutorials on Codeigniter?
Thankyou
about 8 months ago
Thanks for u’re comment.
yes i believe i will have more nice idea on next codeigniter tutorial..
about 8 months ago
In truth, immediately i didn’t understand the essence. But after re-reading all at once became clear.
about 8 months ago
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
about 8 months ago
Check your inbox bro..
about 8 months ago
gan belum masuk tuh… dah gw cek di spam/bulk juga ga ada…
btw emailnya ke narko_freak@yahoo.com kan ?
about 8 months ago
Coba dicek lagi bro..
about 8 months ago
gan… kaskus emoticonnya dah ada update tuh di wordpress.org
salah satunya bug fixing masalah syntaxhighlighter plugin..
about 7 months ago
mantep bro, uda bener tuh.
karakter dari kode jquery ga dianggap lagi sbg smiley apanya yg diperbaiki?
about 7 months ago
syntaxhighlighter kebetulan pembuka dan penutupnya menggunakan tag pre, semua content di dalam tag pre ngga ane ubah ke smiley.. gitu gan…
about 6 months ago
Kerenzz brooo
about 6 months ago
makasi bro komennya, klo ada proyekan share ya
about 6 months ago
about 6 months ago
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?
about 6 months ago
Thanks for your comment and suggestion about membership on shopping cart, i will write more about this when i have a plenty of time
about 6 months ago
Thanks tutorialnya mas..btw bisa kirim ke email saya ga mas codingnya yg tutorial satu dan duanya. Thq Sebelumnya
about 6 months ago
Ane uda update tutorial nya mas dikasi link donlod source codenya
Ditunggu feedbacknya lagi ya
about 5 months ago
gan ane mau juga donk codingnya dikirim ke email ane…
ane gy coba code igniter yang delete row pake anchor jquery tapi gagal mulu gan…help gan…
about 5 months ago
gan donlod aja langsung di postingannya ada, cuman bbrp KB lah
about 1 month ago
Kak, CodeIgniter itu cuma buat kerangkanya saja yah? file css didepan disimpen dimana kak?
about 1 month ago
ente uda ngerti klo codeigniter tu PHP framework?
File CSS tsb diinclude ke View yg bakal dipanggil ama Cart Controller. Cobain aja donlod codenya gan
about 1 month ago
hoo… oke deh kak tar saya coba lagi
kalau mau nambahin halaman admin gimana caranya kak? buat update barangnya gituh 
about 1 month ago
ya tinggal bikin model cart, products dsb. Trus dibikin juga model/controller untuk autentikasinya atau gunakan library autentikasi yang uda ada