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
    Nov 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Any ideas as to what's going on??

    Hi all

    I've been tasked with coding up a basic shopping cart...

    here's the working page

    http://www.earthinke.co.uk/main_site/wallpaper.php

    All seems well, but if you add a product to the cart, it goes in, but you can't change the quantity at the cart stage? And in trying it loses the selected colour. Also when you add multiple items of the same spec it adds them as separate items rather than combining them together...

    Any advice would be appriecated!!

    Heres the code:

    PHP Code:
    <?php
    SESSION_START
    ();


    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //       Section 1 (if user attempts to add something to the cart from the product page)
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    if (isset($_POST['pid'])) 
        {
        
    $pid $_POST['pid'];
        
    if(isset(
    $_POST['selectedcol'])) 
        {
        
    $colour $_POST['selectedcol'];
            
        
    $wasFound false;
        
    $i 0;
        
    // If the cart session variable is not set or cart array is empty
        
    if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
            
    // RUN IF THE CART IS EMPTY OR NOT SET
            
    $_SESSION["cart_array"] = array(=> array("item_id" => $pid"quantity" => 1,"colour" => $colour));
        } else {
            
    // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
            
    foreach ($_SESSION["cart_array"] as $each_item) {
                  
    $i++;
                  while (list(
    $key$value) = each($each_item)) {
                      if (
    $key == "item_id" && $value == $pid && $key == "colour" && $value == $colour) {
                          
    // That item is in cart already so let's adjust its quantity using array_splice()
                          
    array_splice($_SESSION["cart_array"], $i-11, array(array("item_id" => $pid"colour" => $colour"quantity" => $each_item['quantity'] + 1)));

                          
                          
                          
    $wasFound true;
                      } 
    // close if condition
                  
    // close while loop
               
    // close foreach loop
               
    if ($wasFound == false) {
                   
    array_push($_SESSION["cart_array"], array("item_id" => $pid"colour" => $colour"quantity" => 1));
               }
            }
        }
            
    header("location: cart.php");
           
        exit();
    }

    ?>
    <?php 
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //       Section 2 (if user chooses to empty their shopping cart)
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
        unset(
    $_SESSION["cart_array"]);
    }
    ?>
    <?php 
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //       Section 3 (if user chooses to adjust item quantity)
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
        
    // execute some code
        
    $item_to_adjust $_POST['item_to_adjust'];
        
    $quantity $_POST['quantity'];
        
    $quantity preg_replace('#[^0-9]#i'''$quantity); // filter everything but numbers
        
    if ($quantity >= 100) { $quantity 99; }
        if (
    $quantity 1) { $quantity 1; }
        if (
    $quantity == "") { $quantity 1; }
        
    $i 0;
        foreach (
    $_SESSION["cart_array"] as $each_item) { 
                  
    $i++;
                  while (list(
    $key$value) = each($each_item)) {
                      if (
    $key == "item_id" && $value == $item_to_adjust && $key == "colour" && $value == $colour ) {
                          
    // That item is in cart already so let's adjust its quantity using array_splice()
                          
    array_splice($_SESSION["cart_array"], $i-11, array(array("item_id" => $item_to_adjust"colour" => $colour"quantity" => $quantity)));
                      } 
    // close if condition
                  
    // close while loop
        
    // close foreach loop
    }
    ?>
    <?php 
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //       Section 4 (if user wants to remove an item from cart)
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
        
    // Access the array and run code to remove that array index
         
    $key_to_remove $_POST['index_to_remove'];
        if (
    count($_SESSION["cart_array"]) <= 1) {
            unset(
    $_SESSION["cart_array"]);
        } else {
            unset(
    $_SESSION["cart_array"]["$key_to_remove"]);
            
    sort($_SESSION["cart_array"]);
        }
    }
    ?>

    <?php 
    include("./includes/database.php");
    ?>

     

    <?php 
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //       Section 5  (render the cart for the user to view on the page)
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    $cartOutput "";
    $cartTotal "";
    $pp_checkout_btn '';
    $product_id_array '';
    if (!isset(
    $_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
        
    $cartOutput "<h2 align='center'>Your shopping cart is empty</h2>";
    } else {
        
    // Start the For Each loop
        
    $i 0
        foreach (
    $_SESSION["cart_array"] as $each_item) { 
            
    $item_id $each_item['item_id'];
            
    $sql mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
            while (
    $row mysql_fetch_array($sql)) {
                
    $product_name $row["prodname"];
                
    $price number_format($row["prodprice"], 2);
                
    $details $row["proddesc"];
            
            }
            
    $pricetotal $price $each_item['quantity'];
            
    $cartTotal $pricetotal $cartTotal;

        
            
    $pricetotal number_format($pricetotal2);
            
            
    // Create the product array variable
            
    $product_id_array .= "$item_id-".$each_item['quantity'].$each_item['colour'].","
            
    // Dynamic table row assembly
            
    $cartOutput .= "<tr>";
            
    $cartOutput .= '<td><a href="product.php?id=' $item_id '">' $product_name ."</a> <br />in ".$each_item['colour'].'</td>';
            
    $cartOutput .= '<td>'.$colour.'</td>';
            
    $cartOutput .= '<td>&pound;' $price '</td>';
            
    $cartOutput .= '<td><form action="cart.php" method="post">
            <input name="quantity" type="text" value="' 
    $each_item['quantity'] . '" size="1" maxlength="2" />
            <input name="adjustBtn' 
    $item_id '" type="submit" value="change" />
            <input name="item_to_adjust" type="hidden" value="' 
    $item_id '" />
            </form></td>'
    ;
            
    //$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
            
    $cartOutput .= '<td>&pound;' $pricetotal '</td>';
            
    $cartOutput .= '<td align="center"><form action="cart.php" method="post"><input name="deleteBtn' $item_id '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' $i '" /></form></td>';
            
    $cartOutput .= '</tr>';
            
    $i++; 
        } 

        
    $cartTotal number_format($cartTotal2);
        
    $cartTotal "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : &pound;".$cartTotal."</div>";
        
    }
    ?>

    <?php

    $page_title 
    "Wallpaper Collection";
    $meta_desc "Earth Ink Meta Description Here";
    $meta_key "Earth Inke, wallpaper, wall, paper, ink, earth, designer, design, handcrafted,";
    ?>

    <?php include("./includes/head.php"?> 

    <!-- WRAPPER DIV Started by HEAD -->

    <?php include("./includes/nav.inc.php"?> 

    <!-- MAIN CONTENT OF PAGE BELOW HERE -->

    <div id="mainPage">
    <div class="">

    <table width="100%" border="0" cellspacing="0" cellpadding="4">
       
       
       




        <td width="100%" align="left" valign="top"  bgcolor="">

    <div id="pageContent">




        <div style="margin:24px; text-align:left;">
        
        <br />
        <table width="100%" border="0" cellspacing="0" cellpadding="6">
          <tr>
            <td width="35%" bgcolor="#C5DFFA" align="center"><strong>Product</strong></td>
            <td width="40%" bgcolor="#C5DFFA"><strong>Product Description</strong></td>
            <td width="10%" bgcolor="#C5DFFA"><strong>Unit Price</strong></td>
            <td width="15%" bgcolor="#C5DFFA"><strong>Quantity</strong></td>
            <td width="18%" bgcolor="#C5DFFA"><strong>Total</strong></td>
            <td width="10%" bgcolor="#C5DFFA" align="center"><strong>Remove</strong></td>
          </tr>
         <?php echo $cartOutput?>
         <!-- <tr>
            <td align="center">&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr> -->
        </table>
        <?php echo $cartTotal?>
        <br />
      <div id="comprar">
         <div style="right:auto;">
        <a href="#">Go to check out</a>
        </div>
        </div>

    <br />
        <br />
        <br />
        <a href="cart.php?cmd=emptycart">Empty cart</a><br />
             <a href="wallpaper.php">Back to the range...</a>
     
        
       <br />
      </div>


    <br />
    <br />
    <br />
    <br />
    <br />
    <br />




           
           


        

      </tr>
    </td>

    </table>

    </div>
    </div>


      </div>
    <!-- END MAIN CONTENT OF PAGE BELOW HERE -->

        <?php include("./includes/footer.php"?>
    Last edited by Inigoesdr; 12-10-2011 at 04:53 PM.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,362
    Thanks
    61
    Thanked 530 Times in 517 Posts
    I can only assume that you've ignored my previous advice in your previous topic about thsi issue. That being the case why ask again?
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    havent ignored your advice...took it => found another way of doing it that uses session arrays...but it's still not working, so I was looking for some help.


  •  

    Tags for this Thread

    Posting Permissions

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