好吧,我从数据库中输出多行数据没有问题,我根本不知道如何以我想要的格式输出它。
这是我的基本问题。
<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就是它自己的列等等。
我该怎么做?显然必须有办法,我只是新手。非常感谢任何帮助!
答案 0 :(得分: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);
}
?>