0

好吧,我从数据库中输出多行数据没有问题,我只是不知道如何以我想要的格式输出。

这是我的基本问题。

<tr>
    <?php               
    //make connection to database, bail if no connection
    $connection = odbc_pconnect('sitedata','','');
    if (!$connection) { exit("Connection Failed: " . $connection); }

    //retrieve relevant data
    $Date = "SELECT EntryDate AS DT FROM TimeSheet WHERE EmployeeID = 'AA01'";

    $rs = odbc_exec($connection, $Date);

    //output data
    while(odbc_fetch_row($rs)) {
        $Line1 = odbc_result($rs, 'DT');
        printf("<td><center>%s</center></td>", $Line1);                 
    }
    ?>
</tr>

这将输出一个单行表,每个新日期创建一个新列。

现在,这对我来说有点用处,因为这些需要输出到单个列中,其中每个新数据值都是行的开头。在此之后,我需要扩展表格宽度,添加新的数据值,例如:

$Time = "SELECT TotalTime as EN FROM TimeSheet WHERE EmployeeID = 'AA01'";
$JobName = "SELECT JobName as JN FROM TimeSheet WHERE EmployeeID = 'AA01'";

Time 是它自己的列,JobName 是它自己的列,等等。

我该怎么做?显然必须有一种方法,我只是这方面的新手。非常感谢任何帮助!

4

1 回答 1

1

您需要在一个 SQL 语句中获取字段...

SELECT EntryDate AS DT, TotalTime as EN, JobName as JN 
    FROM TimeSheet WHERE EmployeeID = 'AA01';

有了它,您可以完全按照您现在正在做的事情做,除了构建一整行而不是列。

<?php               
    //make connection to database, bail if no connection
    $connection = odbc_pconnect('sitedata','','');
    if (!$connection) { exit("Connection Failed: " . $connection); }

    //retrieve relevant data
    $Date = "SELECT EntryDate AS DT, TotalTime as EN, JobName as JN FROM TimeSheet WHERE EmployeeID = 'AA01';";

    $rs = odbc_exec($connection, $Date);

    //output data
    while(odbc_fetch_row($rs)) {
        $DT = odbc_result($rs, 'DT');
        $EN = odbc_result($rs, 'EN');
        $JN = odbc_result($rs, 'JN');

        printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>", $DT, $EN, $JN);                 
    }
?>
于 2012-11-08T13:59:03.810 回答