Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2011
    Location
    London
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Shopping Cart problem. array_push()

    Hi guys, I'm working on a little shopping cart at the moment, and what I'm trying to do is append the array to the $_SESSION["cart"] array. You can copy and paste the code to test it for yourselves, however, if anyone could help me by telling me where my error is then please let me know.

    thanks
    sho

    PHP Code:
    <?php
        session_start
    ();

        
    $_SESSION["cart"] = array();
        
        
    $products = array(
                                        
    "1"=>array("Adidas DeMOlisher""39.99"), //array 1
                                        
    "2"=>array("Adidas Prime Sprint""109.99"), //array 2
                                        
    "3"=>array("Nike Zoom Mawler""79.99"), //array 3
                                        
    "4"=>array("Asics Cyberflash""54.99"), //array 4
                                 
    );
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <title>Sho-Silva Shopping Cart Example</title>
    <style type="text/css">
        body {
            font: 11px verdana;
            line-height: 18px;
        }
        
        label {
            float: left;
            padding: 5px;    
            width: 100px;
        }
        
        select {
            border-radius: 3px;
            border: 1px solid #ccc;
            font: 11px verdana;
            padding: 5px;
            width: 217px;
        }
        select:hover {
            box-shadow: 0px 0px 8px #ccc;    
        }
            
        input[type=text] {
            border-radius: 3px;
            border: 1px solid #ccc;
            font: 11px verdana;
            padding: 5px;
            width: 205px;
        }
        input[type=text]:focus {
            box-shadow: 0px 0px 8px #ccc;
            color: #c00;
        }
        input[type=submit] {
            background: #f1f1f1;
            border: 1px solid #ccc;
            border-radius: 2px;
            color: #888;
            font: inherit;
            padding: 5px;
        }
        input[type=submit]:hover {
            background: #eee;
            border: 1px solid #ccc;
            border-radius: 2px;
            box-shadow: 0px 0px 6px #ccc;
            color: #888;
            font: inherit;
            padding: 5px;
        }
    </style>
    </head>

    <body>
    <div style="float: left;">
    <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
        <p><label>Chose Item </label>
                    <select name="prod">
                        <option>Please select item</option>
                        <?php
                            
    foreach($products as $key =>$value) {
                                    echo 
    "<option value='".$key."'>".$value[0]." - &pound".$value[1]."</option>";
                            }
                        
    ?>                
                    </select>
        </p>
        <p><label>Quantity</label><input type="text" name="sdQuantity" /></p>
        <p><input type="submit" name="btnSub" value="Add to Cart" /></p>
    </form>
    </div>

    <div style="float: left; padding-left: 50px; width: 400px;">
        <p style="background: url(icons/shopping-cart.gif) no-repeat; border-bottom: 1px dotted #cc0; font-weight: bold; height: 25px; text-indent: 20px;">Shopping Cart</p>
        <?php
            
    if (!empty($_SESSION["cart"])) {
                echo 
    "Your basket is empty at the moment.";
            } else {
                
    $prodKey=$_POST["prod"];
                
    //die($prodKey);
                
    if (!empty($prodKey)) {
                    if (!
    is_numeric($prodKey)) {
                        echo 
    "There's an error with the product ID.";
                    } else {
                        if(isset(
    $_POST["sdQuantity"])) {
                            
    $quantity=$_POST["sdQuantity"];
                            if(
    is_numeric($quantity)) {
                                    echo 
    "This product key is: <span style='color: #c00; font-weight: bold;'>" $prodKey "</span><br />";
                                    if(
    array_key_exists($prodKey$products)) {
                                        echo 
    "...and we've found it in the array.<br />";
                                        
                                        
    $mixedArray $products[$prodKey]; //array key
                                        
    list($name$cost) = $mixedArray;
                                        
                                        
    $i 0;
                                        if ((!isset(
    $_SESSION["cart"])) || (($_SESSION["cart"])<1)) {
                                            
    $_SESSION["cart"] = array(
                                                                    
    1=> array(
                                                                            
    "item_id" => $prodKey,
                                                                            
    "quantity" => 1
                                                                        
    )
                                                                    );
                                        }
                                        else {
                                            foreach(
    $_SESSION["cart"] as $each_item) {
                                                
    $i++;
                                                while(list(
    $key$value) = each($each_item)) {
                                                    if (
    $key == "item_id" && $value == $prodKey) {
                                                        
    #array_splice($input, $offset, $length, $replacement);
                                                        #watch this: http://www.youtube.com/watch?v=WXqbQy9fOp8
                                                        
    array_splice($_SESSION["cart"], $i-11, array(array("item_id" => $prodKey"name" => $name"price" => $cost"quantity" => $each_item["quantity"] + 1)));
                                                    }
                                                }
                                            }
                                            
                                            
                                            
                                            if (
    array_push($_SESSION["cart"], array("item_id" => $prodKey"name" => $name"price" => $cost"quantity" => 1))) {
                                                echo 
    "<p style='color: #c00; font-weight: bold;'>Item was added</p>";
                                            } else {
                                                echo 
    "Unable to add item.";
                                            }
                                            
                                            print 
    "<pre>";
                                            
    print_r($_SESSION["cart"]);
                                            print 
    "</pre>";                                        
                                        }
                                    }
                            } else {
                                echo 
    "This isn't a number.";
                            }
                        } else {
                            echo 
    "Please ensure you've entered the quantity you want.";
                        }
                    }
                } else {
                    echo 
    "Please ensure you select the product you want.";
                }
            }    
        
    ?>
    </div>
    </body>

    </html>
    Last edited by Inigoesdr; 09-19-2011 at 12:02 AM.

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Are you getting an error? I don't really understand what the question is.

    Look into array_merge() maybe.

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    You would append it with:
    PHP Code:
    $_SESSION['cart'] = $products// overwrite the cart
    $_SESSION['cart'][] = $products// append to the card array
    $_SESSION['cart']['products'] = $products// append the cart array specifying a key 
    Or use array_merge() to combine them, or array_push() if you are looping over them and don't want to use the "append" syntax above


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •