DiigIT | IT Community
No Profile Image
Welcome Guest
New User? Register | Login

Total Combo boxes

By: | 18 Feb 2009 4:25 pm

Hi,

how can u have Values assigned to each of those selections and have them Sum at a bottom Total ?
<select name="division">
<option value="12.75" label="Math">Math</option>
<option value="15.25" label="Arts">Arts</option>

<option value="8.95" label="Science">Science</option>

</select>

<select name="division2">
<option value="12.75" label="Math">Math</option>

<option value="15.25" label="Arts">Arts</option>
<option value="8.95" label="Science">Science</option>

</select>
TOTAL
 

Comments

Are the values in each "option" set in PHP? If so, add it there. I think someone posted a suggestion involving arrays. Along that line, I might do something like this with static values (they could be computed values though).

$div[1] = array("Math"=>12.75, "Arts"=>15.25, "Science"=>8.95);
$div[2] = array("Math"=>12.75, "Arts"=>15.25, "Science"=>8.95);

$fmt = " <option value=`%s` label=`%s`>%s</option>n";
$tot = array();

# loop through divisions
foreach ($div as $div_num=>$div_array)
{
# display select list
print "<select name=`division$div_num`>n";
# loop through the $div_array list
foreach ($div_array as $label=>$value)
{
printf($fmt, $value, $label, $label);
}
print "</select>n";
$tot[$div_num] = array_sum($div_array);
}

printf("<pre>%s</pre>", print_r($tot, true));

By: | 18 Feb 2009

Leave a comment

Enter the text in the image
img
Can't read?
Type the characters you see in the picture below.


Close Move