首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Foreach循环只在表中放置第一个条目,然后退出它。

Foreach循环只在表中放置第一个条目,然后退出它。
EN

Stack Overflow用户
提问于 2021-04-26 16:13:17
回答 1查看 53关注 0票数 0

我的前轮有个小问题。它循环遍历表,但只显示对应表中的第一个条目。之后,所有条目都在表之外。我希望它们出现在表中,我猜想这与SELECT语句有关。但我不确定。你能告诉我为什么这些条目不在表上吗?

显示条目

代码片段

代码语言:javascript
复制
    <?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
?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-26 16:24:45

你把</tbody></table>锁在前额。试试下面的代码。

代码语言:javascript
复制
<?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 } ?>

代码语言:javascript
复制
<?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  } ?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67270162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档