Codeigniter shopping cart using session
![]() |
Codeigniter shopping cart using session |
Hi Guys in this Lesson we are going to learn how to create simple shopping cart in codeigniter ..in this lesson you will learn how to add,delete and display cart items..
just follow the below example to learn codeigniter Shopping Cart...
Configuration:
Database and Table::
Database Name: Codenair
Table Name: product
SQL For Table product
Database and Table::
Database Name: Codenair
Table Name: product
SQL For Table product
CREATE TABLE IF NOT EXISTS `product` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL, `price` int(10) NOT NULL, PRIMARY KEY(id) ) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;Insert Some rows in table product using below SQL Commands
INSERT INTO `product` (`id`, `name`, `price`) VALUES (NULL, 'ASUS VivoBook', 500), (NULL, 'HP Power Gamming', 1230), (NULL, 'Nokia 1020', 460), (NULL, 'Sony XA Ultra', 340), (NULL, 'Intel Sky Lake 4.3GHz', 340), (NULL, 'ASUS BM-86 6 Generation', 543);
application/config/autoload.php
$autoload['libraries'] = array('session','cart');
application/config/autoload.php
$autoload['helper'] = array('url');
application/config/autoload.php
$config['base_url'] = 'http://localhost/cart/';
application/config/database.php
$active_group = 'default'; $query_builder = TRUE; $db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'codenair', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE );
Controllers: blog.php
application/controllers/blog.php
copy and paste the below codes to your controllers file..
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Blog extends CI_Controller { public function __construct(){ parent::__construct(); $this->load->library('cart'); $this->load->model('Cart_model'); } public function index(){ $data['result']=$this->Cart_model->cartindex(); $this->load->view('index',$data); } public function add(){ $id=$this->uri->segment('3'); $data=$this->Cart_model->productbyid($id); $product=array( 'id'=>$data[0]['id'], 'qty'=>1, 'name'=>$data[0]['name'], 'price'=>$data[0]['price'] ); $this->cart->insert($product); redirect('blog/index'); } //Removing The Product public function remove(){ $id=$this->uri->segment('3'); $this->cart->remove($id); redirect('blog/index'); } } ?>
Models: cart_model.php
application/models/cart_model.php
copy and paste the below codes to your models file
<?php class Cart_model extends CI_model{ public function __construct(){ parent::__construct(); $this->load->database(); } public function cartindex(){ $query= $this->db->query("SELECT*FROM product"); return $query->result_array(); } //Fetching Product by Their ID public function productbyid($id){ $query=$this->db->query("SELECT*FROM product WHERE id='$id'"); return $query->result_array(); } } ?>
Views: index.php
Below codes for you view files
<h2>Codeigniter Shopping Cart</h2> <table border="1" cellspacing="0" cellpadding="2"> <tr> <th>Products Name:</th> <th>Price</th> <th>Action</th> </tr> <?php foreach($result as $row){?> <tr> <td><?php echo $row['name'];?></td> <td>$<?php echo $row['price'];?></td> <td><a href="<?php echo base_url();?>index.php/blog/add/<?php echo $row['id'];?>">Add To Cart</a></td> </tr> <?php }?> </table> <h2>Your Cart</h2> <?php if(empty($this->cart->contents())){ echo "Your Cart is Empty"; }else{?> <table border="1" cellspacing="0"> <tr> <th>Name</th> <th>Price</th> <th>Quantity</th> <th>Total Price</th> <th>Action</th> </tr> <?php $i=0; foreach($this->cart->contents() as $items){?> <tr> <td><?php echo $items['name'];?></td> <td>$<?php echo $items['price'];?></td> <td><?php echo $items['qty'];?></td> <td>$<?php echo $items['price']*$items['qty'];?></td> <td><a href="<?php echo base_url();?>index.php/blog/remove/<?php echo $items['rowid'];?>">Delete</a></td> </tr> <?php }?> <tr> <td colspan="3" align="right">Total:</td> <td align="right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td> </tr> </table> <?php }?>
That's it guys how to create shopping cart in Codeigniter..
Now run http://localhost/cart/index.php/blog from your browser and enjoy.
Keep Visiting This Blog for more Codes.. Thank You...
No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.