Foreach help for a noob

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.
[php]// 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());



} [/php]

this is the repeat region table if it helps.


<?php do { ?>

<input <?php if (!(strcmp($row_rs_addedextras['extraFree'],true))) {echo "checked=\"checked\"";} ?> type=“checkbox” name=“checkbox[<?php echo $row_rs_addedextras['idextra']; ?>]” id=“checkbox” />
<?php echo $row_rs_addedextras['extraName'];?> £ <?php echo $row_rs_addedextras['extraPrice']; ?>

<?php } while ($row_rs_addedextras = mysql_fetch_assoc($rs_addedextras)); ?>

any help would be really great.

Sponsor our Newsletter | Privacy Policy | Terms of Service