1,105,375 Community Members

PHP file update

Member Avatar
drogba123
Junior Poster
170 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
veedeoo
Veteran Poster
1,016 posts since Oct 2011
Reputation Points: 314 [?]
Q&As Helped to Solve: 166 [?]
Skill Endorsements: 17 [?]
 
0
 

Hi,

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

Member Avatar
drogba123
Junior Poster
170 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
veedeoo
Veteran Poster
1,016 posts since Oct 2011
Reputation Points: 314 [?]
Q&As Helped to Solve: 166 [?]
Skill Endorsements: 17 [?]
 
0
 

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..

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article