你的问题听起来很幼稚,但它实际上包括一些复杂的元素,如果需要解决在最优惠的方式。
快速回答我可能会做什么来生成您的显示 –
>使用提供的函数将行号添加到表中
>使用您的PHP代码生成DIV容器
style =“display:block”
>里面的行生成适当的大小DIV(end-start * scale)
with style =“display:inline-block; float:left; display:relative”和(EDIT :)添加透明DIV元素以补偿所需的空格。 (即从0到开始,从结束到下一个DIV的开始)
>在DIV元素中添加名称字段
使用mySchema;
drop procedure如果存在tileItems;
DELIMITER $$
CREATE PROCEDURE tileItems ()
BEGIN
DECLARE p_id, p_start, p_end, p_row int;
DECLARE done INT DEFAULT FALSE;
DECLARE cur1 CURSOR FOR SELECT id, start, end FROM tasks order by start, id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
drop temporary table if exists tiles;
create temporary table tiles (
row int(11) NOT NULL,
id int(11) NOT NULL,
end int(11) NOT NULL
);
-- row field will indicates the row number the task should apear
OPEN cur1;
next_task: LOOP
FETCH cur1 into p_id, p_start, p_end;
IF (done) THEN
LEAVE next_task;
END IF;
select min(row) from (select row, max(end) me from tiles t2 group by row) t1
where me < p_start
into p_row;
-- take care of row numbering
IF (p_row IS NULL) then
select max(row) from tiles
into p_row;
IF (p_row IS NULL) then
SET p_row = 0;
END IF;
SET p_row=p_row+1;
END IF;
insert into tiles (id, row, end)
values (p_id,p_row,p_end);
END LOOP;
-- CLOSE cur1;
-- here is your output, on the PHP/.Net code you should loop on the row
select tasks.*, tiles.row from tasks
inner join tiles
on tasks.id = tiles.id
order by tiles.row, tasks.start;
END $$
DELIMITER ;
这里是我用来检查它的表 –
CREATE TABLE `tasks` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`start` int(11) NOT NULL,
`end` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=11 ;
INSERT INTO `tasks` (`id`, `name`, `start`, `end`) VALUES
(1, 'A', 2, 6),
(2, 'B', 5, 7),
(3, 'C', 8, 10),
(4, 'D', 1, 5),
(5, 'E', 6, 7);
几个词关于优化(我最喜欢的主题:) –
在这段代码中没有优化,这意味着任务会被分配到第一个可用的行。
为了最小化它的可能的行数(但将需要一些时间)创建一个使用Heuristic方法来解决这个问题的函数。
输出:
id name start end row
4 D 1 5 1
5 E 6 7 1
3 C 8 10 1
1 A 2 6 2
2 B 5 7 3