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
    Jan 2014
    Posts
    74
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Can't get foreach working

    Basically, $_POST['cartquantity'] equals 3, so I want it to add the value of $row['weight'] three times to $_SESSION['cart']['weight']. I tried to do it with foreach, but it doesn't work. What have I done wrong?

    PHP Code:

    foreach ($_POST['cartquantity']) {
                
    $_SESSION['cart']['weight'] += $row['weight'];
            } 

  • #2
    Regular Coder
    Join Date
    Jun 2009
    Posts
    145
    Thanks
    3
    Thanked 21 Times in 21 Posts
    You need to count, I take it your post value is a 3, not 3 incidences of the POST so a foreach loop wouldnt be the way to achieve what you are after, also your logic for your loop doesnt make sense.

    PHP Code:
    $total $_POST['cartquantity'];
    while(
    $total 0){
          
    $_SESSION['cart']['weight'] += $row['weight'];
          
    $total -= 1;


  • #3
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,343
    Thanks
    11
    Thanked 288 Times in 287 Posts
    Or skip the loop and just multiply the weight by the count:
    PHP Code:
    $_SESSION['cart']['weight'] += $_POST['cartquantity'] * $row['weight']; 


  •  

    Posting Permissions

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