Multiple items in a PHP shopping cart

Posted by Nick on Stack Overflow See other posts from Stack Overflow or by Nick
Published on 2010-05-22T21:43:29Z Indexed on 2010/05/22 21:50 UTC
Read the original article Hit count: 290

Filed under:
|
|
|

I'm in the progress of making a shopping cart in PHP. To check if a user has selected multiple products, I put everything in an array ($contents). When I output it, I get something like "14,14,14,11,10". I'd like to have something like "3 x 14, 1 x 11, 1 x 10". What is the easiest way to do that? I really have no clue how to do it.

This is the most important part of my code.

    $_SESSION["cart"] = $cart;

    if ( $cart ) {
        $items = explode(',', $cart);
        $contents = array();
        $i = 0;
        foreach ( $items as $item ) {
            $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
            $i++;
        }

        $smarty->assign("amount",$i);


        echo '<pre>';
        print_r($contents);
        echo '</pre>';

Thanks in advance.

© Stack Overflow or respective owner

Related posts about php

Related posts about shopping-cart