From a database I’m pulling a kind of timeline of Div’s with a certain starting point and certain end point. Some of them overlap, some of them can be fitted next to each other.

Ultimately I want to slide them together so that it’s as compact as possible like this:

I’m doubting how to approach this challenge: through a server side (php) script or with some javascript floating script thingy. Or off course a completely different approach
Could some one push me in the right direction?
Edit::
It’s important, as it’s a timeline, that the horizontal position of the div’s remain the same. So floating all the divs to the left or inline-block them is no option 🙂
My database setup:
id | name | start | end
1 | a | 2 | 7
2 | b | 5 | 10
etc
your question sounds very naive but it is actually include some complex elements if needed to be resolved in the most OPTIMISED way.
the quick answer of what I probably would do to generate your display –
use your PHP code to generate a DIV container with
style=”display:block” for every row
inside the row generate appropriate size DIV (end-start * scale)
with style=”display:inline-block; float:left; display:relative” and (EDIT:) add transparent DIV elements to compensate for the white spaces you need. (i.e. from 0 to start and from the end to the start of the next DIV)
add the name field inside the DIV element
use mySchema;drop procedure if exists tileItems;
here is the table I used to check it –
few words regarding optimisation (one of my favourite subjects 🙂 –
in this code there is no optimisation, it means that the task will be allocated to the first available line.
in order to minimise the number of lines its possible (but will take some time) to create a function that uses Heuristic method to resolve this problem.
output: