Flat File Database Demo 5

3rd May 2010 · Last updated: 5th October 2016


I've revamped the previous demo to include the following improvements:

  • Two words can now be searched for in the database
  • Exact phrases can be searched for by entering speech marks around the words
  • You can search for more than two words if they are part of the same phrase
  • The code is theoretically faster as it no longer splits the database lines into separate fields
  • Searches are now safer as the demo strips out HTML tags and also multiple spaces


UPDATE 24th May 2010: I have rewritten large chunks of the demo to improve it. Firstly, you can now search using "AND" for two words that must be together on the same line. I've also taken out some of the repetitive code and put it into PHP functions. That way it's only needed once and can be called by a single function name instead of repeating the same code. I've also redone the database itself to be more realistic.

Known Issues

  • Searching for two words that overlap in the database causes it to fail. The reason is because after each word is found the code adds highlighting code, which then stops the second word from being found if it's within the region of the first one. The solution is simple - remove the highlighting routine from the demo if you don't need it.
  • Only the first word found is highlighted on each line of the results.

Comments (13)

  1. Avid:
    Can't load the the file, something wrong with your php from your ffdd5-php.txt. It shows this error:-

    No usable search string found!

    Posted on 8th December 2011 at 10:03 am
  2. Chris Hester:
    You need BOTH pages to work - the first page of the demo, and also the results file. You're not the first person to discover this! If you only have the 2nd page, then the search is blank, and you get the error message shown.

    I have redone the demo pages so it's now clear that you need both pages.

    Posted on 8th December 2011 at 7:49 pm
  3. Ben Yanke:

    Is there any way you could write a script to add and remove and change things in the database?

    Posted on 21st December 2011 at 4:09 pm
  4. Chris Hester:

    It's possible - I did a script that updated a database once before. I will try to do a new demo sometime later that writes to the database.

    Posted on 21st December 2011 at 7:10 pm
  5. Chris:

    I'm wondering if it's possible to modify the code to search for 4 keywords instead of 2 and based on a grid layout similar to below:


    So for example you'd search for 3B, 2H, 5J, 1C

    And it would return the corresponding results: 5, J, Q, M

    Any thoughts?

    Thanks in advance.

    Posted on 4th January 2012 at 4:47 am
  6. Chris Hester:

    This could be done already in that the database text file is first read into an array. Using the array you can reference any part of it. (Think of the array as the grid.) So your example of 3B would be defined something like this:


    That would give you "5". (Using the array co-ordinates for Column 3, Row 3 - remember arrays always start at 0, not 1.)

    To search for more than 2 items at once, you could just keep searching the array as many times as you wanted.

    Hope this helps.

    Posted on 4th January 2012 at 8:20 pm
  7. Chris:

    Thank you.
    I understand your explanation conceptually ... but you lost me technically with the $array[2][2]; :-)

    I'm wondering if it's easier to have the database formatted like this?



    That way each keyword is a number/letter combination and easier to search for?
    The reason I want to search for 4 keywords at once is because I have to use the result as part of my login for work which is different with every session. So I'm looking to save time by kind of automating the query rather than manually looking it up on my printed grid card.

    Posted on 5th January 2012 at 11:02 pm
  8. Chris Hester:

    I'm lost myself now on this one! Firstly, your database above will use a lot more lines, and thus bytes.

    Secondly, are the keywords you're searching for unique? Or do they occur more than once?

    Thirdly, again I think that you would be okay just referencing the grid via the array code I gave you. If the keyword combinations are unique, then you could just search for them as normal in my demo. (Just do the search four times in the code.) But if they repeat, then you could access the grid using an array. It's really easy:

    My demo first reads the contents of a file into an array (think: grid). So you have all the lines that were in the original file (for lines think rows). If the lines are split into variables (by looking for the pipe symbol we use) then each row then has columns.

    Let's use a database that looks like this as an example:


    After feeding it into an array by reading the file line by line and splitting the text using the pipe symbol, it will give us an array that matches the database.

    To get the first 'cell' (or the contents of column 1 and row 1) we access the grid via this code:

    echo $array[0][0];

    This gets the contents of line 0 (as PHP counts from 0 not 1!) and the first variable on the line (again using 0 not 1). We could have written the code like this:

    echo part of the array (that the database was put into at first) using row [0] and column [0];

    See? This will echo "A".

    To echo "E" we would use:

    echo $array[1][2];

    Or "row 2" and "column 3".

    Anyway, I'm not sure if this is what you need, but give arrays some thought. They really are easy to use. Have a look at the online PHP manual for examples.

    One last thing. To echo the whole of a row in an array, just use one variable. Eg:

    echo $array[0];

    That would give you all of line 1 in the database.

    Posted on 5th January 2012 at 11:41 pm
  9. Chris:

    Ok...so what happens is this:

    - When I log into work from home I use my username and password for the VPN.
    - In addition to that, the login site displays 4 keywords for me to authenticate against using a pre-printed grid card (like a lookup table where I have to match the rows & columns and enter the result.
    - So for example the site will give me the following unique keywords: A1, J5, E3, D4
    - I physically have to look those up on the grid card, and let's say they correspond to X, 3, I, C
    - I enter X3IC back into the site to authenticate and I'm logged in.

    The problem is that sometimes I need to do this several times a night and let me tell you, at 2:00AM it becomes very challenging. That's why I thought I'd try to implement an easier way to lookup the keywords.

    I kinda know how to do it using php and a mysql database but I think it's overkill to use a mysql db for this, and I wouldn't mind learning a new thing.

    Your site has been very helpful so far but I think I've gotten over my head. I understand the array part, it's the querying that I don't understand.

    Any chance you'd consider creating this for me and I'd be more than happy to send you an Amazon gift card or if you have an Amazon wish list, something from it?

    If not, then no worries. I'll keep at it.

    Posted on 6th January 2012 at 1:20 am
  10. Chris Hester:

    I'll have a look into this if I get time, though I can't promise anything.

    Posted on 6th January 2012 at 7:09 pm
  11. Anubhav Sharma:

    I will very thank full, if you tell me how to impliment this demo in my site.

    will this demo auto create rows, when we enter something new to database,
    If yes, then how ?

    Do i need first demo also to impliment this 5th demo, How ?

    I am new to this so please dont mind, but i to learn.


    Posted on 1st April 2012 at 5:32 pm
  12. Anubhav Sharma:

    Am new to php, Please help me with code.

    Can i replace exact sentence in text file with new sentence using PHP code

    Please help.!!

    Posted on 4th April 2012 at 7:31 pm
  13. Chris Hester:

    Hi Anubhav, sorry for the late reply. To use this demo you will need 3 files:

    1. the database (a text file you create)
    2. the HTML page with a form on it that you see when you run the demo
    3. a 'results' page made with PHP that is called when the user submits the form

    You can download all these from the bottom of the demo's first page.

    Just upload them to your website server (so long as it has PHP running!).
    You can then run the first page (the HTML form) from the browser, just like any other page.

    To alter the demo, you will need to know basic PHP. Then you can edit the results page to do what you want. PHP is very easy to learn. Start from the official website here: PHP Manual (UK)

    It is really best if you learn by trying to code yourself as I do not have the time to do the coding for you! That way you will also know how the demo works and be able to change it to your needs. Good luck!

    You then ask if the demo will "auto create rows, when we enter something new to database". Yes, it counts the number of lines (rows) in the database before outputting them to the screen. So you can keep adding more lines to the database text file as you wish.

    You don't need the 1st demo to run this 5th one. Each demo works separately.

    Lastly, you ask "Can i replace exact sentence in text file with new sentence using PHP code". Yes! But this code is not written by me yet. I have done it before at work. Basically what you have to do is have the demo read all of the database into a string, line by line. (It does this now, but uses an array.) You can then replace part of that string with your new text. (Look at str_replace in the PHP Manual.) Then you simply write this new text back to the file, erasing the previous contents.

    I will try to remember to create my next demo which will show this in action. It's fairly easy to do in theory. Wish me luck!

    Posted on 4th April 2012 at 9:20 pm