Forum Moderators: coopster
I have a simple PHP task. The end user on the web site has a list of 12 checkboxes on the site that they can click to choose products.
I am displaying the choices using a "foreach" statement. But in addition, I would like the script fo look at home many boxes were checked and add them up, multiply by $1000 and display the total.
This is what I have but it's obviously not working:
$page = $_POST_['page'];
foreach($_POST['page'] as $pagecost) {
$pagecost = 1;
};
$pagecost2 = $pagecost * 1000;
Can you tell me where I am going wrong? No matter how many boxes I check, the total is showing as $1000.
Thanks!
Pat
IN effect, you are saying
for each page
{ pagecost = 1 }
There could be a thousand pages. Each and every one sets the value to 1, over and over again.
Do it a million times, and pagecost = 1.
Multiply that by $1000 and you get a result of $1000 every time.
What you are TRYING to do in INCREMENT pagecost.
In C or C++, for each page { pagecost ++; }
Don't forget to define pagecost as an integer, and set it to ZERO before you perform the loop.
- Larry
$page = $_POST['page'];
$total = 0;
foreach($page as $pagecost) {
if ($pagecost == 1)
{
$total++;
}
};
$pagecost2 = $total * 1000;
You can not assign a value of 1 to $pagecost as it is set to the next value of the $page array during each iteration of the foreach loop. So it's value will always be 1 and hence your result of 1000 for $pagecost2.
Arjan
PS: Larry was just ahead of me in answering you question, what het says is right and the result is in my code above
$sectiontotal = 0;
foreach($page as $pagecost) {
if ($pagecost == 1)
{
$sectiontotal++;
}
};
$pagecost2 = $sectiontotal * 1000;
but the display is showing $0 no matter how many boxes I check, almost as if the "foreach" command is being skipped and the code is going right from "$sectiontotal = 0; to the math, resulting in a 0-sum.
ANy thoughts would be appreciated.
Thanks,
Pat