Foreach and adding

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
AlbinoJellyfish
Forum Commoner
Posts: 76
Joined: Sun Apr 04, 2004 7:39 pm

Foreach and adding

Post by AlbinoJellyfish »

I have a script that adds checkboxes values to an array, and I can print them out using

Code: Select all

<?php
foreach($_POST['array'] as $key => $value) {
echo $value."<br />";
 }
?>
Each checkbox corresponds to a product, and then adds it to the array, and when i print the array on the second page, how do I get the values separated so I can put the prices on?

Suppose I check Calea. On the post page, i include a file with a bunch of variables, and Calea is assigned the price of 5. How do I see if calea is in the array, and then add 5 dollars to a sum if it is?
Last edited by AlbinoJellyfish on Thu Sep 23, 2004 9:59 pm, edited 2 times in total.
AlbinoJellyfish
Forum Commoner
Posts: 76
Joined: Sun Apr 04, 2004 7:39 pm

Post by AlbinoJellyfish »

my check boxes look like this:

Code: Select all

&lt;input name="array&#1111;]" type="checkbox" id="CALEA" value="CALEA"&gt;
prices:

Code: Select all

<?php
$R2 = "5";
$GR303 = "10";
$IDSN-PRI = "40";
$V5 = "60";
$TR08 = "100";
$CALEA = "27";
$SS7 = "92";

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

there's a wonder function called [php_man]in_array[/php_man] :roll:

I'd strongly suggest storing those prices in an array, it's easier to access.
AlbinoJellyfish
Forum Commoner
Posts: 76
Joined: Sun Apr 04, 2004 7:39 pm

Post by AlbinoJellyfish »

Thanks, this is my first time using arrays and couldnt find that. Those prices will soon be stored in a mysql database, im just getting the code for the order form down.
Post Reply