Firestarter30
asked on
How do i sum a variable in php?
The variable is in fact a pseudo column that contains a mathematical formula as follows
I made the query without sum inside it cause was creating big precision problems.So i thought to let php to make the sum , while pulling the data from the mysql query.
For example the variable in php to be like this :
$i1 = SUM('$row_i_totals[kinisis _1_total]' );
echo $i1
But i dont know the syntax to output the correct sum in php , cause this is a string and have no idea how to format it. Preferably a decimal number cause it outputs money value.
Can you tell me pls how to do it?
Thank you :)
SELECT (((kinisis_1c + kinisis_1e + ((kinisis_1c / kinisis_1b) * kinisis_1a))/(1 + fpa)) * 1.005) AS kinisis_1_total, (((100c + 100e + ((100c / 100b)* 100a)) / (1 + fpa)) *1.005) AS 100ara_total, (((super_1c + super_1e + ((super_1c / super_1b)* super_1a)) / (1 + fpa)) *1.005) AS super_1_total, (((95_1c + 95_1e + ((95_1c / 95_1b)* 95_1a)) / (1 + fpa)) *1.005) AS 95_1_total, (((95_2c + 95_2e + ((95_2c / 95_2b)* 95_2a)) / (1 + fpa)) *1.005) AS 95_2_total, (((super_2c + super_2e + ((super_2c / super_2b) * super_2a)) / (1 + fpa)) *1.005) AS super_2_total, (((kinisis_2c + kinisis_2e + ((kinisis_2c / kinisis_2b) * kinisis_2a)) / (1 + fpa)) *1.005) AS kinisis_2_total
FROM kiniseis
WHERE `date` BETWEEN '$_GET[date1]' AND '$_GET[date2]'
I made the query without sum inside it cause was creating big precision problems.So i thought to let php to make the sum , while pulling the data from the mysql query.
For example the variable in php to be like this :
$i1 = SUM('$row_i_totals[kinisis
echo $i1
But i dont know the syntax to output the correct sum in php , cause this is a string and have no idea how to format it. Preferably a decimal number cause it outputs money value.
Can you tell me pls how to do it?
Thank you :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
or use array_sum: http://www.plus2net.com/php_tutorial/array_sum.php
ASKER
Thank you , i didnt came up to the correct syntax though
This is my precice query : (the recordset name in dreamweaver is i_totals )
$query_i_totals = "SELECT (((kinisis_1c + kinisis_1e + ((kinisis_1c / kinisis_1b) * kinisis_1a))/(1 + fpa)) * 1.005) AS kinisis_1_total, (((100c + 100e + ((100c / 100b)* 100a)) / (1 + fpa)) *1.005) AS 100ara_total, (((super_1c + super_1e + ((super_1c / super_1b)* super_1a)) / (1 + fpa)) *1.005) AS super_1_total, (((95_1c + 95_1e + ((95_1c / 95_1b)* 95_1a)) / (1 + fpa)) *1.005) AS 95_1_total, (((95_2c + 95_2e + ((95_2c / 95_2b)* 95_2a)) / (1 + fpa)) *1.005) AS 95_2_total, (((super_2c + super_2e + ((super_2c / super_2b) * super_2a)) / (1 + fpa)) *1.005) AS super_2_total, (((kinisis_2c + kinisis_2e + ((kinisis_2c / kinisis_2b) * kinisis_2a)) / (1 + fpa)) *1.005) AS kinisis_2_total FROM kiniseis WHERE `date` BETWEEN '$_GET[date1]' AND '$_GET[date2]'";
$i_totals = mysql_query($query_i_total s, $gas_connection) or die(mysql_error());
$row_i_totals = mysql_fetch_assoc($i_total s);
$totalRows_i_totals = mysql_num_rows($i_totals);
I have problem implementing the solution correctly :(
This is my precice query : (the recordset name in dreamweaver is i_totals )
$query_i_totals = "SELECT (((kinisis_1c + kinisis_1e + ((kinisis_1c / kinisis_1b) * kinisis_1a))/(1 + fpa)) * 1.005) AS kinisis_1_total, (((100c + 100e + ((100c / 100b)* 100a)) / (1 + fpa)) *1.005) AS 100ara_total, (((super_1c + super_1e + ((super_1c / super_1b)* super_1a)) / (1 + fpa)) *1.005) AS super_1_total, (((95_1c + 95_1e + ((95_1c / 95_1b)* 95_1a)) / (1 + fpa)) *1.005) AS 95_1_total, (((95_2c + 95_2e + ((95_2c / 95_2b)* 95_2a)) / (1 + fpa)) *1.005) AS 95_2_total, (((super_2c + super_2e + ((super_2c / super_2b) * super_2a)) / (1 + fpa)) *1.005) AS super_2_total, (((kinisis_2c + kinisis_2e + ((kinisis_2c / kinisis_2b) * kinisis_2a)) / (1 + fpa)) *1.005) AS kinisis_2_total FROM kiniseis WHERE `date` BETWEEN '$_GET[date1]' AND '$_GET[date2]'";
$i_totals = mysql_query($query_i_total
$row_i_totals = mysql_fetch_assoc($i_total
$totalRows_i_totals = mysql_num_rows($i_totals);
I have problem implementing the solution correctly :(
ASKER
ok i made it working but still i have big precision problems. The main issue i have is that cause im dividing some cells i have big loss of precision. I used the code below and then i just echoed each variable in php.
$query_i_totals = "SELECT (((kinisis_1c + kinisis_1e + ((kinisis_1c / kinisis_1b) * kinisis_1a))/(1 + fpa)) * 1.005) AS kinisis_1_total, (((100c + 100e + ((100c / 100b)* 100a)) / (1 + fpa)) *1.005) AS 100ara_total, (((super_1c + super_1e + ((super_1c / super_1b)* super_1a)) / (1 + fpa)) *1.005) AS super_1_total, (((95_1c + 95_1e + ((95_1c / 95_1b)* 95_1a)) / (1 + fpa)) *1.005) AS 95_1_total, (((95_2c + 95_2e + ((95_2c / 95_2b)* 95_2a)) / (1 + fpa)) *1.005) AS 95_2_total, (((super_2c + super_2e + ((super_2c / super_2b) * super_2a)) / (1 + fpa)) *1.005) AS super_2_total, (((kinisis_2c + kinisis_2e + ((kinisis_2c / kinisis_2b) * kinisis_2a)) / (1 + fpa)) *1.005) AS kinisis_2_total FROM kiniseis WHERE `date` BETWEEN '$_GET[date1]' AND '$_GET[date2]'";
$i_totals = mysql_query($query_i_total s, $gas_connection) or die(mysql_error());
$row_i_totals = mysql_fetch_assoc($i_total s);
$totalRows_i_totals = mysql_num_rows($i_totals);
$i1 = 0;
$i2 = 0;
$i3 = 0;
$i4 = 0;
$i5 = 0;
$i6 = 0;
$i7 = 0;
while ($row_i_totals = mysql_fetch_array($i_total s)) {
$i1 += $row_i_totals['kinisis_1_t otal'];
$i2 += $row_i_totals['100ara_tota l'];
$i3 += $row_i_totals['super_1_tot al'];
$i4 += $row_i_totals['95_1_total' ];
$i5 += $row_i_totals['95_2_total' ];
$i6 += $row_i_totals['super_2_tot al'];
$i7 += $row_i_totals['kinisis_2_t otal'];
}
What is the solution to the precision loss in mysql-php , when someone makes a sum that contains a formula with a division?
This is the main problem i have , and i thought that this work around above that microsoft recommended will work in mysql-php as well , but it didnt :(
http://support.microsoft.com/kb/281341
This code gave me the same precision as i would have the SUM() within the mysql query
Maybe the array_sum is not having this problem but i dont know...
Can you please help me with this is torturing me more than 2 weeks :(
$query_i_totals = "SELECT (((kinisis_1c + kinisis_1e + ((kinisis_1c / kinisis_1b) * kinisis_1a))/(1 + fpa)) * 1.005) AS kinisis_1_total, (((100c + 100e + ((100c / 100b)* 100a)) / (1 + fpa)) *1.005) AS 100ara_total, (((super_1c + super_1e + ((super_1c / super_1b)* super_1a)) / (1 + fpa)) *1.005) AS super_1_total, (((95_1c + 95_1e + ((95_1c / 95_1b)* 95_1a)) / (1 + fpa)) *1.005) AS 95_1_total, (((95_2c + 95_2e + ((95_2c / 95_2b)* 95_2a)) / (1 + fpa)) *1.005) AS 95_2_total, (((super_2c + super_2e + ((super_2c / super_2b) * super_2a)) / (1 + fpa)) *1.005) AS super_2_total, (((kinisis_2c + kinisis_2e + ((kinisis_2c / kinisis_2b) * kinisis_2a)) / (1 + fpa)) *1.005) AS kinisis_2_total FROM kiniseis WHERE `date` BETWEEN '$_GET[date1]' AND '$_GET[date2]'";
$i_totals = mysql_query($query_i_total
$row_i_totals = mysql_fetch_assoc($i_total
$totalRows_i_totals = mysql_num_rows($i_totals);
$i1 = 0;
$i2 = 0;
$i3 = 0;
$i4 = 0;
$i5 = 0;
$i6 = 0;
$i7 = 0;
while ($row_i_totals = mysql_fetch_array($i_total
$i1 += $row_i_totals['kinisis_1_t
$i2 += $row_i_totals['100ara_tota
$i3 += $row_i_totals['super_1_tot
$i4 += $row_i_totals['95_1_total'
$i5 += $row_i_totals['95_2_total'
$i6 += $row_i_totals['super_2_tot
$i7 += $row_i_totals['kinisis_2_t
}
What is the solution to the precision loss in mysql-php , when someone makes a sum that contains a formula with a division?
This is the main problem i have , and i thought that this work around above that microsoft recommended will work in mysql-php as well , but it didnt :(
http://support.microsoft.com/kb/281341
This code gave me the same precision as i would have the SUM() within the mysql query
Maybe the array_sum is not having this problem but i dont know...
Can you please help me with this is torturing me more than 2 weeks :(