Hi,
Well first of all, what errors are you reporting? ...the $itemList line looks dodgy to me (there are "" inside the string). I've turned on full error reporting.
Also, print out the POST variable just to check what's being sent (just get rid of the debug lines once everything's working).
<?php
// Debug: Turn on full error reporting ...
ini_set('error_reporting', E_ALL);
// Debug: make sure the post variables are being sent ...
echo '<hr>POST<pre>'; print_r($_POST); echo '</pre>';
$total = $numtotal = 0;
$itemList = "";
foreach($_POST as $key => $quantity){
// $quantity is the same as $_POST[$key]
if(strstr($key, "quantity")){
$num = str_replace("quantity", "", $key);
if(!isset($_POST['price'.$num])){
die('Error: $_POST[price'.$num.'] is not set.');
} else {
$price = $_POST['price'.$num];
}
if(!isset($_POST['name'.$num])){
die('Error: $_POST[name'.$num.'] is not set.');
} else {
$name = $_POST['name'.$num];
}
$total += $price*$quantity;
$itemList .= "item:\t".$name."\tquantity:\t".$quantity."\tprice each:\t".$price."\n";
$numtotal++;
}
}
if($numtotal == 1){
$shipping = 4.50;
} else if ($numtotal > 1){
$shipping = $total * 0.1;
} else {
// Things are going wrong ...
$shipping = 0;
}
?>
Notice I've got rid of that horrible $itemList line just by cutting in and out of the string ... can see what's going on now.
Then, always use the {} in if statements etc. even if it's trivial ... you can see what's going on, that's all.
I changed these two lines ...
/// Get the key and value at the same time
foreach($_POST as $key => $value){
// the !empty bit is unnecessary ... it won't ever be empty
if(strstr($key, "quantity")){
I haven't looked at the logic ... does it work now?
Paul 🙂
PS. Always put php code inside the [ PHP ][ / PHP ] tags ... makes it easier to read on the forum.