Forum Moderators: coopster

Message Too Old, No Replies

calculation inside array

php array calculation

         

jhonnhy

3:20 am on Dec 9, 2006 (gmt 0)

10+ Year Member



Hello I'm new to php and been reading, on how to find a solution to my problem, even searching on google but haven't found an explenation

what I'm trying to do is to add up values that are in an array, I can do this with javascript but the total that I get with javascript can't be transfered to php, so this is the code I need, to let the user choose
any of the checkboxes, this will add up the values display them onscreen then the user pays this total,


while ($row = mysql_fetch_array($result1)){

echo "<tr>";
echo "<th>";echo $_SESSION['uname'] = ($row['uname']); echo "</th>";
echo "<th>";echo $_SESSION['ID'] = ($row['ID']); echo "</th>";
echo "<th>";echo $_SESSION['WOrder'] = ($row['WOrder']); echo "</th>";
echo "<th>"; echo $_SESSION['BName'] = ($row['BName']);
echo "</th>";
echo "<th>"; echo $_SESSION['FReport'] = ($row['FReport']); echo "</th>";
echo "<th>"; echo $_SESSION['amount'] = ($row['amount']); echo"</th>";

echo "<th><input type='text' name='val' value='$amount' size='7'></th>";

echo "<th><input type='checkbox' name='num[]' value='$amount'></th>";
}

//this here would be what I'm trying to do

if (num == checked)
{
$total1 = ($total1 + $amount);
}
else{
$total1 = 0;
}

I get nothing out of ten checkboxes

omoutop

7:39 am on Dec 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to webmasterworld.

Php is run on the server. You can't show up a total sum to user instantly. You must process the maths in server, then return the total to the user's browser.

Javascript can show the total to user the way you need.

On the process page/script, you must recalculate the total, for the php to use it in whatever manner you wish (database storage, comparison, etc).

mcibor

10:19 pm on Dec 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can do this with javascript but the total that I get with javascript can't be transfered to php

Who told you that?

Just perform calculations with javascript and send them to
<input type="hidden" name="total" id="total" value="0">

and in php check:

if(isset($_POST['num'])) $sum = array_sum [php.net]($_POST['num']);

Regards
Michal

And welcome to WebmasterWorld!