我目前在我的网站上使用以下查询/表格显示显示了一个功能正常的表格。我需要修改表格的显示方式,以便我可以根据我在查询中调用的 CompanyID 将最后一列设为链接。
查询(出于安全考虑):
$tableResults = dbQuery('SELECT CompanyName, ContractStatus, LEFT(ContractDate, 10), CCNumber, LevelName, cinfo.CompanyID
FROM <databases> WHERE <things>
order by CompanyName');
while($tableRow = mysql_fetch_assoc($tableResults))
{
$tableData[] = $tableRow;
}
$tableColNames = array_keys(reset($tableData));
还有表格(忽略格式,数据结构是我关心的):
<table width="100%" border="0" cellspacing="1" cellpadding="5" class="tablesorter" id="contcomm_table">
<thead>
<tr>
<th class="header"><strong>Company Name</strong></th>
<th class="header"><strong>Contract Status</strong></th>
<th class="header"><strong>Contract Date</strong></th>
<th class="header"><strong>Writing Number</strong></th>
<th class="header"><strong>View Comm Schedule</strong></th>
</tr>
</thead>
<tbody>
<?
foreach($tableData as $row)
{
echo "<tr>";
foreach($tableColNames as $colName)
{
echo "<td>" . $row[$colName] . "</td>";
}
echo "</tr>";
}
?>
</tbody>
</table>
相关链接为“/_Document.pdf”。我想我遇到了周一的情况,因为我现在无法理解这一点。
编辑:到目前为止的答案已经让我更接近了,现在我只需要克服最后的困难。我的所有内容都显示正确,并且 URL 将显示...但现在我需要将 URL 与 URL 中的公司 ID 连接起来。
所以,我现在正在研究的相关领域,以及我为此付出的最大努力:
if ($colName=='LevelName') {
echo "<td><a href='http://my.url.here/".$CompanyID."_Document.pdf' target='_blank'>" . $row[$colName] . "</a></td>";
}
else {
echo "<td>" . $row[$colName] . "</td>";
}
最佳答案
应该是这样的:
<table width="100%" border="0" cellspacing="1" cellpadding="5" class="tablesorter" id="contcomm_table">
<thead>
<tr>
<th class="header"><strong>Company Name</strong></th>
<th class="header"><strong>Contract Status</strong></th>
<th class="header"><strong>Contract Date</strong></th>
<th class="header"><strong>Writing Number</strong></th>
<th class="header"><strong>View Comm Schedule</strong></th>
</tr>
</thead>
<tbody>
<?
foreach($tableData as $row)
{
echo "<tr>";
foreach($tableColNames as $colName)
{
if ($colName=='name_of_you_link)' echo "<td><a href='/_Document.pdf'>" . $row[$colName] . "</td>";
echo "<td>" . $row[$colName] . "</td>";
}
echo "</tr>";
}
?>
</tbody>
</table>
关于php - 添加到我的 SQL 查询表的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23898814/