Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2007
    Location
    England
    Posts
    495
    Tokens
    0

    Default [PHP] Session Help

    Hey,

    I have this code:

    PHP Code:
    <?php
    session_start
    ();

    if(
    $_GET['del'] == "yes"){
    session_destroy();
    }

    function 
    addToCart() {
        
    // count at what order_line number we currently are and increment with 1 for our next line
        
    $order_line_id count($_SESSION['cart']) + 1;
        
    $_SESSION['cart'][$order_line_id]['product_id'] = $_POST['product_id']; // unique product id, should be taken from your DB
        
    $_SESSION['cart'][$order_line_id]['quan'] = $_POST['quan'];
        
    $_SESSION['cart'][$order_line_id]['price'] = $_POST['price']; // price for each product
    }


    if(isset(
    $_POST['submit'])) { // did the user press a submit button?
        
    addToCart(); // run the addToCart function to add the form information to the session array.
    }


    if(
    $_SESSION['cart']){

    $total_order_price 0;

    for(
    $i 0$i <= count($_SESSION['cart']); $i++) {


        
    echo 
    "Product id : " $_SESSION['cart'][$i]['product_id'] . "<br>";
    echo 
    "Quantity : " $_SESSION['cart'][$i]['quan'] . "<br>";
    echo 
    "Price : " $_SESSION['cart'][$i]['price'] . "<br>";

    $line_price $_SESSION['cart'][$i]['price'] * $_SESSION['cart'][$i]['quan'];

    echo 
    "Total price : " $line_price "<br><br>";

    $total_order_price $total_order_price $line_price;

    }

    echo 
    "Shipping: 4.99<br><br>";

    $total_order_price_ship $total_order_price 4.99;

    echo 
    "Totale order price : "$total_order_price_ship;


    }else{
    echo 
    "No Cart!";
    }

    ?>



    <form action="test2.php" method="post">

      <input name="product_id" type="hidden" id="product_id" value="IMG-001">
      <input name="quan" type="hidden" id="quan" value="5">
      <input name="price" type="hidden" id="price" value="1.00">
      
      <input type="submit" name="submit" value="Add">
    </form>
    And when you click add, it shows this:

    Product id :
    Quantity :
    Price :
    Total price : 0

    Product id : IMG-001
    Quantity : 5
    Price : 1.00
    Total price : 5

    Shipping: 4.99

    Totale order price : 9.99
    Why does it make one with nothing in it?!
    It only does it with the first one...

    Dan


  2. #2
    Join Date
    Dec 2006
    Location
    Swindon
    Posts
    3,299
    Tokens
    215
    Habbo
    dunko

    Latest Awards:

    Default

    Delete this post please, read the code wrong.
    Last edited by Blob; 18-10-2009 at 05:20 PM.

  3. #3
    Join Date
    Jun 2007
    Location
    England
    Posts
    495
    Tokens
    0

    Default

    Also, How would you make the code check the session to check if they have already added the product and then update the quantity so it doesnt make a new session and then make duplicate sessions.

    Dan


  4. #4
    Join Date
    Mar 2008
    Location
    Swindon, UK
    Posts
    1,274
    Tokens
    187
    Habbo
    :Ltd

    Latest Awards:

    Default


    for(
    $i = 1; $i <= count($_SESSION['cart']); $i++) {
    Hi, names James. I am a web developer.

  5. #5
    Join Date
    Jun 2007
    Location
    England
    Posts
    495
    Tokens
    0

    Default

    Quote Originally Posted by Protege View Post

    for(
    $i = 1; $i <= count($_SESSION['cart']); $i++) {

    Thanks mate,

    Also did you read my post above?

    Dan


  6. #6
    Join Date
    Jun 2007
    Location
    England
    Posts
    495
    Tokens
    0

    Default

    Oh and how do I remove a product?

    Dan


  7. #7
    Join Date
    Jun 2007
    Location
    England
    Posts
    495
    Tokens
    0

    Default

    Still need help on this please =)


Posting Permissions

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