0

我正在使用 php、pear HTML_Table 和 sql 构建动态表。我的第一个查询提取表头的信息,然后 pear 使用它来创建表头。下一个查询从多个表中提取信息以返回正确的数据集。接下来,我需要将这些数据集与标题联系起来,并在每个匹配列的下方显示结果。我能够显示所有数据,但不能正确显示。第 0 列从第 1 行开始,显示当前的 4 个测试项目,第 1 列从第 5 行开始,显示当前的 2 个测试项目,第 2 列然后从 7 开始...如何让列数重置为 0上一列的匹配数据用完了?这个问题的另一部分是我还需要将 rowSpan 应用于插入的数据,因为这是一个调度项目。我已经在这里待了一周,但找不到任何相关的例子或建议。我错过了什么,因为我认为这不应该是一项艰巨的任务?代码如下。

<?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; $细胞位置 = 0; $rowCounter = 1; echo "循环外的标头位置:$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']);

        }
4

1 回答 1

0

好的,问题是我离开初始循环后没有增加 rowcounter 变量。以下行:

 $cellPosition++;
            $rowCounter = 1;
            $headerCounter++;
            $table->setCellContents($rowCounter,$cellPosition,$row['newField']);

本来应该:

 $cellPosition++;
            $rowCounter = 1;
            $headerCounter++;
            $table->setCellContents($rowCounter++,$cellPosition,$row['newField']);

很抱歉原始帖子的格式不佳。我不确定出了什么问题。

于 2013-06-11T16:00:34.537 回答