Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-22-2011, 10:51 PM   PM User | #1
goop
New to the CF scene

 
Join Date: Nov 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
goop is an unknown quantity at this point
FOR Loop

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!
goop is offline   Reply With Quote
Old 11-22-2011, 11:19 PM   PM User | #2
Adee
Regular Coder

 
Join Date: Jul 2010
Location: Oregon City
Posts: 280
Thanks: 5
Thanked 50 Times in 49 Posts
Adee can only hope to improve
PHP Code:
$k file("weeklyData.txt");

$total 0;
$badDays 0;

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


$avgDailyIncome $total count($k); 
Adee is offline   Reply With Quote
Old 11-22-2011, 11:41 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,640
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You can do this without looping:
PHP Code:
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.
Fou-Lu is offline   Reply With Quote
Old 11-27-2011, 12:19 AM   PM User | #4
goop
New to the CF scene

 
Join Date: Nov 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
goop is an unknown quantity at this point
Loop

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

Last edited by goop; 11-27-2011 at 12:33 AM.. Reason: Got it to run, but needed additional help.
goop is offline   Reply With Quote
Old 11-27-2011, 02:17 AM   PM User | #5
goop
New to the CF scene

 
Join Date: Nov 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
goop is an unknown quantity at this point
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 is offline   Reply With Quote
Old 11-27-2011, 02:33 AM   PM User | #6
Adee
Regular Coder

 
Join Date: Jul 2010
Location: Oregon City
Posts: 280
Thanks: 5
Thanked 50 Times in 49 Posts
Adee can only hope to improve
Quote:
Originally Posted by goop View Post
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

PHP Code:
$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 />";

Adee is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:04 PM.


Advertisement
Log in to turn off these ads.