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 matlab数字图像处理频率域滤波
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行