Need help with foreach loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
midjam
Forum Newbie
Posts: 2
Joined: Wed Sep 22, 2010 4:40 am

Need help with foreach loop

Post by midjam »

Hi guys,

I'm new to php and i'm having difficulty getting a foreach loop to work as I want it. I'm trying build a simple shopping cart where users can select what extras to have with thier meal.
I need to know whether a checkbox has been checked, if so insert the extraName, extraPrice and extraFree= 1/0 into the database.

Here is what I have so far, it checks if the checkbox is checked and if so inserts the records, but it's only inserting the FIRST extraName, extraPrice and extraFree record into each row.

Code: Select all

/////////////////////////////////////////////////////
// Submit to Order Form /////////////////////////////
if (isset($_POST["addtoorderButton"]))
    {
    mysql_query("INSERT INTO cart(cookieID, customerID, fooditemID, foodItemName, foodItemPrice) VALUES('$cookieID', '$customerID', '$fooditemID', '$foodItemName', '$foodItemPrice' ) ") or die(mysql_error());
    
    if ($_POST['checkbox'] != NULL)
        {    
            $cart_id = mysql_insert_id();
            echo "Checkboxes: ";
            print_r ($_POST['checkbox']);
            
                foreach($_POST['hextraFree'] as $key2)
                    {
                        $extraFree = $key2;
                        echo "extra free : ";
                        echo $extraFree;
                        echo "<br />";
                    }
                        
                    foreach($_POST['hextraName'] as $key3 => $value3)
                        {
                            $extraName = $value3;
                            echo "extra name : ";
                            echo $extraName;
                            echo "<br />";
                        }
                            
                        foreach($_POST['hextraPrice'] as $key4 => $value4)
                            {
                                $extraPrice = $value4;
                                echo "extra price : ";
                                echo $extraPrice;
                                echo "<br />";
                            }
                                
                            foreach($_POST['checkbox'] as $key => $value)
                                {
        //echo $key;
        mysql_query("INSERT INTO cartextras(cartID, cookieID, extraName, extraID, extraPrice, extraFree) VALUES('$cart_id', '$cookieID', '$extraName', '$key', '$extraPrice', '$extraFree') ") or die(mysql_error());

        }

    }

}

/////////////////////////
Any kind of help would be great.
midjam
Forum Newbie
Posts: 2
Joined: Wed Sep 22, 2010 4:40 am

Re: Need help with foreach loop

Post by midjam »

this is the repeat region if it helps.

Code: Select all

      <table width="100%" border="0" cellspacing="6" cellpadding="6">
        <tr>
          <td width="6%"><img src="../images/foodimages/<?php echo $row_rs_fooditem['foodItemImage']; ?>" width="250" height="150" /></td>
          <td width="94%"><?php do { ?>
              <table width="200" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td><input <?php if (!(strcmp($row_rs_addedextras['extraFree'],true))) {echo "checked=\"checked\"";} ?> type="checkbox" name="checkbox[<?php echo $row_rs_addedextras['idextra']; ?>]" id="checkbox" />
                    <label for="checkbox[]"></label></td>
                  <td><?php echo $row_rs_addedextras['extraName'];?></td>
                  <td>£ <?php echo $row_rs_addedextras['extraPrice']; ?></td>
                </tr>
                <tr>
                  <td colspan="3">
                    <input name="hextraName[<?php echo $row_rs_addedextras['extraName']; ?>]" type="text" id="hextraName[]" value="<?php echo $row_rs_addedextras['extraName']; ?>" />
                    <label for="hextraPrice[]"></label>
                    <input name="hextraPrice[<?php echo $row_rs_addedextras['extraPrice']; ?>]" type="text" id="hextraPrice[]" value="<?php echo $row_rs_addedextras['extraPrice']; ?>" />                    
                    <input name="hextraFree[<?php echo $row_rs_addedextras['extraFree']; ?>]" type="text" id="hextraFree[]" value="<?php echo $row_rs_addedextras['extraFree']; ?>" /></td>
                  </tr>
              </table>
              <?php } while ($row_rs_addedextras = mysql_fetch_assoc($rs_addedextras)); ?>
              </td>
        </tr>
      </table>
Post Reply