4

There are quite a few different threads about this similar topic, yet I have not been able to fully comprehend a solution to my problem.

What I'd like to do is quite simple, I have a flat-file db, with data stored like this -

$username:$worldLocation:$resources

The issue is I would like to have a submit data html page that would update this line based upon a search of the term using php

search db for - $worldLocation

if $worldLocation found

replace entire line with $username:$worldLocation:$updatedResources

I know there should be a fairly easy way to get this done but I am unable to figure it out at the moment, I will keep trying as this post is up but if you know a way that I could use I would greatly appreciate the help.

Thank you

Marc B
  • 356,200
  • 43
  • 426
  • 500
user1707616
  • 117
  • 2
  • 9
  • 2
    As usual... why not use a real database? Then read the PHP manual: http://php.net/file http://php.net/implode http://php.net/file_put_contents – Marc B Oct 04 '12 at 18:16
  • This sounds extremely inefficient. – rid Oct 04 '12 at 18:16
  • 2
    It's quite a simple application with maybe only 100 entries. I am writting it to learn a bit more about basic php and was wondering if there was a way to accomplish it. It's probably not the most efficient way but for now I'm simply using an html input page and using php to write the data to a flat-file. – user1707616 Oct 04 '12 at 18:21

5 Answers5

1

I always loved c, and functions that came into php from c.

Check out fscanf and fprintf.

These will make your life easier while reading writing in a format. Like say:

$filehandle = fopen("file.txt", "c");
while($values = fscanf($filehandle, "%s\t%s\t%s\n")){
    list($a, $b, $c) = $values;
    // do something with a,b,c
}

Also, there is no performance workaround for avoiding reading the entire file into memory -> changing one line -> writing the entire file. You have to do it.

This is as efficient as you can get. Because you most probably using native c code since I read some where that php just wraps c's functions in these cases.

Prasanth
  • 5,230
  • 2
  • 29
  • 61
0
  1. You like the hard way so be it....
  2. Make each line the same length. Add space, tab, capital X etc to fill in the blanks
  3. When you want to replace the line, find it and as each line is of a fixed length you can replace it.
  4. For speed and less hassle use a database (even SQLLite)
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

If you're committed to the flat file, the simplest thing is iterating through each line, writing a new file & changing the one that matches.

Yeah, it sucks.

I'd strongly recommend switching over to a 'proper' database. If you're concerned about resources or the complexity of running a server, you can look into SQLite or Berkeley DB. Both of these use a database that is 'just a file', removing the issue of installing and maintaining a DB server, but still you the ability to quickly & easily search, replace and delete individual records. If you still need the flat file for some other reason, you can easily write some import/export routines.

Another interesting possibility, if you want to be creative, would be to look at your filesystem as a database. Give each user a directory. In each directory, have a file for locations. In each file, update the resources. This means that, to insert a row, you just write to a new file. To update a file, you just rewrite a single file. Deleting a user is just nuking a directory. Sure, there's a bit more overhead in slurping the whole thing into memory.

Other ways of solving the problem might be to make your flat-file write-only, since appending to the end of a file is a trivial operation. You then create a second file that lists "dead" line numbers that should be ignored when reading the flat file. Similarly, you could easily "X" out the existing lines (which, again, is far easier than trying to update lines in a file that might not be the same length) and append your new data to the end.

Those second two ideas aren't really meant to be practical solutions as much as they are to show you that there's always more than one way to solve a problem.

Sean McSomething
  • 6,376
  • 2
  • 23
  • 28
0

ok.... after a few hours work..this example woorked fine for me... I intended to code an editing tool...and use it for password update..and it did the trick! Not only does this page send and email to user (sorry...address harcoded to avoid posting aditional code) with new password...but it also edits entry for thew user and re-writes all file info in new file... when done, it obviously swaps filenames, storing old file as usuarios_old.txt.

grab the code here (sorry stackoverflow got VERY picky about code posting)

https://www.iot-argentina.xyz/edit_flat_databse.txt

  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Anton Menshov Apr 11 '20 at 05:46
  • Dont understand your point....my post SOLVES the issue...and I provided link to actual working code (because stackoverflow has turned it too complicated to post...4 spaces indentation..and so on) – user3152171 Apr 12 '20 at 03:24
  • StackOverflow does not work like that. Posting a link to the solution is not acceptable on its own. Please, see the link above. – Anton Menshov Apr 12 '20 at 03:43
  • Use ctrl+K to indent the code. Also, the code is full of non-English comments, which would prevent it from being a helpful answer on English StackOverflow. – Anton Menshov Apr 12 '20 at 03:46
-1

Is that what you are location for :

update `field` from `table` set `field to replace` = '$username:$worldlocation:$updatesResources' where `field` = '$worldLocation';
poudigne
  • 1,694
  • 3
  • 17
  • 40
  • could it be used as if($worldLocationInput == $worlLocation) then update `field` from `table` set `field to replace`.... – user1707616 Oct 04 '12 at 18:31
  • Unfortunately I have only had a basic php class and am trying to learn as I go. Please forgive my odd questions. – user1707616 Oct 04 '12 at 18:36
  • I thought there may be a way to accomplish it with preg_replace or something similar? – user1707616 Oct 04 '12 at 18:38
  • You could load the whole file in, do the jig with `preg_replace` then write it back. Rather inefficent. Just use a database and have the Friday afternoon off! – Ed Heal Oct 04 '12 at 18:40
  • If your are looking for a flat file DB. Just choose an usual character to separate Value (like | ) you can read the file line by line, strip to line on '|' remove extra white space with trim(). But i suggest to learn basic SQL – poudigne Oct 04 '12 at 18:44
  • Well I know I can easily parse the data. I was thinking use fgets to get the line username:world:resources: If I could even delete that entire line I could simply write another one after that. It wouldnt be the most efficient of course but it would work for what I need to do. – user1707616 Oct 04 '12 at 18:49
  • Well at the start a "custom-made" database system is not efficient. I suggest to learn Basic MySQL since it's pretty easy you can learn everything you need in a hour or 2 – poudigne Oct 04 '12 at 18:51