Codeigniter

Clean URLs by removing /index.php/ from urls in codeigniter

In codeigniter you can easily use very search engine friendly urls.

By default it is shown as

http://www.yoursite.com/index.php/controller_name/function/id

You can easily remove this disgusting index.php from your urls.

3 Comments

Read more

Custom shopping cart library for codeigniter

I have created this shopping cart library for my super shop management application I created few days ago. I would like to share this with others. This library contains default functionality of creating shopping cart and holding info about products in user session.

<?php
/*********************************************
 * Custom Shopping Cart for codeigniter      *
 * Author: Burhan Uddin                      *
 * Date: 17 March, 2010                      *
 * Web: http://www.dscripts.net              *
**********************************************/

class Shopping_cart {
    public $ci;             // holds reference pointer to CI object
    public $cart = array(); // array to store cart
    private $session_var = 'shopping_cart'; // name of the session variable to be used

    // constructor
    function Shopping_cart() {
        $this->ci = &get_instance(); //put reference to codeigniter object for this application
        $this->_get_cart();  // get the cart from session to array
    }

    /* Gets the cart from session
     * access: private
     */
    function _get_cart() {
        $cart = $this->ci->session->userdata($this->session_var);
        if(!empty($cart))
            $this->cart = unserialize($cart);
    }

    /* saves the cart to session
     * access: private
     */
    function _save_cart() {
        return $this->ci->session->set_userdata($this->session_var, serialize($this->cart));
    }

    /* add items to cart
     * access: public
     * @param: id =  String or integer, unique id used to determine the product generally database id
     * @param: quantity = Integer, quantity of product added in cart
     */
    function add($id, $quantity) {
        $this->cart[$id] = $quantity;
        $this->_save_cart();
    }

    /* Updates cart specific item
     * access: public
     * @param: id =  String or integer, unique id used to determine the product generally database id
     * @param: quantity = Integer, quantity of product added in cart
     */
    function update($id, $quantity) {
        $this->cart[$id] = $quantity;
        $this->_save_cart();
    }

    /* Removes items from cart
     * access: public
     * @param: id =  String or integer, unique id used to determine the product generally database id
     */
    function remove($id) {
        unset($this->cart[$id]);
        $this->_save_cart();
    }

    /* Clear all contents of cart
     * access: public
     */
    function clear() {
        $this->cart = array();
        $this->_save_cart();
    }

    /* Returns the total number of items in cart
     * access: public
     * return integer
     */
    function count() {
        return count($this->cart);
    }

    /* Returns the items array in cart
     * access: public
     * return array
     * sample return data
     * $cart = $this->shopping_cart->items();
     * when integer id
     * $cart = array(
     *              [1] => 3,
     *              [2] => 5
     *          )
     * when string id
     * $cart = array(
     *              [mango] => 3,
     *              [banana] => 5
     *          )
     */
    function items() {
        return $this->cart;
    }
}
?>
1 Comment

Decimal validation using codeigniter form validation class

Codeigniter is a really cool light weight php framework with lots of cool features. One of its cool features in codeigniter form validation. Using codeigniter you can set rules for specific fields and validate them without any hastle of rewritting validation code. Here is the manual of this validation library. If you dont have idea about it go and read.

http://codeigniter.com/user_guide/libraries/form_validation.html

So here we can see we can set validation rules for a input field like this

No Comments

Read more