Flat File Database Demo 4
Now with added search function!
16th January 2009 · Last updated: 20th April 2010
Comments
Doug F emailed me to ask if it was possible to add a search function to my flat-file database script. It is! In fact I had already written a basic search function before but for different code. So I added new code from scratch that would work with the second flat-file demo here.
You can now search for a word in the database and it will show each row that contains it. I even added a highlight to the words that match your search!
A simple form is used to enter the search word on the first page. This is passed to a second page that looks for the word in each field of the database. If the word is found then the script will also output the full row from the database into a table. If the word isn't found then a simple error message is shown.
RUN THE DEMO
UPDATE 21st January 2009: Improved the code so part words can be searched for. It will also pick up words of any case (upper or lower, or mixed).
Update 20th April 2010: It now displays the number of searches found. Thanks to Frank Höfken for the inspiration!
Useful Info
EMAIL: www.designdetector.com (replace the 1st dot with "@")
NEWSFEED: Subscribe to news of fresh posts and site updates. (RSS 2.0 compatible newsreader required.)
Disclaimer
Some links on this website lead to information provided by external services not under my control, therefore I am not responsible for the content or accuracy of the linked information.
All code examples are not guaranteed 100% free from bugs and/or mistakes. Use them at your own risk. I do not take any blame should a problem occur relating to use of code on this site given as an example for your own use. Such code has been tested and found to work for me, but I cannot vouch for other computer systems (existing now, or in the future) which it may be used on, or changes you introduce yourself based on my code.
This website is © Christopher Hester, except where separate authors are named. No part of this website may be reproduced or re-used in any way without my prior permission, except content added from separate authors (who retain the copyright on their material), examples of code, and any other content I explicity state is free to copy and make use of.
This page was last updated on 20th April 2010.
Comments (4)
how we can delete specific line from text file
Sent in by email on 24th February 2009 ¶
This is just what I was looking for
Sent in by email on 6th August 2009 ¶
Hey there,
I've found this database code extremely helpful! I am using a similar and somewhat customized code of the one in Demo 1, and I would just like to ask a question about it.
How would I search the database, and only display lines that begin with a certain number (a serial number)?
Thanks a lot! This is great!
Again, thanks for this amazing script! I use it all the time!!
Sent in by email on 17th September 2009 ¶
Just loop through the database and check the first field (by splitting the line as in the demo). Then just check if it is the serial number you want. If so, split the line again to get the whole of the fields in the line, then echo the contents of that line as required (eg: using table tags). Something like this should work from within the main loop:
list ($serialnumber) = split ('\|', $line);if ($serialnumber == $searchstring) {list ($serialnumber, $product, $description, $price, $date) = split ('\|', $line);echo <<<HTML
<tr><td>$serialnumber</td><td>$product</td><td>$description</td>$price<td>$date</td></tr>HTML;}17th September 2009 ¶