0

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++;
royjm
  • 107
  • 14
  • 1
    1. learn about sql injection. 2. there is no need to unset(). 3. this has nothing to do with sql. give a minimal example that works without sql queries. 4. State what you expect, and what actually comes out. – cweiske Jul 25 '13 at 05:38
  • Ok, the table's first row (a header row of asset names from the assets table) is populated by the first sql statement '$sql_assets', and then the first column is populated by the for loop that calculates the time in 15 minute increments. Then the object '$obj' is created by querying the schedule table, and that object (in this case) is passed to the currentJobsList function to determine starting time and placed into an array. That array currentJobs is then used to populate the inner cells of the table. However, it just lays them out one next to the other. I need to have them under there asset. – royjm Jul 25 '13 at 12:43
  • try to break that problem down to some small code example without sql queries. – cweiske Jul 25 '13 at 12:45
  • Since all data is derived from the database it is taking a few minutes to create a simple sample file. Working on it now. Thanks for the help and assistance. – royjm Jul 25 '13 at 14:05
  • I placed a simple example of the code but the data is far more simple than what is actually being presented and used for calculating position in the table. – royjm Jul 25 '13 at 15:15
  • Thanks cweiske! Eliminating the sql allowed me to see exactly what was going wrong. I'm sure there is way to do this without presorting, but for now I can look at other aspects of this project. Thanks again! Now I just hope it doesn't blow up when put the sql back into the mix. – royjm Jul 25 '13 at 17:17

1 Answers1

1

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!

royjm
  • 107
  • 14