Jump to content

missing line from my .txt file


bscotyb

Recommended Posts

i am trying to pull a .txt file for a class assignment and it is pulling everything except for line 1 any suggestions would be great

 

html:

 

<html>

<head>

<title>Weekly Report</title>

<link rel ="stylesheet" type="text/css" href="sample.css" />

</head>

 

<body>

<h1>Weekly Report</h1>

<p>

<form action = "weeklyReport1Boles.php" method = "post" >

<p>

<input type = "submit" value = "Display the Report" />

</p>

</form>

</body>

</html>

 

txt.file

1  236.00

2  284.00

3  148.00

4  128.00

5  0.00

6  110.00

7  0.00

8

 

php file:

 

<?php

 

$total = $_POST['total'];

$avgDailyIncome = $_POST['avgDailyIncome'];

$badDays = $_POST ['badDays'];

$paintFile = $_POST ['paintfile'];

$nextDay = $_POST['nextDay'];

 

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

$week = fgets($paintFile);

$total = 0;

 

for ($count = 1; $count <=8; $count = $count +1)

{     

$nextDay = fgets($paintFile);

$total = $total + $nextDay;

}

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

 

?>

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/261045-missing-line-from-my-txt-file/
Share on other sites

You're not getting your first line because fgets() reads and entire line, then moves the line pointer ahead one. So this line:

$week = fgets($paintFile);

reads the first line of your text file, doesn't do anything with it, and moves the pointer ahead one. So when you encounter this line:

$nextDay = fgets($paintFile);

the line pointer is already on line 2, which you are using. And I'm not sure this code is doing what you want it to. Since fgets() reads and entire line, you are adding up your line numbers and not the dollar amounts.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.