Hi guys,

How can i read a specific line of the text file for example:

user1 : johnson
number of parts1 : 5
number of parts2 : 10

user2 : andy
number of parts1 : 10
number of parts2 : 10

and i want to get the total number of parts1 and parts2 from different users altogether. Here is my code:

Code blocks are created by indenting at least 4 spaces
... and can span multiple lines

if(file_exists($file)) {
$f = fopen($file, 'r');
while(!feof($f)) {
   $line = fgets($f);      
   $oldData = $oldData . $line;
   }
   $oldData = $oldData . "\r";
   fclose($f);
}

Thanks.

Recommended Answers

All 3 Replies

Hi,

You can read Christopher's tutorial about flat file Here. The guy is really good in flat file manipulation. A very unique approach.

How do i get the int and sum them up together?

Code blocks are created by indenting at least 4 spaces
... and can span multiple lines
$result = 0;
$result += (int)$part1;

I am trying to get the number of parts1 from session in my php code. but when i print to text file, it shows the string which is 1111 not 4

Hi,

Is there any way for your script to write the text file like this?

user1|johnson | number of parts1| 5 | number of parts2 | 10
user2 | andy | number of parts1 | 10 |number of parts2 | 10

The reason I am asking is that the script would read it line by line. If we can set it up just like above. It would be very easy for you to calculate, because php will read those items as an array. Something like this

 [user] =>array
          [0] => user1
          [1] => johnson
          [2] => number of parts1
          [3] => 5
          [4] => number of parts2
          [5] => 10
  [user] => array
          [0] => user2
          [1] => andy
          [2] => number of parts1
          [3] => 10
          [4] => number of parts2
          [5] => 10

So, with that arrangement on the flat file, we can easily explode the |, and then we can loop through the lines using for loop. That should give us a difinitive values on every items

For example if want to calculate on the total of parts1 and parts2 for the first user, it will be just as simple as $user[3] + $user[5];

Let me know if you are willing to change the formatting of your text file, so that I can help you..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.