vendredi 27 juin 2014

How can I change a PHP variable with an input form?


Vote count:

0




I have sets of arrays that each have a value attributed to them. This value goes through a function to find a percentage then it is displayed on the page. Here's a snippet of that code:



<?php
$balls = array(
'blue' => 4,
'red' => 13,
'green' => 5
);

function percent($array, $element)
{
$total = array_sum($array);
$elementValue = $array[$element];
return ($elementValue / $total) * 100;
}
?>

<html>
<body>
Blue balls per set:<?php echo $balls["blue"]; ?>.
Percentage of blue balls:<?php echo percent($balls, 'blue'); ?>%.
</body>
</html>


How do I add an input form that could change this equation around. Specifically, I want to be able to add a variable ($sets) that would multiply the values of the array items before they are summed up.


The closest thing I can think of is something like this:



$sets = value from input form, if no value entered, default as 1

$balls = array(
'blue' => 4 * $sets,
'red' => 13 * $sets,
'green' => 5 * $sets
);


The rest of the equation would work as usual, but be updated with the new value from $sets as submitted in the input form. Also, would such a thing update the numbers displayed on the webpage instantly?



asked 24 secs ago






Aucun commentaire:

Enregistrer un commentaire