Simple FOR loop problem that is driving me nuts

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 Report

Weekly 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>");

?>

[php]<?php

  $paintFile = fopen("weeklyData.txt","r");

  $file = 'weeklyData.txt';
  $lines = count(file($file));  // Counts how many lines are in text file
  
  $total = 0;
  
  for ($count = 1; $count <= $lines; $count++) // So it doesn't matter how many lines are in the txt file. Its not limited to 7.
  {
     $dailyIncome = fgets($paintFile);
	 if($dailyIncome < 1){ //Check to see if line == 0
		 $badDays=$badDays+1; // Add to the variable if it is.
	 }
	 
     $total = $total + $dailyIncome;
     

        
  }
     fclose($paintFile);
     $avgDailyIncome = $total / $lines;
     
  
  
  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>");

?>[/php]

Hey, try this.

Thanks a lot lothop. That works like a charm :slight_smile: Now I can move on to bigger things.

No problem. If you get stuck again let us know :slight_smile:

Also this is a bit cleaner

[php] if($dailyIncome == 0){
$badDays++;
}[/php]

Instead of
[php] if($dailyIncome < 1){
$badDays=$badDays+1;
}[/php]

And remember to set $badDays = 0; before the FOR loop, otherwise if there are no zero values it will result in DAYS with NO INCOME: . instead of DAYS with NO INCOME: 0.

Sponsor our Newsletter | Privacy Policy | Terms of Service