【发布时间】:2014-02-03 04:08:03
【问题描述】:
我正在尝试使用 PDO 从 mysql 中获取数据并将其输出到 html 表中
<div class="data">
<table class="table table-striped">
<?php
if($_POST['submit'] && $_POST['search_for'] == "keyword") {
$stmt = $db->prepare("SELECT * FROM my_tbl WHERE log like '%?%' ORDER BY submission_timestamp DESC");
foreach($stmt->execute($_POST['search_for']) as $row):
} elseif($_POST['submit'] && $_POST['search_for'] == "address") {
$stmt = $db->prepare("SELECT * FROM my_tbl WHERE address like '?' ORDER BY submission_timestamp DESC");
foreach($stmt->execute($_POST['search_for']) as $row):
} else {
foreach($db->query('SELECT * FROM my_tbl ORDER BY submission_timestamp DESC') as $row):
}
?>
<tr>
<td width="10%"><?php echo $row['address'] ?></td>
<td><?php echo $row['log'] ?></td>
<td width="10%"><?php echo $row['submission_timestamp'] ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
我在 elseif 声明之前只说意外的 '}' 时收到一个错误。
虽然我用的时候
<div class="data">
<table class="table table-striped">
<?php
foreach($db->query('SELECT * FROM my_tbl ORDER BY submission_timestamp DESC') as $row):
?>
<tr>
<td width="10%"><?php echo $row['address'] ?></td>
<td><?php echo $row['log'] ?></td>
<td width="10%"><?php echo $row['submission_timestamp'] ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
没有发生错误,代码正常运行,从 mysql 数据库中获取数据并将其显示为 html 表。
我本质上是想根据发送到页面的发布数据来更改表格的输出。
有没有一种方法可以用类似于后面的 if 语句格式化第一个代码,或者我应该在每个 if 语句循环时回显 html(我假设发生错误是因为它正在关闭 foreach 子句什么的)。
【问题讨论】: