【发布时间】:2018-03-15 04:54:34
【问题描述】:
经过多次调整,我终于让我的桌子看起来有点像样了:
<table id="Productslist" class="list">
<thead>
<tr>
<th> Part </th>
<th> Brand </th>
<th> Name </th>
<th> Quantity </th>
<th> Price </th>
</tr>
</thead>
<tbody>
<?php
$part = find_part();
foreach($parts_list as $col => $value) {
echo '<tr><td>'.$col.'</td><td>'.$value.'</td>
<td>'.$part['Name'].'</td></tr>';
}
?>
</tbody>
<tfoot>
<tr>
<td> Total </td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tfoot>
</table>
foreach 循环效果很好!因为它在该列中找到列名和值并将其插入到它们自己的行中。极好的!现在我真的很想在数据库查询中使用$col 和$value,但是当我在函数中或通过全局检索尝试它时,它不起作用....有什么见解吗?
function find_part() {
global $db;
global $col;
global $value;
$sql = "SELECT * FROM " .$col. "";
$sql .= "WHERE ID='" . $value ."' ";
$result = mysqli_query($db, $sql);
$part = mysqli_fetch_assoc($result);
mysqli_free_result($result);
return $part; }
或
function find_part($col, $value) {
global $db;
$sql = "SELECT * FROM " .$col. "";
$sql .= "WHERE ID='" . $value ."' ";
$result = mysqli_query($db, $sql);
$part = mysqli_fetch_assoc($result);
mysqli_free_result($result);
return $part; }
error_log 两次都表示它的索引和未定义的索引。
【问题讨论】:
标签: php database mysqli foreach html-table