Log in

View Full Version : search function on flat file



hannah sofia
05-25-2011, 12:55 AM
Hello,
I have a text file database and here are the sample contents:

server1|title1|image url1|category1|video id1
server2|title2|image url2|category2|video id2
server3|title3|image url3|category3|video id3
server4|title4|image url4|category4|video id4
server5|title5|image url5|category5|video id5

How to make a search feature that will generate a result like the one below if someone will write any part of the TITLE on the search box?

<a href="video.php?v=server1&id=video id1&t=title1">title1</a>

Please help me on this... I'm surfing on the net for 12 hrs but can't find anything close to the function I want.
I am making a simple script out of php codes I collected on the net. I dont know anything about codes, I only know how to analyze it and make some trial and error...
Thank you in advance!

traq
05-25-2011, 01:22 AM
two approaches:

1) read the file into an array, and simply search the array for your keyword.

2) read the file one line at a time and use a regex to look for matches.

either way is going to be highly inefficient, especially if you have a lot of entries. a database is much better suited to this sort of task.

if you decide to you flat files, I'd recommend storing your records in JSON format - much quicker and easier to translate into PHP. You also might separate each "column" into its own text file (so when you search a specific column, you only have to load the data for THAT column).