Need help in interesting PHP code! 7x7 block on click

Hello, i have this square 7x7 where each row is clickable in it.

If row is clicked i want it to make 3x3 block around it (and it kinda work) if it is possible if not mark only available rows as marked.

It works grate until i try to use row a-1 to row a-7 or rows a-1, b-1, c-1, d-1, e-1, f-1 and g-1

Here is a demo:

Try to click around and you will see that it generates 3x3 blocks or for example on g-7 2x2 witch is correct but if you try g-1 for example you see that something breaks!

I cant figure out the best method to solve this! Any help?
Thanks in advance!

The first question is what should it do if you click on G1? Should it block out F1, F2 and G2, or should it “wrap” and also block out F7, G7, A7, A1 and A2?

1 Like

If clicked on G1 it should mark F1, F2, G1 and G2 same as if you click G7 it marks F6, F7, G6 and G7.

For example if you click E1 it should mark D1, D2, E1, E2, F1 and F2.

As i told it works as expected on all places except on whole A(1-7) rows and whole first vertical line rows.

I suspect the issue is that you don’t do any bounds-checking when you are splitting the arrays. I just put max() and min() around some stuff in both areas:

$beforeLetter = array_slice($rangeLetter, max($foundIndexLetter -1,0), 1);
$afterLetter = array_slice($rangeLetter, min($foundIndexLetter + 1,6), 1);

and in the number section as well, and it seems to work better.

1 Like

Yeah, it seems to be working now as needed! can you explain what are the second parameters in min max?

As the names suggest, really, min() returns the minimum of the two parameters, and max() returns the maximum of the two parameters.

You could achieve it in other ways, an if() or a ternary operator, but these are clear and concise.

But why in max($foundIndexLetter -1,0) there is ,0 ?
And min($foundIndexLetter + 1,6) there is ,6?

As i have 1-7 … . Does it sees it as 0-6?

Yes, the base index on a PHP array is 0, so your array elements are 0 to 6. You can see this by just adding a var_dump() on the array.

1 Like