我需要阻止ID列显示在html / php中生成的foreach表中,但同时我还需要通过ID引用要编辑的数据。这是代码。
$queryz = "SELECT * FROM box WHERE date=CURDATE()";
print "<table> ";
$result = mysqli_query($db,$queryz);
//return only the first row (we only need field names)
$row = $result->fetch_assoc();
print " <tr> ";
foreach ($row as $field => $value){
print " <th>$field</th> ";
} // end foreach
print " </tr> ";
//second query gets the data
$data = $result;
$data->fetch_assoc();
foreach($data as $row){
print " <tr> ";
foreach ($row as $name=>$value){
print " <td>$value</td> ";
} // end field loop
echo '<td><a href="edit.php?id=' . $row['ID'] . '">Edit</a></td>';
echo '<td><a href="print.php?id=' . $row['ID'] . '">Print</a></td>';
print " </tr> ";
} // end record loop
我希望它抓取所有列,但不显示ID列。我仍然需要ID数据,因此我可以编辑表格:echo '<td><a href="edit.php?id=' . $row['ID'] . '">Edit</a></td>';
谢谢你们!我已经尝试过滤过于数组并排除SQL中的数据,但它不允许我之后编辑该表。我猜是因为它首先没有抓住数据。
答案 0 :(得分:0)
在select语句中使用*不是一个好主意。您应列出要检索的字段(列)。如果您未在字段列表中包含ID,则问题将会消失。
变化:
$queryz = "SELECT * FROM box WHERE date=CURDATE()";
类似于:
$queryz = "SELECT field1, field2, etc. FROM box WHERE date=CURDATE()";
答案 1 :(得分:0)
您可以通过简单地忽略其键为&#39; ID&#39;的数组项来解决当前问题,如下所示:
$queryz = "SELECT * FROM box WHERE date=CURDATE()";
print "<table> ";
$result = mysqli_query($db,$queryz);
//return only the first row (we only need field names)
$row = $result->fetch_assoc();
print " <tr> ";
foreach ($row as $field => $value){
if ( $field <> 'ID' ) {
print " <th>$field</th> ";
}
} // end foreach
print " </tr> ";
//second query gets the data
$data = $result;
$data->fetch_assoc();
foreach($data as $row) {
print " <tr> ";
foreach ($row as $name=>$value){
if ( $name <> 'ID' ) {
print " <td>$value</td> ";
}
} // end field loop
echo '<td><a href="edit.php?id=' . $row['ID'] . '">Edit</a></td>';
echo '<td><a href="print.php?id=' . $row['ID'] . '">Print</a></td>';
print " </tr> ";
} // end record loop