Wednesday, December 5, 2012

Saving cookie as an array


Every Browser have limit of setting cookie by domain And it is best practice  if save your cookie in form of array .Browser support saving cookie in form :-
1) name,value
2) Json
3) Serialized

So I have Create a class Cookie Manager in Php for saving , getting & checking cookie in form of array using serialized - unserialized.


/**
 * Description of CookieManager
 *
 * @author saurabh goyal
 */
class CookieManager {
    protected  $path;
    protected  $domain;
    protected  $expire;
    protected  $secure;
   
    /**
     * __construct
     * @param mixed $path,$domain,$expire
     * @param boolean $secure,$httpOnly
     * @param mixed $objResumeUpdateManager
     * @return void
     */
  
    public function __construct($path='/',$domain='.localhost',$expire='',$secure=false,$httpOnly=false) {       
        $this->path = $path;
        $this->domain = $domain;
        $this->expire = $expire;
        $this->secure = $secure;
        $this->httpOnly = $httpOnly;
    }
   
    public function setCookie($name,$value){
        setcookie($name, $value, $this->expire, $this->path, $this->domain, $this->secure,$this->httpOnly);       
    }
      /*
     * Set setCookie
     * @param string $name
     * @param string $value
     * return boolean
     */
    public function setSerializeCookie($name,$value) {
        try {           
            $this->setCookie($name,serialize($value));
            return true;
        } catch (Exception $e) {
            return false;
        }
    }

    public function getUnSerializeCookie($name) {
        return (isset($_COOKIE[$name]) && $_COOKIE[$name]!='')?unserialize($_COOKIE[$name]):false;
    }

     /*
     * Set setCookieArray
     * @param string $name
     * @param array $arrayValue
     * return boolean
     */

    public function setCookieArray($name,$arrayValue) {
        $cookieArray = $this->getUnSerializeCookie($name);
        if($cookieArray) {
             $arrayValue = $arrayValue+$cookieArray;
        }
        $this->setSerializeCookie($name,$arrayValue);
        return true;
    }
  
    /*
     * Set unsetCookieArray
     * @param string $name
     * @param string $value key of array
     * return boolean
     */
    public function unsetCookieArrayByKey($name,$key) {
         $cookieArray = $this->getUnSerializeCookie($name);
         if($cookieArray && (array_key_exists($key,$cookieArray) !== false)) {
             unset($cookieArray[$key]);            
             $this->setSerializeCookie($name,$cookieArray);
         }
         return true;
    }

    public function getCookieValueByKey($name,$key) {
        $cookieArray = $this->getUnSerializeCookie($name);
        if($cookieArray) {
           return isset($cookieArray[$key])?$cookieArray[$key]:false;
        }
        return false;
    } 
      
}

?>





And Javascript code for same :-



Tuesday, August 23, 2011

Funtion of Sorting an Array using Php

$arr = array(1,7,6,8,4,5,2);

function sg_array_sort($arr) {
    for($i=1;$i      for($j=$i-1;$j>=0;$j--) {   
         if($arr[$i] < $arr[$j]) {
            $temp = $arr[$i];
            $arr[$i]= $arr[$j];
            $arr[$j] = $temp;
            $i = $j;
         } 
      }
    }
}


$sort_arr =  sg_array_sort($arr);


Many-to-many MySql relation: how to retrieve all related entries in one query?

Suppose there are 3 Table 1) Products , 2)Category & 3)Pro_Cat :- Many to Many RelationShip B/w Products & Category Table :-

1) Products Table :-                                  


Product_Id
Product_Name
1
Samsung
2
Relience
3
Micromax

2) Category Table :-


Category_Id
Category_Name
1
Mobile
2
Retails
3
Usb

3) Product_Category Many into Many Relationship Table :-


Product_Id
Category_Id
1
1
2
1
2
2
3
1
3
3


Result Query :-

SELECT
  product.product_name,
  GROUP_CONCAT(category.category_name) AS category_name
FROM product,category,pro_cat
Where product.product_id = pro_cat.product_id
and category.category_id = pro_cat.category_id
GROUP BY product.product_id;

Result :-


Product_Name
Category_Names
Samsung
Mobile
Relience
Mobile,Retails
Micromax
Mobile ,Usb

Tuesday, July 26, 2011

The Flipkart Story (Case Study)

It’s been about a year since Flipkart started moving from being a pure bookstore to selling mobiles/DVDs etc. Then we had cribbed about a possible brand dilution and investor pressure. Probably we were never sure about the bigger picture. Since then, Flipkart went for a major brand makeover, making it look more ‘upmarket’. There has been large newspaper ads, TVCs and a lot of web ads. But unlike other eCommerce companies the inorganic marketing kicked in only when the product was strong.
Flipkart already had a proven model execution with books and extending to other verticals did not need infrastructural changes. Flipkart’s real achievement has been in solving the pain points in Indian eCommerce that most well funded players are still complaining  about.
Here are some of the things that Flipkart has done well in solving each of these problems.

1. Discoverability:

It is the case with any venture on the web, “How does the customer find us?” Answer: Organically!
Flipkart has been the “baap” of SEO. This has been the most important contributor to their success. I say this, because only when you see people coming to you, you get encouraged to deliver more and keep adding. There is no fun (motivation) in adding features to a product that no one is using.
Though from what I had noticed, SEO did not come the straight way. There were particularly 2 things that are worth mentioning.
a. Yahoo News: Until last year Flipkart had a feed of Yahoo News on its product pages. From what I understand of SEO, this is to increase the keyword density and introduce ‘original’ content on the page, as the product description across all books sites is same. This was removed later as it was violating the Yahoo’s ToS on using the service for a commercial site. Check the Waybackmachine here. I loved the risk they took for this.
b. We Do Not Sell Used Books: This one is my favorite. If you check the Waybackmachine in the last line you will notice the following text.

  • We DO NOT sell old books or used books. All the books listed at Flipkart.com are new books.

  • The books listed at Flipkart.com are NOT available for free download in ebook or PDF format.

  • The magic of this text is that if you search for free ebook or pdf download” you would always get Flipkart among the top results. These are very popular search queries and Flipkart had nothing to do with it but still they cashed in. This was also the time when Flipkart had Adsense embedded. People would come to the site, see nothing like a “PDF download” button, and then see an ad for PDF download and click. This meant more revenue for Flipkart. I have done this atleast twice myself. Given that the margin on books are very small after the discount, Flipkart was probably earning more by saying what they did not do than by doing what they actually were suppose to do.

    2. Payments:

    No credit card/netbanking, fear to transact online, repeat transaction failures, no access to web – these are the common problems with online payments. What Flipkart is doing to overcome these?
    From what I last counted, Flipkart had atleast 4 different Payment Gateways integrated. They introduced Cash-on-Delivery. Then they are also doing order on phone. Payment via DD/Cheque is also accepted.
    2 basic things that they are currently doing that takes little tech. effort but quite some product management ‘will’:
    a. Auto redirection to banking site: Unlike most other ecommerce sites, Flipkart never lands you on CCavenue page, you are auto redirected to the banks page where the info is required to be filled. Flipkart by-passes 1 unnecessary page by passing the required parameters directly to CCavenue and not through a user interface.
    b. Banks Status: Flipkart maintains its own real time status if the bank’s netbanking is working or not. So there are no surprises after you have chosen the bank and then go to the netbanking page.
    And if you think Flipkart gets very good rates from Payment Gateways, not really. Atleast 1 big PG that I am aware of charges quite high rates to Flipkart, atleast 40-50% more than to lesser known players.

    3. Inventory:

    I come from a traditional business family where we believe in selling what we have. The world of eCommerce really amazes me when I see the players keeping a standard list of products and then go out procuring it only when there is an order placed. Imagine if you go to a brick & mortar shop and after billing the manager sends a boy to a nearby store to get the goods. This is where the problem starts. There is no inventory on their end and there is no live status of inventory from their supplier. Remember The Alchemist, “Never Promise something that you don’t have“.
    After placing an order, they would keep looking for the product at multiple places. After a week you might get a call saying that either the product is not available and we will do a ‘favor’ by refunding your money or if the product is there, it is not the color/size that you asked for.
    Flipkart was no different in 2009, a couple my friends used to get similar calls after days of ordering. But for the last 1 year atleast Flipkart maintains its own inventory(or atleast it seems so). They are selling what they have. From pure hearsay, Flipkart is taking up a big warehouse in Bangalore and is in talks for one in NCR as well. One of the few companies that is using the funding to build a business and not spend it like a FMCG company on ads.

    4. Delivery:

    I have dealt with courier companies in my last startup and am quite aware of the ‘fcukall’ standards they have. Most similar looking envelopes are never delivered thinking that it is a marketing package and no one would track it. They would be willing to bargain on rates but would never say anything about the service. Paying a premium may not solve the problem always.
    Flipkart is exploiting this problem as a cashable need gap and building its own delivery backendFlipkart is seen delivering through their own delivery boys in Bangalore and at times within 12hrs from order.
    Flipkart has started putting fliers in newspapers in Bangalore with a product listing, call-to-order phone number and a promise of delivering ‘tomorrow’. This means more discoverability, no payment problem and no delivery delay. The way it is actually suppose to be.
    From what I heard from multiple sources, Flipkart is looking to build its own courier company. The recent $20Mn funding from Tiger Global was only part of a larger sum they are known to be raising. Flipkart is looking to raise $100Mn at a valuation of $200Mn.
    Recently, Flipkart has started selling everything from cameras, laptops to gaming consoles to personal and health care electronic products. There are major talks about Amazon acquiring Flipkart but it would only make sense to grow it bigger and better from here. A handover wouldn’t see the same product management ‘will’.
    Flipkart is a story that is come from smart work and an ‘it is possible’ attitude. There is a need to for a couple of more stories like these and there would be no cribbing about Indian eCommerce not working.

    Thursday, May 26, 2011

    MySql FEDERATED storage engine :Data to be accessed from a remote MySQL database on a local server without using replication or cluster technology

    MySql FEDERATED storage engine :-

    Than enable you to create table into local server Database and allow access data from Remote Server Database without using replication or cluster technology.

    Use FEDERATED Tables :-

    First, you must have a table on the remote server that you want to access by using a FEDERATED table. Suppose that the remote table is in the federated database and is defined like this:

    CREATE TABLE test_table (
        id     INT(20) NOT NULL AUTO_INCREMENT,
        name   VARCHAR(32) NOT NULL DEFAULT '',
        other  INT(20) NOT NULL DEFAULT '0',
        PRIMARY KEY  (id),
        INDEX name (name),
        INDEX other_key (other)
    )
    ENGINE=MyISAM
    DEFAULT CHARSET=latin1;
    The example uses a MyISAM table, but the table       could use any storage engine.     
    Next, create a FEDERATED table on the local server for accessing the remote table:

    CREATE TABLE federated_table (
        id     INT(20) NOT NULL AUTO_INCREMENT,
        name   VARCHAR(32) NOT NULL DEFAULT '',
        other  INT(20) NOT NULL DEFAULT '0',
        PRIMARY KEY  (id),
        INDEX name (name),
        INDEX other_key (other)
    )
    ENGINE=FEDERATED
    DEFAULT CHARSET=latin1
    CONNECTION='mysql://fed_user@remote_host:9306/federated/test_table';
    (Before MySQL 5.0.13, use COMMENT rather than       CONNECTION.)


    Check FEDERATED Engine enable or not in database :-

    mysql> show engines;
    +------------+---------+
    | Engine     | Support | 
    +------------+---------+
    | ndbcluster | NO      | 
    | MRG_MYISAM | YES     | 
    | BLACKHOLE  | YES     | 
    | CSV        | YES     | 
    | MEMORY     | YES     | 
    | FEDERATED  | NO      | 
    | ARCHIVE    | YES     | 
    | InnoDB     | YES     | 
    | MyISAM     | DEFAULT |
     
     
    For Enable federated Engines into MYSQL :-
     
    Add Below Line into my.ini or my.cnf :-
     
     [mysqld]
    federated
     
    Then Restart MySQL Server.