I am building a dynamic table using php, pear HTML_Table, and sql.
My the first query pulls the information for the table headers, and then pear uses this to create the headers. The next query pulls the information from multiple tables to return the correct data sets. Next, I need to tie these data sets to the headers and display the results down each matching column. I am able to display all of the data, but not properly. Column 0 starts at row 1, and displays the current 4 test items, column 1 starts at row 5 and displays the current 2 test items, column 2 then starts at 7...How can I get the column count to reset to 0 after the previous column runs out of matching data? The other part of this problem is that I also need to apply rowSpans to the inserted data as this is a scheduling project. I have been at this for a week now and have been unable to find any relevant examples or suggestions. What am I missing, as I don't think this should be such a difficult task?
Code below.
<?php
session_start();
include_once("../php/functions.php");
include_once("HTML/TABLE.PHP");
$assetHead = headers('Assets', $_SESSION['deptID']);
$captionHeading = $_SESSION['dept'];
$conn = connect();
$attrs = array( 'id' => 'main',
'width' => '100%',
'Border' => '1');
$table = new HTML_Table($attrs);
$table->setAutoGrow(true);
$table->setAutoFill('n/a');
$sql_assets = "select AssetName, AssetID
from Assets
where Assets.DepartmentID = $_SESSION[deptID]";
$stmt1 = sqlsrv_query($conn, $sql_assets);
if ($stmt1)
{
$rows = sqlsrv_has_rows($stmt1);
if ($rows === true)
{
while( $row = sqlsrv_fetch_array( $stmt1, SQLSRV_FETCH_ASSOC) )
{
$assetName [] = $row['AssetName'];
$assetID [] = $row['AssetID'];
}
}
}
else
{
die( print_r(sqlsrv_errors(), true));
}
$i = 0;
foreach($assetName as $val)
{
$table->setHeaderContents(0, $i++, $val);
unset($val);
}
sqlsrv_close($conn);
$conn = connect();
$tsql = "select + 'Job#' + CAST (JobNum AS VARCHAR(10))+ ' ' + Description AS newField, datediff(MI,StartTime, EndTime) / 15 as 'RowSpan', AssetName, AssetID
from Calendar_View, Departments, Status, Assets
where Departments.DepartmentName = '$captionHeading' and Calendar_View.Department = Departments.DepartmentID and AssetStatus = Status.StatusID and
Calendar_View.Asset = Assets.AssetID
order by AssetID asc";
$rowcounter = 1;
$stmt = sqlsrv_query($conn, $tsql);
if ($stmt)
{
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) )
{
for ($i = 0; $i < count($assetID);$i++)
if ($row['AssetID'] == $assetID[$i])
$table->setCellContents($rowcounter++,$i,$row['newField']);
}
}
else
{
die( print_r(sqlsrv_errors(), true));
}
sqlsrv_close($conn);
echo $table->toHTML();
?>
</body>
</html>
$headerCounter = 0;
$stmt = sqlsrv_query($conn, $tsql);
if ($stmt)
{
$rows = sqlsrv_has_rows($stmt);
if ($rows === true)
{
$cellCounter = 1;
$cellPosition = 0;
$rowCounter = 1;
echo "Header position outside of loop: $headerCounter
";
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) )
{
if ($row['AssetID'] == $assetID[$headerCounter])
{
echo "<br>begining of if statement<br>";
echo "Header (beginning of loop) = " . $assetName[$headerCounter] . "<br>" ;
echo "Header Position (beginning of loop) = " . $headerCounter . "<br>";
echo "Row position = " . $rowCounter . "<br>" ;
echo "Cell position = " . $cellPosition . "<br>";
echo "<pre>";
var_dump($row);
echo "</pre>";
/*foreach ($row as $result)
{
echo "<pre>";
var_dump($result);
echo "</pre>";
}*/
$table->setCellContents($rowCounter,$cellPosition,$row['newField']);
//echo "$headerCounter";
//echo "<br>";
echo "Header Name (end of loop) = " . $assetName[$headerCounter] . "<br>" ;
echo "Header Position (end of loop) = " . $headerCounter . "<br>";
echo "Row position = " . $rowCounter . "<br>" ;
echo "Cell position = " . $cellPosition . "<br>";
//$cellPosition++;
$rowCounter++;
echo "end of if statment<br><br>";
}
else
{
echo "<br>header before increase $headerCounter";
echo "<br>header name outside of loop, before increment $assetName[$headerCounter]";
$cellPosition++;
$rowCounter = 1;
$headerCounter++;
$table->setCellContents($rowCounter,$cellPosition,$row['newField']);
echo "<br>header name outside of loop, after increment $assetName[$headerCounter]";
echo "<br>header increased by 1, now: $headerCounter";
}
}//$headerCounter++;
//$table->setCellContents($rowCounter,$cellPosition,$row['newField']);
}