Reputation: 11
Ok, not exactly what I was expecting...I didn't know my code was so ureadable...sorry! What can I do to fix it? I really would like to just accomplish the (what I thought was) simple math to get totals. I've looked everywhere and read so much information on arrays and obviously I am just not grasping the concept...any more help is welcomed and would be GREATLY appreciated!
I’m creating a mock order form that has radio buttons, checkboxes and uses arrays to show the total purchase amount. I have a form that is working except for that I can't get the total amount from the two different arrays i have. $total = $extras + $additional isn't working and honestly, i should have known it couldn’t be that easy! ... Any suggestions on what formula to use so that I can get a total dollar amount of all of the options that are selected? Also, can anyone help me so that checkbox items are listed in a new row, and not a whole new table?
Thanks in advance!
A couple more things: I have to keep this in a redux and would like to keep the output in the table like it is...other than that, feel free to change whatever you want/need.
I’m new to PHP arrays and seem to only be having difficulties when it comes to their values, but since I know how important arrays are in PHP I would like to see how they work!
<?php
/*This stuff is only here because I want to make sure
there are 2 decimal places in the final numbers since
I'm dealing in "money" values*/
$total = number_format ($total,2);
$value = number_format ($value,2);
$additional = number_format ($additional,2);
$value = array("Short Trip"=>15.99, "Long Trip"=>28.99, "Overnight"=>10.99 "Forever"=>99.99);
if(isset($_POST['travel'])) {
$extras = array("Hair Brush"=>1.50, "Shampoo"=>1.50, "Toothpaste"=>1.50,
"Cream Rinse"=>1.50, "Tooth Brush"=>1.50,
"Shower Cap"=>1.50, "Washcloth"=>1.50, "Mouthwash"=>1.50);
if (isset($_POST['extras'])) {
foreach ($_POST['extras'] as $additional) {
echo "<table border =\"2\">
<tr><td>Item</td><td>Charges</td></tr>
<tr><td>".$_POST['travel']."</td>
<td> $".$value[$_POST['travel']]."</td></tr>
<tr>
<td>".$additional."</td>
<td> $".$extras[$additional]."</td>
</tr>
<tr><td>Your total</td> <td>".$total."</td></tr>
</table>";
}
}
}
?>
<html>
<body>
<form action="" method="post">
<table border="2">
<tr>
<td colspan="2" align="center" scope="col">Stay Information</td>
</tr>
<tr>
<td><input type="radio" name="travel" value="Short Trip" />Short trip $15.99</td>
<td><input type="radio" name="travel" value="Long Trip" />Long trip $28.99</td>
</tr>
<tr>
<td><input type="radio" name="travel" value="Overnight" />Overnight $10.99</td>
<td><input type="radio" name="travel" value="Forever" />Forever $99.99</td>
</tr>
</table>
<table border="2">
<tr>
<td colspan="2" scope="col">What will you need?($1.50 each)</td>
</tr>
<tr>
<td><input type="checkbox" name="extras[]" value="Hair Brush" />Hair Brush</td>
<td><input type="checkbox" name="extras[]" value="Shampoo" />Shampoo</td></tr>
<tr>
<tr><td><input type="checkbox" name="extras[]" value="Toothpaste" />Toothpaste</td>
<td><input type="checkbox" name="extras[]" value="Cream Rinse" />Cream Rinse</td></tr>
</tr>
<tr>
<td><input type="checkbox" name="extras[]" value="Tooth Brush" />Tooth Brush</td>
<td><input type="checkbox" name="extras[]" value="Shower Cap" />Shower Cap</td></tr>
<tr>
<tr><td><input type="checkbox" name="extras[]" value="Washcloth" />Washcloth</td>
<td><input type="checkbox" name="extras[]" value="Mouthwash" />Mouthwash</td></tr>
</tr>
<tr><td colspan="2">
<input type="submit" value="Submit"></td></tr>
</table>
</form>
</body>
</html>
Upvotes: 1
Views: 3035
Reputation: 4448
The comments have pointed out some issues, the main one being the formatting of your code. Indeed, when trying to figure out what one did wrong in a script, confusing formatting can add hours of wasted time.
The first thing you might notice is that your $value
array is missing a comma.
$value = array("Short Trip"=>15.99, "Long Trip"=>28.99, "Overnight"=>10.99 "Forever"=>99.99)
// comma here -----------------------------------------------------------^
Formatting is to some degree a matter of style, but the main point is readability, so that you can more easily catch mistakes like this.
Here is a condensed version of something what your script might look like:
<?php
$value = array(
"Short Trip" => 15.99,
"Long Trip" => 28.99,
"Overnight" => 10.99,
"Forever" => 99.99
);
$extras = array(
"Hair Brush" => 1.50,
"Shampoo" => 1.50,
"Toothpaste" => 1.50,
"Cream Rinse" => 1.50,
"Tooth Brush" => 1.50,
"Shower Cap" => 1.50,
"Washcloth" => 1.50,
"Mouthwash" => 1.50
);
// combine condititions
if (isset($_POST['travel']) && isset($_POST['extras'])) {
$total = $value[$_POST['travel']];
// start table html (before foreach loop)
// store html in a variable to print later
$html = "<table border =\"2\">
<tr>
<td>Item</td>
<td>Charges</td>
</tr>
<tr>
<td>" . $_POST['travel'] . "</td>
<td> $" . $total . "</td>
</tr>";
foreach ($_POST['extras'] as $additional) {
// add a row per extra
$html .= "<tr>
<td>" . $additional . "</td>
<td> $" . $extras[$additional] . "</td>
</tr>";
// increment total
$total += $extras[$additional];
}
$html .= "<tr>
<td>Your total</td>
<td>" . $total . "</td>
</tr>
</table>";
}
?>
<html>
<body>
<form action="" method="post">
<?php
if (isset($html)) {
echo $html;
}
?>
<table border="2">
.....
There may be further issues, as I'm not clear on which part you're having trouble, but they will now be much easier to debug.
Upvotes: 2
Reputation: 1
Impressive code Doug. I hate to tell you, but I think there may be one more error. I can't figure out why, but if anyone else runs this code they would see that the math is only performed on his $1.50 "additional" items-- There isn't a working collective total.
Upvotes: 0