I am writing PHP script, which will store mouse position over the page with a timestamp. It will be used for generating simple heatmap.
I have MySQL database of X & Y coordinates and I thought, that I would do an array, which I would use for adding counts of appearances of the cursor over specific pixel.
It would work like this:
I read data from the database saying for example four recordings (x/y) 10/9,8/7,2/10,10/9
I would put 1 on each of the coordinates in the array, the last point would already in the array, so it would put 2 on x=10/y=9, as total count of mouse being over that pixel.
How should I make the array and how to read it?
First, you need to create an empty matrix, where the entire grid is defined and populated with zeros:
Now you simply iterate over your MySQL results and increment each associated point every time you hit it. In this example I assume your x and y columns are just called
xandy.Now if you want to get the number of hits for the point where e.g.
x = 1, y = 8, you simply do:You can swap the levels round so
xis on the outer level so they can be referenced as$matrix[$x][$y]instead of$matrix[$y][$x]if you wish, but I personally feel it makes more sense if the array is structured as rows of columns (ys ofxs) like a database table.