简体   繁体   English

传递需要使用 foreach 循环从数据库中显示表的函数 PHP

[英]Passing function that needs to display a table from the database with a foreach loop PHP

Im making an overview for users that are administrators on my website.我正在为我网站上的管理员用户做一个概述。

The problem i am having is that i have no clue how to pass a function like the i want it to.我遇到的问题是我不知道如何传递我想要的函数。 I've tried searching around using arguments but nothing seemed like it would fix my problem.我试过使用参数四处搜索,但似乎没有什么能解决我的问题。

The code: I have 3 scripts (+ stylesheet) that are connected to each other代码:我有 3 个相互连接的脚本(+ 样式表)

adminUsers.php adminUsers.php

<?php
session_start();
require_once ("../model/handler.php");
require_once ("../model/gui_mod.php");
echo("<link rel='stylesheet' type='text/css' href='../stylesheet/stylesheet.css'>");

$gui_obj = new class_gui();

$gui_obj->admin();
$gui_obj->adminUsers();

handler.php (this page i make the connection to the database with var $conn) handler.php(这个页面我用 var $conn 连接到数据库)

function admins(){
    ?>
    <table>
        <?php 
            $query = "SELECT * FROM admin";
            $stmt = $conn->prepare($query);
            $stmt->execute();
            $result = $stmt->fetchall();

            foreach($result as $row){
        ?>

        <tr>
            <td></td>
            <td></td>
            <td></td>
        </tr>

        <?php 
            }
        ?>
    </table>
    <?php
}

gui_mod.php - This is where i make the Gui of the page gui_mod.php - 这是我制作页面 Gui 的地方

function adminUsers(){
        ?>
            <html>
                <body>

                    <div class="admin_container_head_bg">
                        <div class="admin_container_head_text_one">
                            <header>Administrator</header>
                        </div>
                        <div class="admin_container_head_text_two">
                            <p>Manager</p>
                        </div>
                    </div>
                    <div class="admin_users">
                        <div class="table_admins">

                            <?php admins(); ?>
                        </div>
                    </div>
                </body>
            </html>
        <?php
    }

I had the feeling that the problem is caused because i'm requesting a function inside another function.我感觉问题是因为我在另一个函数中请求一个函数。 But i honestly have no clue so if this is false please tell me :#但老实说我不知道​​所以如果这是假的请告诉我:#

You have to either declare the connection object global or pass it into the function.您必须将连接对象声明为全局或将其传递给函数。

function admins() {
    global $conn;
    ...

Or:或者:

function admins($conn) {
    ...
}

And then call by passing in the connection, like:然后通过传入连接来调用,例如:

admins($conn);

Otherwise it's not in scope.否则不在范围内。 Keep in mind you'll have to declare the connection object global from the other function or pass it in to then call admins from it, or you'll run into the exact same problem.请记住,您必须从另一个函数中声明全局连接对象或将其传递给然后从中调用admins ,否则您将遇到完全相同的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM