首页 > 解决方案 > 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
?>

标签: phphtmlwordpress

解决方案


</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  } ?>

推荐阅读