I have a homework assignment that I am stuck on. The program requirements are: Use a FOR loop to read all records of a .txt file and include the necessary processing to calculate the total income, the average income and the number of days with no income (the .txt file contains 7 records:
110.00
0.00
140.00
180.00
0.00
198.00
246.00
So I have the total income and the average income working correctly (i.e $totalIncome = adding the 7 records of the .txt file together and $avgDailyIncome = $totalIncome/7).
I am stuck on how to read the .txt file, find all 0.00 values and add all the records that contain 0.00 so that print(“
NUMBER OF DAYS with NO INCOME: $badDays.
”); with $badDays being 2 using the 7 records listed above. Here is my code so far. All I need to know how to add the code that will add up all “badDays” (i.e records that are 0.00) so that I can add that value to the variable $badDays and use the variable $badDays in my last print statement. Anyway, here’s the code: Weekly ReportWeekly Report
<?php
$paintFile = fopen("weeklyData.txt","r");
$total = 0;
for ($count = 1; $count <= 7; $count = $count + 1)
{
$dailyIncome = fgets($paintFile);
$total = $total + $dailyIncome;
}
fclose($paintFile);
$avgDailyIncome = $total / 7;
print("<p>TOTAL INCOME FROM PAINT CONTRACTS: ");
print("$".number_format($total, 2)."</p>");
print("<p>AVG DAILY INCOME FROM PAINT CONTRACTS: ");
print("$".number_format($avgDailyIncome, 2)."</p>");
print("<p>NUMBER OF DAYS with NO INCOME: $badDays.</p>");
?>