- 0
php Looping through values to perform a calculation
-
Recently Browsing 0 members
- No registered users viewing this page.
-
Similar Content
-
- 3 answers
- 3,228 views
-
QNAP issues patch for an RCE security vulnerability affecting PHP in NAS Drive management
By Alap Naik Desai,
- remote code execution
- rce
- (and 12 more)
- 0 replies
- 6 views
-
Get this Dynamic 2022 Data & IT Security Training Bundle at 97% off
By News Staff,
- neowin deals
- cyber security
- (and 4 more)
- 0 replies
- 6 views
-
Save 98% off this 27 Course Premium Learn to Code Certification Bundle
By News Staff,
- neowin deals
- coding
- (and 10 more)
- 0 replies
- 9 views
-
Get the Complete 2021 Superstar Backend Developer Bundle for only $39.99
By News Staff,
- neowin deals
- sponsored
- (and 7 more)
- 0 replies
- 321 views
-
Question
game_over
I have developed a web app that logs children attending a club using PHP and have been asked to add a finance module that calculates how much each member owes based on days they are attending... off the top of my head the only way to do it is to create a query that pulls children based on certain criteria and then loop through the fields to decide whether to charge them.
Here is some sample data:
The important part for now is the days they are attending, if a child is attending monday then the cell holds 1 integer, i the child is not attending on a specified day then it holds a zero.
Here is my god awful code that doesn't even work:
So after the query i put the days (mon-fri) into an array and then set the $amount to 0.
Then i use a foreach to grab each child which can now be accessed via $child->column (i.e. $child->monday)
I have a nested foreach that loops through each day and assigned it to the column.
In English: the for each selects the child and then the second loop selects the day, if the $child->day column holds 1 then add 0.50 to the amount, after it has looped through 5 days(from the array) it will go back to the next child and do the same for the days here, each time incrementing by 0.50 every time it finds a 1.
$bc_income_week returns: 0.50.50.50.50.50.50.50.50.50.50.50.50.50.50.50.50.50.5
Sorry for my poor explanation, hope it makes sense, surely there must be a better way to achieve this? It is possible my table structure is the real problem but couldn't think of a easier way to access the data.
This is just one calculation that I need to perform, I also need to get the income based on week, term and for both clubs, that is a lot of queries and loops.
Any ideas how I can do this better?
Link to comment
Share on other sites
3 answers to this question
Recommended Posts