php - Foreach 循环仅将第一个条目放入表中,然后将其移出
问题描述
我的 foreach 循环有点问题。它循环遍历表,但仅显示相应表中的第一个条目。之后所有条目都在表格之外。我希望它们在表中,我的疯狂猜测是它与SELECT语句有关。但我不确定。你能告诉我为什么这些条目不在表格中吗?
显示条目
代码片段
<?php
//Beginn of PHP
if(isset($_POST['but_show'])){
$show_id = $_POST['showid'];
//prepare statement for MySQL injection
$queryResults = $wpdb->prepare( "SELECT id, fileurl, fk_verfuegungsberechtigter FROM dateien_vermieter WHERE fk_verfuegungsberechtigter = '$show_id'" );
$results = $wpdb->get_results( $queryResults );
//loop through data
//$name = $print->gemeinde;
?>
<table class='wp-list-table widefat striped'>
<thead>
<tr> <!-- display table header for edit button -->
<th style='text-align:center;' width=<?php $width[0]?>>ID</th>
<th style='text-align:center;' width=<?php $width[0]?>>Dateien</th>
<th style='text-align:center;' width=<?php $width[0]?>>Vermieter-ID</th>
</tr>
</thead>
<?php
foreach($results as $print) {
echo "
<tbody>
<tr> <!-- display table data for edit button use of above mentioned loop only change !!!!->name -->
<td style='text-align:center;' width=" . $width[0] . ">$print->id</td>
<td style='text-align:center;' width=" . $width[7] . "><a href='$print->fileurl'><button id='files' type='button'><i class='far fa-file'></i></button></a></td>
<td style='text-align:center;' width=" . $width[0] . ">$print->fk_verfuegungsberechtigter</td>
</tr>
</tbody>
</table>";
}
}
//End of PHP
?>
解决方案
你</tbody></table>
在 foreach 里面关闭。试试下面的代码。
<?php if(isset($_POST['but_show'])){
$show_id = $_POST['showid'];
//prepare statement for MySQL injection
$queryResults = $wpdb->prepare( "SELECT id, fileurl, fk_verfuegungsberechtigter FROM dateien_vermieter WHERE fk_verfuegungsberechtigter = '$show_id'" );
$results = $wpdb->get_results( $queryResults );
//loop through data
//$name = $print->gemeinde;
?>
<table class='wp-list-table widefat striped'>
<thead>
<tr> <!-- display table header for edit button -->
<th style='text-align:center;' width=<?php $width[0]?>>ID</th>
<th style='text-align:center;' width=<?php $width[0]?>>Dateien</th>
<th style='text-align:center;' width=<?php $width[0]?>>Vermieter-ID</th>
</tr>
</thead>
<tbody>
<?php foreach($results as $print) { ?>
<tr> <!-- display table data for edit button use of above mentioned loop only change !!!!->name -->
<td style='text-align:center;' width="<?php echo $width[0]; ?>"><?php echo $print->id; ?></td>
<td style='text-align:center;' width="<?php echo $width[7]; ?>"><a href="<?php echo $print->fileurl; ?>"><button id='files' type='button'><i class='far fa-file'></i></button></a></td>
<td style='text-align:center;' width="<?php echo $width[0]; ?>"><?php echo $print->fk_verfuegungsberechtigter; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
或者
<?php
//Beginn of PHP
if(isset($_POST['but_show'])){
$show_id = $_POST['showid'];
//prepare statement for MySQL injection
$queryResults = $wpdb->prepare( "SELECT id, fileurl, fk_verfuegungsberechtigter FROM dateien_vermieter WHERE fk_verfuegungsberechtigter = '$show_id'" );
$results = $wpdb->get_results( $queryResults );
//loop through data
//$name = $print->gemeinde;
?>
<table class='wp-list-table widefat striped'>
<thead>
<tr> <!-- display table header for edit button -->
<th style='text-align:center;' width=<?php $width[0]?>>ID</th>
<th style='text-align:center;' width=<?php $width[0]?>>Dateien</th>
<th style='text-align:center;' width=<?php $width[0]?>>Vermieter-ID</th>
</tr>
</thead>
<tbody>
<?php foreach($results as $print) {
echo "
<tr> <!-- display table data for edit button use of above mentioned loop only change !!!!->name -->
<td style='text-align:center;' width=" . $width[0] . ">$print->id</td>
<td style='text-align:center;' width=" . $width[7] . "><a href='$print->fileurl'><button id='files' type='button'><i class='far fa-file'></i></button></a></td>
<td style='text-align:center;' width=" . $width[0] . ">$print->fk_verfuegungsberechtigter</td>
</tr>";
} ?>
</tbody>
</table>
<?php } ?>
推荐阅读
- typescript - 导入 IReact 类型定义
- react-native - Expo本地身份验证指纹传感器在IOS中不起作用
- sql - 我应该使用 SSIS 还是 SQL Server 导入导出工具进行大容量插入操作?
- java - 如何使用 Selenium 和 Java 从强标签的兄弟文本节点中提取文本
- laravel - Laravel:连接 AWS ElasticCache Redis 超时
- docker - docker desktop windows不会为wsl2安装docker守护进程
- flutter - 没有为类“WallpaperImages”定义方法“DefaultCacheManager”。扑
- javascript - 如何根据条件获得独特的物品
- java - 有没有比解析堆栈跟踪以获取当前正在运行或调用的方法更好的方法?
- wordpress - Wordpress 自定义端点显示 404