duanmaifu3428 2013-07-24 14:55
浏览 108
已采纳

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

The project is a web calendar/scheduler using php. The idea is that a user can schedule a job that is assigned to specific machine at a specific time and others can see the job schedule and which 'asset' the job is assigned to. The scheduling works, however, I cannot tie the data to header/columns they belong to. It looks like this:

 <?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();
?>

How can I tie the information to only the column it belongs to?

I have gotten this far, but I get odd results if the first column is true:

$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条回答 默认 最新

  • dongqing483174 2013-07-25 17:13
    关注

    Ok this is working, but only if the array results are in order by asset. If not the row is reset to row 1 and the second entry overlays the previous.

    $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++;
    }
    

    My solution is to presort the records using sql order by. I don't know how elegant that is but for the moment it is working and I can move further into this project. Any suggestions would be welcome. Thanks!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大