Sinisterly
[Need Help] PHP runsum formula - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: PHP (https://sinister.ly/Forum-PHP)
+--- Thread: [Need Help] PHP runsum formula (/Thread-Need-Help-PHP-runsum-formula)

Pages: 1 2


[Need Help] PHP runsum formula - YellowKnife - 03-11-2014

Hi Guys,

I have a Runsum formula to calculate the total of a row of employee hours.

How do I create another total for the next row, as currently It wont let me have two Runsums on the same page.

Thanks,
Yellowknife.


RE: [Need Help] PHP runsum formula - 0xDEAD10CC - 03-22-2014

This explains about as much as an accountant could explain about biology; nothing. What's this algorithm? How are you displaying data on the page? What do you mean exactly by "row" are you using SQL, or row as in a <table> row?


RE: [Need Help] PHP runsum formula - TryhardHusky - 04-15-2014

I'm going to go ahead and assume he is referring to a mysql row.

You can do something like this...

Code:
//after connecting to your database.... $query = mysql_query("SELECT * FROM employees"); $hours = 0; while($data = mysql_fetch_assoc($query)){ $hours = $hours + $data['hours-worked']; } echo "Your employees have worked ".$hours." hours.";


Edit: Im not too sure what runsum is, but lets hope this helps.


RE: [Need Help] PHP runsum formula - 0xDEAD10CC - 04-15-2014

(04-15-2014, 12:15 AM)TryhardHusky Wrote: I'm going to go ahead and assume he is referring to a mysql row.

You can do something like this...

Code:
//after connecting to your database.... $query = mysql_query("SELECT * FROM employees"); $hours = 0; while($data = mysql_fetch_assoc($query)){ $hours = $hours + $data['hours-worked']; } echo "Your employees have worked ".$hours." hours.";


Edit: Im not too sure what runsum is, but lets hope this helps.

Only you should be using mysqli instead of mysql..

You can also use += instead of what you're writing to add the hours to the existing value. In addition:
Code:
echo "Your employees have worked {$hours} hours.";

You're using double quotes, you know the variable can be within the string.


RE: [Need Help] PHP runsum formula - TryhardHusky - 04-15-2014

After a quick trip to php.net, I now understand the difference between mysql and mysqli.

Updated code using @0xDEAD10CC 's advice.

Code:
//after connecting to your database.... $query = mysqli_query("SELECT * FROM employees"); $hours = 0; while($data = mysqli_fetch_assoc($query)){ $hours += $data['hours-worked']; } echo "Your employees have worked {$hours} hours.";



RE: [Need Help] PHP runsum formula - 0xDEAD10CC - 04-15-2014

Its good to surround the variables with {} to allow them to be parsed easier within double quotted strings. Theres other reasons for arrays too.


RE: [Need Help] PHP runsum formula - TryhardHusky - 04-16-2014

(04-15-2014, 03:06 PM)0xDEAD10CC Wrote: Its good to surround the variables with {} to allow them to be parsed easier within double quotted strings. Theres other reasons for arrays too.

Luckily we are not using arrays.
Thanks for the advice though.


RE: [Need Help] PHP runsum formula - 0xDEAD10CC - 04-16-2014

(04-16-2014, 01:07 AM)TryhardHusky Wrote: Luckily we are not using arrays.
Thanks for the advice though.

It still applies to regular variables:
Code:
$some_string = "Contents: {$other_var}";

As much as it applies to array indexing:
Code:
$some_string = "Contents: {$other_var['key1']}";



RE: [Need Help] PHP runsum formula - TryhardHusky - 04-16-2014

(04-16-2014, 01:17 AM)0xDEAD10CC Wrote: It still applies to regular variables:
Code:
$some_string = "Contents: {$other_var}";

As much as it applies to array indexing:
Code:
$some_string = "Contents: {$other_var['key1']}";

Okay, Well since it bothers you so much, Ill update the code for you.


RE: [Need Help] PHP runsum formula - 0xDEAD10CC - 04-16-2014

(04-16-2014, 01:18 AM)TryhardHusky Wrote: Okay, Well since it bothers you so much, Ill update the code for you.

It doesn't bother me, I'm just informing you on things that could improve your code. You didn't understand me the first time if you thought I was implying that it only pertained to arrays...

Since OP still hasn't responded to his own thread, I don't know how he expects us to help him though.