...

View Full Version : FOR Loop



goop
11-22-2011, 10:51 PM
Could some one help me with the following?


.txt file contains:

236.00
284.00
148.00
128.00
0.00
110.00
0.00

php that needs to be amended:

<html>
<head>
<title>Weekly Report</title>
<link rel ="stylesheet" type="text/css" href="sample.css" />
</head>

<body>
<h1>Weekly Report</h1>

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



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

?>

</body>
</html>

I need to amend this php to use a FOR loop to read the 7 lines from the file and add the total income, the average income and the number of days with no income. Can anyone help me on this? I am soooo lost! Thanks!

Adee
11-22-2011, 11:19 PM
$k = file("weeklyData.txt");

$total = 0;
$badDays = 0;

foreach($k as $data)
{
if($data == 0.00)
{
$badDays++;
}
else
{
$total+=$data;
}

}


$avgDailyIncome = $total / count($k);

Fou-Lu
11-22-2011, 11:41 PM
You can do this without looping:


function zeroDays($item)
{
return $item <= 0.00;
}

$dSum = array_sum($paintFile);
$dAvg = $dSum / count($paintFile);
$iDaysZero = count(array_filter($paintFile, 'zeroDays'));

printf('Sum: $%0.2f<br />', $dSum);
printf('AVG: $%0.2f<br />', $dAvg);
printf('0 days: %d<br />', $iDaysZero);

Also, use money_format if you have it available, as its locale aware.

goop
11-27-2011, 12:19 AM
Thanks Adee, I just got this to work! Yay!!! Now if I needed to show each line as Monday through Sunday, how would I do that, with the first line starting out as Monday and Showing income for each day of the week?

Thanks,

Goop

goop
11-27-2011, 02:17 AM
I just got this to work! Yay!!! Now if I needed to show each line as Monday through Sunday, how would I do that, with the first line starting out as Monday and Showing income for each day of the week?

Thanks,

Goop

Adee
11-27-2011, 02:33 AM
I just got this to work! Yay!!! Now if I needed to show each line as Monday through Sunday, how would I do that, with the first line starting out as Monday and Showing income for each day of the week?

Thanks,

Goop

can make an array



$days = array('sunday','monday','tuesday','wednesday','thursday','friday','saturday');

$data = file('weeklyData.txt');

for($i=0;$i<(count($data));$i++)
{
echo $days[$i] . ": " . $data[$i] . "<br />";
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum