如何在动态表头下排序和显示动态数据?

时间:2013-07-24 14:55:28

标签: php sql-server-2008 pear html-table

该项目是使用php的网络日历/日程安排程序。这个想法是用户可以安排在特定时间分配给特定机器的作业,而其他人可以查看作业计划以及作业分配给哪个“资产”。但是,调度工作正常,我无法将数据绑定到它们所属的标题/列。它看起来像这样:

 <?php
include_once("HTML/TABLE.PHP");
$data = array(  0=>array(1,'asset1','2013-07-24 10:00:00', '2013-07-24 12:00:00','red',2),
                1=>array(2,'asset1','2013-07-24 12:00:00', '2013-07-24 13:00:00','green',3),
                2=>array(3,'asset2','2013-07-24 11:00:00', '2013-07-24 12:00:00','blue', 4),
                3=>array(4,'asset2','2013-07-24 12:00:00', '2013-07-24 14:00:00','red', 2),
                4=>array(5,'asset3','2013-07-24 11:30:00', '2013-07-24 12:00:00','green', 4),
                5=>array(6,'asset4','2013-07-24 12:00:00', '2013-07-24 14:00:00','blue', 3),
                6=>array(7,'asset1','2013-07-24 11:45:00', '2013-07-24 13:00:00','red', 1),
                7=>array(8,'asset4','2013-07-24 13:00:00', '2013-07-24 15:00:00','yellow', 5)
              );
    $attrs = array( 'class' => 'main', 
        'id' => 'main_id',
        'width' => '100%',
        'border' => '1',
        'cellspacing' => '0',
        'cellpadding' => '0');
    $table = new HTML_Table($attrs);
    $table->setAutoGrow(true);
    $table->setAutoFill('n/a');

    $heads = array( array('asset1','asset2','asset3','asset4'));
        $i = 1;
    foreach($heads as $val)
    {
        $table->setHeaderContents(0, $i++, $val);
        unset($val);
    }   

    $now = date('U');
    $offset = ($now % 900);
    $now = $now-$offset;
    for ($i = 0;$i < 33; $i++)
    {
        $table->setHeaderContents($i,0, date('g:i',$now));
        $now += 900;
    }

    $cellPosition = 1;
    $rowCounter = 1;

    for ($i=0;$i < count($data);$i++)
    {
        $table->setCellAttributes ($rowCounter,$cellPosition,' bgcolor = '. $data[$i][4].  ' rowspan=' . $data[$i][5]);
        $table->setCellContents($rowCounter,$cellPosition,"Job# ".$data[$i][0] . " belongs to: " . $data[$i][1]);
            $cellPosition++;
            $rowCounter =1;
    }
    echo $table->display();
?>

如何将信息仅绑定到它所属的列?

我已经走到了这一步,但如果第一列是真的,我会得到奇怪的结果:

$cellPosition = 0;
$rowCounter = 1;
for ($x=0;$x <= count($heads);$x++)
{
    for ($i=0;$i < count($data);$i++)
    {
        if ($data[$i][1] == $table->getCellContents(0,$x))
        {
        $table->setCellAttributes ($rowCounter,$cellPosition,' bgcolor = '. $data[$i][4].  ' rowspan=' . $data[$i][5]);
        $table->setCellContents($rowCounter,$cellPosition,"Job# ".$data[$i][0] . " belongs to: " . $data[$i][1]);
            //$cellPosition++;
            //echo ;
            echo "<br> The current count of x = : ".  $x;
            echo "<br>" . $table->getCellContents(0,$x) . " Matches " . $table->getCellContents(0,$x) . " at index " . $i;
            $rowCounter += $data[$i][5];
        }
            else
            {
                $rowCounter = 1;
            }

    }
    $cellPosition++;

1 个答案:

答案 0 :(得分:1)

好的,这是有效的,但前提是数据结果按资产顺序排列。如果不是,则行重置为第1行,第二个条目覆盖前一行。

$cellPosition = 0;
$rowCounter = 1;
for ($x=0;$x <= count($heads);$x++)
{
    for ($i=0;$i < count($data);$i++)
    {
        if ($data[$i][1] == $table->getCellContents(0,$x))
        {
        $table->setCellAttributes ($rowCounter,$cellPosition,' bgcolor = '. $data[$i][4].  ' rowspan=' . $data[$i][5]);
        $table->setCellContents($rowCounter,$cellPosition,"Job# ".$data[$i][0] . " belongs to: " . $data[$i][1]);
            echo "<br> The current count of x = : ".  $x;
            echo "<br>" . $table->getCellContents(0,$x) . " Matches " . $data[$i][1] . " at index " . $i;
            $rowCounter += $data[$i][5];
        }
            else
            {
                $rowCounter = 1;
            }

    }
    $cellPosition++;
}

我的解决方案是使用sql order by预分配记录。我不知道它有多优雅,但目前它正在发挥作用,我可以进一步深入这个项目。欢迎大家提出意见。谢谢!