Duration Select Menu in Shopping Cart

Discussion in 'PHP' started by diane.langenstrass, Dec 19, 2012.

  1. #1
    Good Day

    I am creating my first php shopping cart and have been struggling to get the duration select menu to work. Below is the code I am working on. If anyone could please point out were I am going wrong or see anything I have missed it will be greatly appreciated. I have been able to use the select menu in the shopping cart but when there is more then one product in the cart it takes the same price of the first product added to the cart.

    This is php part telling it what to do.....

    <?php
    $duration = "";
    $price_to_adjust = "";
    if (isset($_POST['price_to_adjust']) && $_POST['price_to_adjust'] != "") {
    // execute some code
    $price_to_adjust = $_POST['price_to_adjust'];
    $duration = $_POST['duration'];
    foreach ($_SESSION["cart_array"] as $each_item) {
    $item_id = $each_item['item_id'];
    $sql = mysql_query("SELECT * FROM products WHERE id='$item_id'");
    while ($row = mysql_fetch_array($sql)) {
    $price = $row["price"];
    $price2 = $row["price2"];
    $price3 = $row["price3"];
    $price4 = $row["price4"];
    }
    if ($duration == "price") { $duration = $price; }
    if ($duration == "price2") { $duration = $price2; }
    if ($duration == "price3") { $duration = $price3; }
    if ($duration == "price4") { $duration = $price4; }


    $i = 0;
    foreach ($_SESSION["cart_array"] as $each_item) {
    $i++;
    while (list($key, $value) = each($each_item)) {
    if ($key == "item_id" && $value == $price_to_adjust) {
    $price_value = array("price" => $price, "price2" => $price2, "price3" => $price3, "price4" => $price4);
    // That item is in cart already so let's adjust its quantity using array_splice()
    array_merge_recursive($each_item, $price_value);
    } // close if condition
    } // close while loop
    }
    }
    }
    ?>

    Here is the select menu that is outputting to the cart....

    $cartOutput .= '<td><form class="form" action="cart.php" method="POST">
    <select name="duration">
    <option value="price">1 Day</option>
    <option value="price2">2 Days</option>
    <option value="price3">3 Days</option>
    <option value="price4">4-7 Days</option>
    </select>
    <input name="changeBtn' . $item_id . '" type="submit" value="Update" />
    <input type="hidden" name="price_to_adjust" value="' . $item_id . '"/>
    </form></td>';

    Thanks in advance for assisting :)
     
    diane.langenstrass, Dec 19, 2012 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    You're setting $duration, but never using it. What are you expecting it to do?

    You're never echoing $cartOutput. (I assume that you're creating the variable somewhere, because .= doesn't create a variable.)
     
    Rukbat, Dec 20, 2012 IP