Approach on searching, insertion and deletion in text file

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
sendoh07
Forum Newbie
Posts: 1
Joined: Sun Sep 03, 2006 12:27 pm

Approach on searching, insertion and deletion in text file

Post by sendoh07 »

Hi all. If I have to read from a text file, and do a query to display all usernames. How can I do it from reading a text file? Please note that as much as I would to use MySQL, I am not allowed to. For example, the text file "test.txt" contains the following:

Code: Select all

M0001:jlee:James:Lee:23:Lakers Avenue 111/03 Country:123-345678
M0002:tbogard:Terry:Bogard:23:Lakers Avenue 211/03 Country:124-333678
etc.....
The format is memberID:username:first name:last name:age:address:phone

In normal mysql queries you would just have to do something like "SELECT username FROM Users" to display all the usernames. However, now that I am using just a text file, how can I do searching, insertion and deletion?

Currently, I can only think of tokenizing those data and storing them into an array or linked-list. However, this approach is not that efficient.

Can anyone point me in a right direction please? Thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

More than likely, you'll need to end up storing the data into an array anyways. There are several conventions for tokening the data. Here are a few:
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Why not take a look at some already existing flat-file database systems? A simple Google search will provide you with some, but a known example can be seen here.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I would look into SQLite. It is well supported in PHP, allows SQL queries, and the database is just a file so no server required.
(#10850)
Yossarian
Forum Contributor
Posts: 101
Joined: Fri Jun 30, 2006 4:43 am

Re: Approach on searching, insertion and deletion in text fi

Post by Yossarian »

sendoh07 wrote:Currently, I can only think of tokenizing those data and storing them into an array or linked-list. However, this approach is not that efficient.

Can anyone point me in a right direction please? Thanks!
sqlite would be best I'd say.

However:

I would investigate: getcsv
http://fr2.php.net/manual/en/function.fgetcsv.php
parse_ini_file
http://fr2.php.net/manual/en/function.p ... i-file.php

OR maybe .... serialize objects? ... Ok, its only a suggestion....
Post Reply