I’m working on writing a web application using php & mysql, and was wondering about the best way to approach something. The application is simple and deals with displaying boxes (“cells”) which contain text.
Here’s the problem:
I have, say, 3 cells to display: Cell A, Cell B and Cell D. In the database each has a “priority” that indicates the order in which it will be displayed. Cell A’s priority is 1, B’s is 2, and D’s is 3. Right now they will display in order, but when I want to add Cell C and put it between B and D, I would first need to change Cell D’s priority to make room for Cell C to be in the middle.
I thought perhaps I could have the priority given at default for a new cell be a higher amount away from neighbor cells, but that would eventually fail in the same way as above.
Also, I considered using the priority field differently. Rather than using an abstract number to indicate the order, I could put the ID number of the cell that precedes it. That way, after displaying a cell, my function would do a select query to find the cell whose “priority” field is equal to the current cell’s id and display it. But then the problem would be that if a cell is deleted, the next one after it (and therefore the rest in the order) would no longer show up.
What is the best way to tackle this problem?
Have a priority column that takes a decimal value, and sort by the priority. With a decimal it is very easy to fit new items in right where they belong without having to change anything else.