Grid Login Demo
9th January 2012 · Last updated: 5th October 2016
Chris commented on my Flat File Database Demo 5:
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:
|A|B|C|D|E|F|G|H|I|J 1|R|C|M|J|6|4|6|Y|W|G 2|J|R|E|4|H|7|F|J|T|Q 3|1|5|V|Q|E|3|R|3|Y|4 4|4|H|C|3|3|E|M|Y|H|T 5|R|E|V|8|4|J|H|R|D|Q
So for example you'd search for 3B, 2H, 5J, 1C and it would return the corresponding results: 5, J, Q, M.
He needed this for work, where he had to look up four letter/number combinations on a paper grid and enter the results to log in. I envisaged you could do this using a flat file database to store the grid, accessing it using PHP arrays. This demo is the result.
The demo uses a single input to enter the four keywords needed. These are then stripped out into four separate strings. Then I realised it was just a matter of converting the letters A-J above the grid into numbers, so you could reference the columns in the grid. I used PHP's
ord function to do that. I could then access the grid using an array format like this:
Note how PHP cleverly allows you to include variables within array brackets. Normally you'd use numbers there, eg:
$col;. $col is the name of the array I used. The other two variables are split from the input string to give you the column and row respectively.
Take a look at the code via the demo to see how it works. I've also added error traps incase the input isn't in the right format, or letters and numbers out of the range of the grid are typed in.