<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Superdit.com &#187; cart</title>
	<atom:link href="http://superdit.com/tag/cart/feed/" rel="self" type="application/rss+xml" />
	<link>http://superdit.com</link>
	<description>blogging, design, tech, and web</description>
	<lastBuildDate>Sat, 31 Mar 2012 20:40:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Drag and Drop Shopping Cart Using ExtJS and CodeIgniter Cart</title>
		<link>http://superdit.com/2010/08/21/drag-and-drop-shopping-cart-using-extjs-and-codeigniter-cart/</link>
		<comments>http://superdit.com/2010/08/21/drag-and-drop-shopping-cart-using-extjs-and-codeigniter-cart/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 09:55:23 +0000</pubDate>
		<dc:creator>aditia rahman</dc:creator>
				<category><![CDATA[Codeigniter]]></category>
		<category><![CDATA[Extjs]]></category>
		<category><![CDATA[cart]]></category>
		<category><![CDATA[drag n drop]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://superdit.com/?p=1967</guid>
		<description><![CDATA[This is my other post about simple usage of ExtJS with CodeIgniter, this time I want to create a simple drag and drop shopping cart, I have learn about how to use the cart class in CodeIgniter that I have posted in the previous post, i&#8217;m using MySQL for the storing the product data. I<a href="http://superdit.com/2010/08/21/drag-and-drop-shopping-cart-using-extjs-and-codeigniter-cart/" class="read-more">Continue Reading</a>]]></description>
			<content:encoded><![CDATA[<p>This is my other post about simple usage of ExtJS with CodeIgniter, this time I want to create a simple drag and drop shopping cart, I have learn about how to use the cart class in CodeIgniter that I have posted in the <a title="Understanding CodeIgniter Cart Class" href="http://superdit.com/2010/08/04/understanding-codeigniter-cart-class/" target="_blank">previous post</a>, i&#8217;m using MySQL for the storing the product data. I prepared the mockup it something like this</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-2417" src="http://superdit.com/wp-content/uploads/2010/08/the_mockup.jpg" alt="" width="500" height="257" /><span id="more-1967"></span></p>
<p>Let just get started to the code:</p>
<h4>1. Prepare The Database</h4>
<p>Create table named <em>&#8220;ci_extjs_cart&#8221;</em> or with other name that you desired, next create the table and here the sql code</p>
<pre class="brush: sql; title: ; notranslate">
CREATE TABLE IF NOT EXISTS `products` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(64) DEFAULT NULL,
  `price` varchar(32) DEFAULT NULL,
  `image` varchar(128) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
</pre>
<p>And this are the sample data (I already included the sample image on the code that you can download)</p>
<pre class="brush: sql; title: ; notranslate">
INSERT INTO `products` (`id`, `name`, `price`, `image`) VALUES
(1, 'HP - 20 Inch Widescreen Flat-Panel LCD Monitor', '169', 'hp.jpg'),
(2, 'Gateway - 20 Inch Widescreen Flat-Panel LCD Monitor', '159', 'gateway.jpg'),
(3, 'Apple - 30 Flat-Panel TFT-LCD Monitor', '1799', 'apple.jpg'),
(4, 'Acer - 24 Inch Flat-Panel LED-LCD Monitor', '299', 'acer.jpg'),
(5, 'Asus - 24 Inch Widescreen Flat-Panel LCD Monitor', '249', 'asus.jpg');
</pre>
<h4>2. Setting Up CodeIgniter</h4>
<p>First time to get hand dirty with a framework is to set all the config file (in folder: <em>&#8220;application/config/&#8221;</em>). The four files that we have to configure are:</p>
<p><strong>config.php</strong></p>
<pre class="brush: php; title: ; notranslate">
$config['base_url']	= &quot;http://localhost/ci_extjs_cart/&quot;;
</pre>
<p><strong>database.php</strong></p>
<pre class="brush: php; title: ; notranslate">
$db['default']['hostname'] = &quot;localhost&quot;;
$db['default']['username'] = &quot;root&quot;;
$db['default']['password'] = &quot;admin&quot;;
$db['default']['database'] = &quot;ci_extjs_cart&quot;;
$db['default']['dbdriver'] = &quot;mysql&quot;;
</pre>
<p><strong>routes.php</strong></p>
<pre class="brush: php; title: ; notranslate">
$route['default_controller'] = &quot;product&quot;;
</pre>
<p>The default controller is set to product controller, in this step we have not create the controller yet, but it will be.</p>
<p><strong>autoload.php</strong></p>
<pre class="brush: php; title: ; notranslate">
$autoload['libraries'] = array('database', 'session');
$autoload['helper'] = array('url');
</pre>
<h4>4. Setting Up ExtJS on CodeIgniter</h4>
<p>Like always I always included ExtJS file on separate folder from Codeigniter</p>
<h4>5. Create a Controller and a View</h4>
<p><strong>The Controller</strong>, create file <em>product.php </em>inside the controllers folder, the very usual method on the controller is the contrustion and the index method, in the construction method the codeigniter cart class loaded. <em><br />
</em></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

class Product extends Controller {

    public function  __construct()
    {
        parent::Controller();
        $this-&gt;load-&gt;library('cart');
    }

    public function index()
    {
        $this-&gt;load-&gt;view('product/index');
    }
}
</pre>
<p><strong>The View</strong>, create the view file inside views folder <em>&#8220;index.php&#8221;</em>, the first step to preparing the view is to preparing the HTML layout, include the ExtJS file, and create a basic layout style in CSS.</p>
<pre class="brush: php; title: ; notranslate">
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;&lt;?php echo base_url(); ?&gt;assets/js/ext/resources/css/ext-all.css&quot;/&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;&lt;?php echo base_url(); ?&gt;assets/js/ext/adapter/ext/ext-base.js&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;&lt;?php echo base_url(); ?&gt;assets/js/ext/ext-all.js&quot;&gt;&lt;/script&gt;
    &lt;!-- product data view style --&gt;
    &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;&lt;?php echo base_url(); ?&gt;assets/js/ext/ux/data-view.css&quot;/&gt;

    &lt;script type=&quot;text/javascript&quot;&gt;
    var BASE_URL = '&lt;?php echo site_url(); ?&gt;/';

    Ext.onReady(function() {
    });
    &lt;/script&gt;
    &lt;title&gt;Extjs Image Gallery Using DataView&lt;/title&gt;
    &lt;style type=&quot;text/css&quot;&gt;
        body {
            padding: 20px;
            margin: 0 auto;
        }
        #container {
            padding: 10px;
            background: #e3e3e3;
            border: 1px solid #d3d3d3;
            margin: 0 auto;
            text-align: left;
            width: 630px;
        }
        #top {

        }
        #bottom {
            margin-top: 10px;
        }
    &lt;/style&gt;
    &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div id=&quot;container&quot;&gt;
          &lt;div id=&quot;top&quot;&gt;&lt;/div&gt;
          &lt;div id=&quot;bottom&quot;&gt;&lt;/div&gt;
      &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre>
<p>The Ext.ready function is still empty, inside this function all the ExtJS component will be placed, and I always using BASE_URL value that have value of <em>site_url(); </em>function<em>, </em>to make it easier when accessing a url.</p>
<h4>6. Get Product List</h4>
<p><strong>The Controller</strong>, this is the method the get all the product data, and sending the data as json format.</p>
<pre class="brush: php; title: ; notranslate">
public function ext_get_all()
{
    $query = $this-&gt;db-&gt;get('products');
    $product_arr = array();
    foreach($query-&gt;result() as $key =&gt; $data)
    {
        $product_arr[] = array(
            'id'    =&gt; $data-&gt;id,
            'name'  =&gt; $data-&gt;name,
            'price' =&gt; $data-&gt;price,
            'image' =&gt; $data-&gt;image
        );
    }
    echo json_encode(array('products' =&gt; $product_arr));
}
</pre>
<p><strong>The View</strong>, this is the code for creating the product list in dataview, and a function to make the dataview draggable.</p>
<pre class="brush: jscript; title: ; notranslate">
var proxyProduct = new Ext.data.HttpProxy({
    url: BASE_URL + 'product/ext_get_all', method: 'POST'
});

var strProduct = new Ext.data.JsonStore({
    proxy: proxyProduct,
    root: 'products',
    fields: [
        'id', 'name', 'price', 'image'
    ]
});

strProduct.load();

var tplProduct = new Ext.XTemplate(
    '&lt;tpl for=&quot;.&quot;&gt;',
        '&lt;div class=&quot;thumb-wrap&quot; id=&quot;{name}&quot;&gt;',
        '&lt;div class=&quot;thumb&quot;&gt;&lt;img src=&quot;http://localhost/ci_extjs_cart/assets/img/{image}&quot; title=&quot;{name}&quot;&gt;&lt;/div&gt;',
        '&lt;span class=&quot;name&quot;&gt;{name}&lt;/span&gt;',
        '&lt;div class=&quot;price&quot;&gt;$ {price}&lt;/div&gt;&lt;/div&gt;',
    '&lt;/tpl&gt;',
    '&lt;div class=&quot;x-clear&quot;&gt;&lt;/div&gt;'
);

var dvProduct = new Ext.DataView({
    autoScroll: true, store: strProduct, tpl: tplProduct,
    autoHeight: false, height: 200, multiSelect: false,
    overClass: 'x-view-over', itemSelector: 'div.thumb-wrap',
    emptyText: 'No product to display',
    style: 'border:1px solid #99BBE8;',
    listeners: {
        render: initializeItemDragZone
    }
});

function initializeItemDragZone(v) {
    v.dragZone = new Ext.dd.DragZone(v.getEl(), {
        getDragData: function(e) {
            var sourceEl = e.getTarget(v.itemSelector, 10);
            if (sourceEl) {
                d = sourceEl.cloneNode(true);
                d.id = Ext.id();
                return v.dragData = {
                    sourceEl: sourceEl,
                    repairXY: Ext.fly(sourceEl).getXY(),
                    ddel: d,
                    itemData: v.getRecord(sourceEl).data
                }
            }
        },

        getRepairXY: function() {
            return this.dragData.repairXY;
        }
    });
}

var panelProduct = new Ext.Panel({
    id: 'images-view',
    frame: true,
    width: 620,
    autoHeight: true,
    title: 'Product DataView',
    style: 'margin:0 auto;',
    items: [dvProduct]
});

panelProduct.render('top');
</pre>
<h4>7. Create The Cart (Using EditorGridPanel)</h4>
<p><strong>The Controller</strong>, we will create some basic function to manipulating the cart I try to explain them in the separate way, but still in the same controller</p>
<p><strong>7.1 Get All Cart</strong></p>
<pre class="brush: php; title: ; notranslate">
public function ext_get_cart()
{
    if ($this-&gt;cart-&gt;total_items() != 0)
    {
        foreach($this-&gt;cart-&gt;contents() as $product)
        {
            $cart_arr[] = array(
                'rowid' =&gt; $product['rowid'],
                'id'    =&gt; $product['id'],
                'qty'   =&gt; $product['qty'],
                'name'  =&gt; $product['name'],
                'price' =&gt; $product['price'],
                'subtotal' =&gt; $product['subtotal']
            );
        }
        $cart_arr[] = array(
            'rowid' =&gt; '',
            'id'    =&gt; '',
            'qty'   =&gt; '',
            'name'  =&gt; '',
            'price' =&gt; '&lt;b&gt;Total:&lt;/b&gt;',
            'subtotal' =&gt; '&lt;b&gt;'.$this-&gt;cart-&gt;total().'&lt;/b&gt;'
        );
        echo json_encode(array('cart' =&gt; $cart_arr));
    }
    else
    {
        $cart_arr[] = array();
        echo json_encode(array('cart' =&gt; $cart_arr));
    }
}
</pre>
<p>This function is to getting all the available product in the cart in the session, if the cart is not empty then just loop through the cart function <em>&#8220;$this-&gt;cart-&gt;contents()&#8221; </em>and put this to array. I add another array to showing the total value from the cart, this will be treated different in the ExtJS grid, and send it as json format</p>
<p><strong>7.2 Add a Product to Cart</strong></p>
<pre class="brush: php; title: ; notranslate">
public function ext_add_cart()
{
    if ($_POST['rowid'] == '')
    {
        $data = array(
            'id'    =&gt; $_POST['id'],
            'qty'   =&gt; 1,
            'price' =&gt; $_POST['price'],
            'name'  =&gt; $_POST['name']
        );
        $this-&gt;cart-&gt;insert($data);
    }
    else
    {
        $data = array(
          'rowid'   =&gt; $_POST['rowid'],
          'qty'     =&gt; intval($_POST['qty']) + 1
        );
        $this-&gt;cart-&gt;update($data);
    }
    echo '{success:true, total: &quot;'.$this-&gt;cart-&gt;total().'&quot;}';
}
</pre>
<p>You must be can see in the add cart function there are conditional statement that updating the cart instead insert a new data to cart, well this is to handle if a user adding the product that already available on the cart, so the cart must be updating only the quantity.</p>
<p><strong>7.3 Update a Product Quantity</strong></p>
<pre class="brush: php; title: ; notranslate">
public function ext_update_cart()
{
    $data = array(
      'rowid'   =&gt; $_POST['rowid'],
      'qty'     =&gt; $_POST['qty']
    );
    $this-&gt;cart-&gt;update($data);
    echo '{success:true}';
}
</pre>
<p>This function is to updating a product quantity on the cart, I make the quantity editable on the grid so the user can easy put the number of a product that he desired, and to deleting a product from cart the user have to put zero value on the quantity editable grid.</p>
<p><strong>7.4 Clear Cart</strong></p>
<pre class="brush: php; title: ; notranslate">
public function ext_clear_cart()
{
    $this-&gt;cart-&gt;destroy();
    echo '{success:true}';
}
</pre>
<p>Well nothing really hard in this function, using &#8220;$this-&gt;cart-&gt;destroy()&#8221; all the cart clear.</p>
<p><strong>7.5 The Cart</strong></p>
<pre class="brush: jscript; title: ; notranslate">
var strCart = new Ext.data.JsonStore({
    root: 'cart',
    fields: [
        'rowid', 'id', 'qty', 'name', 'price', 'subtotal'
    ],
    proxy: new Ext.data.HttpProxy({
        url: BASE_URL + 'product/ext_get_cart', method: 'POST'
    })
});
strCart.load();

var cb_select = new Ext.grid.CheckboxSelectionModel();

function showDollar(val) {
    if (val == '' || val == '&lt;b&gt;Total:&lt;/b&gt;') {
        return val;
    }
    else {
        return '$ ' + val;
    }
}

var panelCart = new Ext.grid.EditorGridPanel({
    frame: true, border: true, stripeRows: true,
    store: strCart, loadMask: true, title: 'Your Cart (Drag Here)',
    style: 'margin:0 auto;font-size:13px;',
    height: 220, width: 500, sm: cb_select,
    columns: [{
        header: 'rowid',
        dataIndex: 'rowid',
        hidden: true,
        hideable: false
    }, {
        header: 'id',
        dataIndex: 'id',
        hidden: true,
        hideable: false
    }, {
        header: &quot;Product Name&quot;,
        dataIndex: 'name',
        sortable: true,
        width: 280
    }, {
        header: &quot;Qty&quot;,
        align: 'center',
        width: 40,
        dataIndex: 'qty',
        menuDisabled: true,
        editor: new Ext.form.TextField({
            allowBlank: false,
            vtype: 'alphanum',
            id: 'qtyField'
        })
    }, {
        header: &quot;Price&quot;,
        dataIndex: 'price',
        sortable: true,
        align: 'right',
        renderer: showDollar,
        width: 80
    }, {
        header: &quot;Subtotal&quot;,
        sortable: true,
        align: 'right',
        width: 80,
        renderer: showDollar
    }],
    listeners: {
        'afteredit': function() {
            var sm = panelCart.getSelectionModel().getSelections();
            panelCart.getSelectionModel().clearSelections();
            Ext.Ajax.request({
                method: 'POST',
                url: BASE_URL + 'product/ext_update_cart',
                params: {
                    rowid: sm[0].get('rowid'),
                    qty: Ext.getCmp('qtyField').getValue()
                },
                success: function() {
                    strCart.load();
                }
            });
        }
    },
    buttons: [{
        text: 'Clear Cart',
        handler: function() {
            Ext.Ajax.request({
                url: BASE_URL + 'product/ext_clear_cart',
                method: 'POST',
                success: function() {
                    strCart.load();
                }
            });
        }
    }, {
        text: 'Check Out',
        handler: function() {

        }
    }]
});

panelCart.render('bottom');

var formPanelDropTargetEl =  panelCart.getView().scroller.dom;

var formPanelDropTarget = new Ext.dd.DropTarget(formPanelDropTargetEl, {
    notifyDrop  : function(ddSource, e, data){
        panelCart.getSelectionModel().selectAll();
        var sm = panelCart.getSelectionModel().getSelections();
        panelCart.getSelectionModel().clearSelections();

        data.itemData.rowid = '';
        for (i=0; i&lt;=sm.length-1; i++) {
            if (sm[i].get('id') == data.itemData.id) {
                data.itemData.rowid = sm[i].get('rowid');
                data.itemData.qty = sm[i].get('qty');
                // so can out from loop
                i = sm.length;
            }
        }

        Ext.Ajax.request({
            url: BASE_URL + 'product/ext_add_cart',
            method: 'POST',
            params: {
                'id': data.itemData.id,
                'price': data.itemData.price,
                'name': data.itemData.name,
                'rowid': data.itemData.rowid,
                'qty': data.itemData.qty
            },
            success: function() {
                strCart.load();
            }
        });
        return(true);
    }
});
</pre>
<p>That&#8217;s all for the Cart features, there is <em>CheckboxSelectionModel</em> component, this component is to check whether the product is already on the cart, you can see it used on <em>notifyDrop</em> inside the <em>dropTarget</em> you have to add, the expected result is something like this.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-2407" title="ExtJS &amp; CodeIgniter Cart" src="http://superdit.com/wp-content/uploads/2010/08/localhost_ci_extjs_cart.png" alt="ExtJS &amp; CodeIgniter Cart" width="500" height="375" /></p>
<p style="text-align: center;"><a title="CodeIgniter ExtJS Drag n Drop Cart" href="http://www.box.net/shared/qamveqfxlt" target="_blank">Download Source</a></p>
]]></content:encoded>
			<wfw:commentRss>http://superdit.com/2010/08/21/drag-and-drop-shopping-cart-using-extjs-and-codeigniter-cart/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Understanding CodeIgniter Cart Class</title>
		<link>http://superdit.com/2010/08/04/understanding-codeigniter-cart-class/</link>
		<comments>http://superdit.com/2010/08/04/understanding-codeigniter-cart-class/#comments</comments>
		<pubDate>Wed, 04 Aug 2010 06:21:20 +0000</pubDate>
		<dc:creator>aditia rahman</dc:creator>
				<category><![CDATA[Codeigniter]]></category>
		<category><![CDATA[cart]]></category>

		<guid isPermaLink="false">http://superdit.com/?p=2214</guid>
		<description><![CDATA[Cart is one of the most important feature when creating an online store. In this post I want to have more understanding about Cart Class in CodeIgniter, cause maybe come in handy in the future, you can see how many new online store out there open in business, and in this internet age, online store<a href="http://superdit.com/2010/08/04/understanding-codeigniter-cart-class/" class="read-more">Continue Reading</a>]]></description>
			<content:encoded><![CDATA[<p><img class="size-full wp-image-2248 alignleft" title="CodeIgniter Cart Class" src="http://superdit.com/wp-content/uploads/2010/08/1280895266_shopping_cart-red.jpg" alt="CodeIgniter Cart Class" width="61" height="61" />Cart is one of the most important feature when creating an online store. In this post I want to have more understanding about Cart Class in CodeIgniter, cause maybe come in handy in the future, you can see how many new online store out there open in business, and in this internet age, online store seems the must have for many people who have retail business, in this post I&#8217;m not writing about how to use it in some example, the code I posted in here just copying from the original <a title="Code Igniter Cart Class Documentation" href="http://codeigniter.com/user_guide/libraries/cart.html" target="_blank">cart class documentation</a>, but i&#8217;m included the link to the example usage of this class on the section 4 below.<span id="more-2214"></span></p>
<h4>1. What is Cart Class in CodeIgniter?</h4>
<p>First from the official documentation</p>
<p><em>- The Cart class utilizes CodeIgniter&#8217;s Session Class to save the cart information to a database, so before using the Cart class you must set up a database table as indicated in the Session Documentation , and set the session preferences in your appliction/config/config.php file to utilize a database.</em> -</p>
<p>which mean this class save the cart from the users and save it to database, you have to create this table</p>
<pre class="brush: sql; title: ; notranslate">
CREATE TABLE IF NOT EXISTS  `ci_sessions` (
	session_id varchar(40) DEFAULT '0' NOT NULL,
	ip_address varchar(16) DEFAULT '0' NOT NULL,
	user_agent varchar(50) NOT NULL,
	last_activity int(10) unsigned DEFAULT 0 NOT NULL,
	user_data text NOT NULL,
	PRIMARY KEY (session_id)
);
</pre>
<p>You can change the table name, but it have to be adapted to the config.php</p>
<pre class="brush: php; title: ; notranslate">
$config['sess_cookie_name']		= 'ci_session';
$config['sess_expiration']		= 7200;
$config['sess_encrypt_cookie']	= FALSE;
$config['sess_use_database']	= FALSE;
$config['sess_table_name']		= 'ci_sessions';
$config['sess_match_ip']		= FALSE;
$config['sess_match_useragent']	= TRUE;
$config['sess_time_to_update'] 	= 300;
</pre>
<p>When you add an array data to the cart class, the data would be inserted to ci_sessions table in user_data field. Saving data to cart class is have to be in array format, it can be single or multidimensional array, this code I take from the original documentation</p>
<pre class="brush: php; title: ; notranslate">
// the single array
$data = array(
   'id'      =&gt; 'sku_123ABC',
   'qty'     =&gt; 1,
   'price'   =&gt; 39.95,
   'name'    =&gt; 'T-Shirt',
   'options' =&gt; array('Size' =&gt; 'L', 'Color' =&gt; 'Red')
);

// the multidimensional array
$data = array(
   array(
           'id'      =&gt; 'sku_123ABC',
           'qty'     =&gt; 1,
           'price'   =&gt; 39.95,
           'name'    =&gt; 'T-Shirt',
           'options' =&gt; array('Size' =&gt; 'L', 'Color' =&gt; 'Red')
        ),
   array(
           'id'      =&gt; 'sku_567ZYX',
           'qty'     =&gt; 1,
           'price'   =&gt; 9.95,
           'name'    =&gt; 'Coffee Mug'
        ),
   array(
           'id'      =&gt; 'sku_965QRS',
           'qty'     =&gt; 1,
           'price'   =&gt; 29.95,
           'name'    =&gt; 'Shot Glass'
        )
);

// insert the data
$this-&gt;cart-&gt;insert($data);
</pre>
<p>The required index in the array are <strong>id</strong>, <strong>qty</strong>, <strong>price</strong>, <strong>name</strong> you can add the additional index but you can&#8217;t use that four index and the <strong>options</strong> index is optional, in case you want to save another information of the product for example, T-Shirt with the same product id but different size. The last thing from the array is you can&#8217;t use another words that reserved, they are <strong>rowid</strong> and <strong>subtotal</strong>.</p>
<h4>2. Displaying The Items</h4>
<pre class="brush: php; title: ; notranslate">
&lt;?php echo form_open('path/to/controller/update/function'); ?&gt;

&lt;table cellpadding=&quot;6&quot; cellspacing=&quot;1&quot; style=&quot;width:100%&quot; border=&quot;0&quot;&gt;

&lt;tr&gt;
  &lt;th&gt;QTY&lt;/th&gt;
  &lt;th&gt;Item Description&lt;/th&gt;
  &lt;th style=&quot;text-align:right&quot;&gt;Item Price&lt;/th&gt;
  &lt;th style=&quot;text-align:right&quot;&gt;Sub-Total&lt;/th&gt;
&lt;/tr&gt;

&lt;?php $i = 1; ?&gt;

&lt;?php foreach($this-&gt;cart-&gt;contents() as $items): ?&gt;

	&lt;?php echo form_hidden($i.'[rowid]', $items['rowid']); ?&gt;

	&lt;tr&gt;
	  &lt;td&gt;&lt;?php echo form_input(array('name' =&gt; $i.'[qty]', 'value' =&gt; $items['qty'], 'maxlength' =&gt; '3', 'size' =&gt; '5')); ?&gt;&lt;/td&gt;
	  &lt;td&gt;
		&lt;?php echo $items['name']; ?&gt;

			&lt;?php if ($this-&gt;cart-&gt;has_options($items['rowid']) == TRUE): ?&gt;

				&lt;p&gt;
					&lt;?php foreach ($this-&gt;cart-&gt;product_options($items['rowid']) as $option_name =&gt; $option_value): ?&gt;

						&lt;strong&gt;&lt;?php echo $option_name; ?&gt;:&lt;/strong&gt; &lt;?php echo $option_value; ?&gt;&lt;br /&gt;

					&lt;?php endforeach; ?&gt;
				&lt;/p&gt;

			&lt;?php endif; ?&gt;

	  &lt;/td&gt;
	  &lt;td style=&quot;text-align:right&quot;&gt;&lt;?php echo $this-&gt;cart-&gt;format_number($items['price']); ?&gt;&lt;/td&gt;
	  &lt;td style=&quot;text-align:right&quot;&gt;$&lt;?php echo $this-&gt;cart-&gt;format_number($items['subtotal']); ?&gt;&lt;/td&gt;
	&lt;/tr&gt;

&lt;?php $i++; ?&gt;

&lt;?php endforeach; ?&gt;

&lt;tr&gt;
  &lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;
  &lt;td class=&quot;right&quot;&gt;&lt;strong&gt;Total&lt;/strong&gt;&lt;/td&gt;
  &lt;td class=&quot;right&quot;&gt;$&lt;?php echo $this-&gt;cart-&gt;format_number($this-&gt;cart-&gt;total()); ?&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;/table&gt;

&lt;p&gt;&lt;?php echo form_submit('', 'Update your Cart'); ?&gt;&lt;/p&gt;
</pre>
<p>The code above is to displaying the current cart on the web pages, and theres a hidden field on the form this is used when we want to updating the cart, the difference between insert and update is, insert function is to adding new items to the cart, yet the update function is to updating items on the cart whether the quantity the options or something else, so when you updating the cart you need the rowid to pass it through. Here is the code</p>
<pre class="brush: php; title: ; notranslate">
$data = array(
   'rowid' =&gt; 'b99ccdf16028f015540f341130b6d8ec',
   'qty'   =&gt; 3
);

$this-&gt;cart-&gt;update($data);

// or a multi-dimensional array

$data = array(
   array(
           'rowid'   =&gt; 'b99ccdf16028f015540f341130b6d8ec',
           'qty'     =&gt; 3
        ),
   array(
           'rowid'   =&gt; 'xw82g9q3r495893iajdh473990rikw23',
           'qty'     =&gt; 4
        ),
   array(
           'rowid'   =&gt; 'fh4kdkkkaoe30njgoe92rkdkkobec333',
           'qty'     =&gt; 2
        )
);

$this-&gt;cart-&gt;update($data);
</pre>
<h4>3. The Available Function</h4>
<p><strong>$this-&gt;cart-&gt;insert();</strong><br />
- <em>Permits you to add items to the shopping cart, as outlined above.</em> -</p>
<p><strong>$this-&gt;cart-&gt;update();</strong><br />
- <em>Permits you to update items in the shopping cart, as outlined above.</em> -</p>
<p><strong>$this-&gt;cart-&gt;total();</strong><br />
- <em>Displays the total amount in the cart.</em> -</p>
<p><strong>$this-&gt;cart-&gt;total_items();</strong><br />
- <em>Displays the total number of items in the cart.</em> -</p>
<p><strong>$this-&gt;cart-&gt;contents();</strong><br />
- <em>Returns an array containing everything in the cart.</em> -</p>
<p><strong>$this-&gt;cart-&gt;has_options(rowid);</strong><br />
- <em>Returns TRUE (boolean) if a particular row in the cart contains options.  This function is designed to be used in a loop with <dfn>$this-&gt;cart-&gt;contents()</dfn>, since you must pass the <kbd>rowid</kbd> to this function, as shown in the <dfn>Displaying the Cart</dfn> example above. -<br />
</em></p>
<p><strong>$this-&gt;cart-&gt;options(rowid);</strong><br />
- <em>Returns an array of options for a particular product. This function is designed to be used in a loop with <dfn>$this-&gt;cart-&gt;contents()</dfn>, since you must pass the <kbd>rowid</kbd> to this function, as shown in the <dfn>Displaying the Cart</dfn> example above.</em> -</p>
<p><strong>$this-&gt;cart-&gt;destroy();</strong><br />
- <em>Permits you to destroy the cart.  This function will likely be called when you are finished processing the customer&#8217;s order.</em> -</p>
<p><strong>$this-&gt;cart-&gt;product_options(rowid);</strong><br />
- <em>This function not described in the documentation but used in the example, this is to get the available options on the product.</em> -</p>
<h4>4. The Examples</h4>
<p>I&#8217;m not providing any code from this post, these tutorial below can provide you to get started with this cart class. Maybe I&#8217;ll post the different tutorial from these. Just enjoy it.</p>
<p>CodeIgniter From Scratch: Shopping Cart ( <a title="CodeIgniter From Scratch: Shopping Cart" href="http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-shopping-cart/" target="_blank">link</a> )</p>
<p><a title="CodeIgniter From Scratch: Shopping Cart" href="http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-shopping-cart/" target="_blank"><img class="alignnone size-full wp-image-2252" title="net.tutsplus.com tutorials php codeigniter from scratch shopping cart" src="http://superdit.com/wp-content/uploads/2010/08/net_tutsplus_com_tutorials_php_codeigniter-from-scratch-shopping-cart.png" alt="net.tutsplus.com tutorials php codeigniter from scratch shopping cart" width="500" height="198" /></a></p>
<p>How to Build a Shopping Cart using CodeIgniter and jQuery ( <a title="How to Build a Shopping Cart using CodeIgniter and jQuery" href="http://net.tutsplus.com/tutorials/php/how-to-build-a-shopping-cart-using-codeigniter-and-jquery/" target="_blank">link</a> )</p>
<p><a title="How to Build a Shopping Cart using CodeIgniter and jQuery" href="http://net.tutsplus.com/tutorials/php/how-to-build-a-shopping-cart-using-codeigniter-and-jquery/" target="_blank"><img class="alignnone size-full wp-image-2253" title="net.tutsplus.com tutorials php how to build a shopping cart using codeigniter and jquery" src="http://superdit.com/wp-content/uploads/2010/08/net_tutsplus_com_tutorials_php_how-to-build-a-shopping-cart-using-codeigniter-and-jquery.png" alt="net.tutsplus.com tutorials php how to build a shopping cart using codeigniter and jquery" width="500" height="198" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://superdit.com/2010/08/04/understanding-codeigniter-cart-class/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Great smartphone comparison cart from Billshrink: Iphone, Palm, Droid, Nexus</title>
		<link>http://superdit.com/2010/01/06/great-smartphone-comparison-cart-from-billshrink-iphone-palm-droid-nexus/</link>
		<comments>http://superdit.com/2010/01/06/great-smartphone-comparison-cart-from-billshrink-iphone-palm-droid-nexus/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 00:43:51 +0000</pubDate>
		<dc:creator>aditia rahman</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[cart]]></category>
		<category><![CDATA[smartphone]]></category>

		<guid isPermaLink="false">http://superdit.com/?p=279</guid>
		<description><![CDATA[I just like the way their make the comparison table for each common feature on the phone, and it taken from http://www.billshrink.com/blog/]]></description>
			<content:encoded><![CDATA[<p>I just like the way their make the comparison table for each common feature on the phone, and it taken from <a href="http://www.billshrink.com/blog/" target="_blank">http://www.billshrink.com/blog/</a></p>
<p><img class="alignnone" src="http://farm5.static.flickr.com/4006/4249704524_0052a7912d_b.jpg" alt="" width="389" height="1024" /></p>
<p><span id="more-279"></span></p>
<p><img class="alignnone" src="http://farm3.static.flickr.com/2762/4249703476_6f2f7b3b3e_b.jpg" alt="" width="465" height="1024" /></p>
<p><img class="alignnone" src="http://farm3.static.flickr.com/2621/4248927927_7d15971d31_b.jpg" alt="" width="466" height="1024" /></p>
]]></content:encoded>
			<wfw:commentRss>http://superdit.com/2010/01/06/great-smartphone-comparison-cart-from-billshrink-iphone-palm-droid-nexus/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

