Forum Moderators: coopster

Message Too Old, No Replies

Simple PHP Math issue

...should be easier than I am making it

         

mcjohnson

9:06 am on May 4, 2006 (gmt 0)

10+ Year Member



Hi folks;

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

larryhatch

9:16 am on May 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know PHP, C language instead, and maybe I see the problem.

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

adb64

9:21 am on May 4, 2006 (gmt 0)

10+ Year Member



Assuming that the value of the checkboxes is stored in array with the name '$page' and a size of 12, the following code should do the trick for you:


$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

mcjohnson

11:37 am on May 4, 2006 (gmt 0)

10+ Year Member



Hmmm..something is still not working. I tweak the names of the variables for my use a bit, as such:

$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

adb64

11:57 am on May 4, 2006 (gmt 0)

10+ Year Member



Did you set $page equal to $_POST['page']?

mcjohnson

12:06 pm on May 4, 2006 (gmt 0)

10+ Year Member



yes - in fact, it's receiving the array from the post ok, and displaying the actual choices that are checked, but the math calcuolations are what seem to be not working. Would it help if I posted the entire code, or would that be too bulky?

Pat

dreamcatcher

12:16 pm on May 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If your checkbox data is coming in as:

$_POST_['page'];

Then use count().

$total = count($_POST['page'])*1000;

echo $total;

I assume you are setting the checkbox as an array? ie:

<input type="checkbox" name="page[]" value="">

dc

mcjohnson

12:30 pm on May 4, 2006 (gmt 0)

10+ Year Member



That did it! Thank you Dreamcatcher. Thanks to all. I am sure I'll have a few more questions - you all rock!

Par