• 0

Looping through values to perform a calculation


Question

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:

 

Screen%20Shot%202015-02-27%20at%2013.28.

 

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:

    $query_bc_income_week = Booking::ofClub('Breakfast Club')->term()->get(); // query returns children based on Breakfast Club and which term, the important thing to note is that this works and returns rows.
    $days = Config::get('config.dates'); // array

    $amount = 0;
    foreach($query_bc_income_week as $child)
    {
      foreach($days as $day)
      {
        $day = strtolower($day);
        if($child->$day == 1)
        {
          $bc_income_week = ($amount + 0.50);
        }
      }
      echo $bc_income_week;

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

  • 0

In the code you posted, you're never incrementing $amount. You're adding to it, but you're never assigning the result of the addition to $amount so it's never updated.

 

So to fix your code, you should probably initialize $amount inside the child loop, so that every child starts at 0, then just you just need to add the assignment statement in there.

 

Also, I'm not sure what the difference is between $amount and $bc_income_week. I am making the assumption that $amount is the individual child cost, and $bc_income_week is the total cost for all children...

$bc_income_week = 0;
 
foreach($query_bc_income_week as $child)
{
    $amount = 0;

    foreach($days as $day)
    {
        $day = strtolower($day);

        if ($child->$day == 1)
        {
            $amount += 0.50;
        }
    }
 
    $bc_income_week += $amount;
    echo "Child's cost: " + $amount;
}
 
echo "Total cost for all children: " + $bc_income_week;

I'm extremely rusty with PHP, so I apologize if the syntax is wrong.

 

As for the data model question, I don't think anyone would be able to answer that without more knowledge of the problem scope. Designing good data models is an entire field of study/focus for some developers/architects, so it's not a trivial problem to solve.

Link to comment
Share on other sites

  • 0

In the code you posted, you're never incrementing $amount. You're adding to it, but you're never assigning the result of the addition to $amount so it's never updated.

 

So to fix your code, you should probably initialize $amount inside the child loop, so that every child starts at 0, then just you just need to add the assignment statement in there.

 

Also, I'm not sure what the difference is between $amount and $bc_income_week. I am making the assumption that $amount is the individual child cost, and $bc_income_week is the total cost for all children...

$bc_income_week = 0;
 
foreach($query_bc_income_week as $child)
{
    $amount = 0;

    foreach($days as $day)
    {
        $day = strtolower($day);

        if ($child->$day == 1)
        {
            $amount += 0.50;
        }
    }
 
    $bc_income_week += $amount;
    echo "Child's cost: " + $amount;
}
 
echo "Total cost for all children: " + $bc_income_week;

I'm extremely rusty with PHP, so I apologize if the syntax is wrong.

 

As for the data model question, I don't think anyone would be able to answer that without more knowledge of the problem scope. Designing good data models is an entire field of study/focus for some developers/architects, so it's not a trivial problem to solve.

 

This works perfect thanks, I know it's not the best way to do it but I can throw it in a function to avoid repeating it.

 

thank you :)

Link to comment
Share on other sites

This topic is now closed to further replies.