Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: FOR Loop

  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    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); 

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #4
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #5
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Regular Coder
    Join Date
    Jul 2010
    Location
    Oregon City
    Posts
    280
    Thanks
    5
    Thanked 50 Times in 49 Posts
    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 />";



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •