如何在一页中多次回显数组?

发布于 2024-11-08 16:37:04 字数 953 浏览 2 评论 0原文

我想知道是否有人可以帮助我解决这个问题。 我想在同一页面上多次打印/回显该函数的解决方案。这可能吗?

这是我的函数:

public function getFeedback($p_iUserid) {

    include("Connection.php"); //open db

    try
    {
        $sql = "select DiaryOpmerkingen, DiaryDoctorcomment from tblDiary 
                WHERE fk_UserId = ".$p_iUserid."
                AND DiaryDay = '".$this->Day."';";
        $rResult = mysqli_query($link, $sql);
        return $rResult;
    }
    catch(Exception $e)
    {
        // no connection database
        $feedback = $e->getMessage();
    }
    mysqli_close($link);
}

这​​就是我现在设法调用该函数的方式。但它只能工作一次:

if(mysqli_num_rows($feedbackPatient) > 0)
                    {
                        while($oUser = mysqli_fetch_assoc($allUsers))
                        {
                            echo $oUser['DiaryOpmerkingen'];
                        }
                    }

我希望有人能帮忙! 不管怎样,谢谢。

I was wondering if someone could help me with this problem.
I want to print/echo the solution of the function multiple times on the same page. Is that possible?

Here's my function:

public function getFeedback($p_iUserid) {

    include("Connection.php"); //open db

    try
    {
        $sql = "select DiaryOpmerkingen, DiaryDoctorcomment from tblDiary 
                WHERE fk_UserId = ".$p_iUserid."
                AND DiaryDay = '".$this->Day."';";
        $rResult = mysqli_query($link, $sql);
        return $rResult;
    }
    catch(Exception $e)
    {
        // no connection database
        $feedback = $e->getMessage();
    }
    mysqli_close($link);
}

And this is how I manage to call on the function by now. But it only works once:

if(mysqli_num_rows($feedbackPatient) > 0)
                    {
                        while($oUser = mysqli_fetch_assoc($allUsers))
                        {
                            echo $oUser['DiaryOpmerkingen'];
                        }
                    }

I hope someone can help!
Thanks anyway.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帅气称霸 2024-11-15 16:37:04

为您的打印任务创建一个函数,然后根据需要多次调用它:

function print_the_stuff($feedbackPatient){

if(mysqli_num_rows($feedbackPatient) > 0)
                    {
                        while($oUser = mysqli_fetch_assoc($allUsers))
                        {
                            echo $oUser['DiaryOpmerkingen'];
                        }
                    }
}

然后,无论您需要在哪里打印:

print_the_stuff($feedbackPatient);

Create a function for your printing task, then call it as many times as you want:

function print_the_stuff($feedbackPatient){

if(mysqli_num_rows($feedbackPatient) > 0)
                    {
                        while($oUser = mysqli_fetch_assoc($allUsers))
                        {
                            echo $oUser['DiaryOpmerkingen'];
                        }
                    }
}

Then, wherever you need to print:

print_the_stuff($feedbackPatient);
扛起拖把扫天下 2024-11-15 16:37:04

最简单的方法可能是一次性获取整个结果集,然后将其传递。

如果结果集很大,这可能会降低性能,但可能不会产生显着差异。使用 mySqli 扩展尤其容易,因为您可以使用 mysql_fetch_all

public function getFeedback($p_iUserid) {

    include("Connection.php"); //open db

    try
    {
        $sql = "select DiaryOpmerkingen, DiaryDoctorcomment from tblDiary 
                WHERE fk_UserId = ".$p_iUserid."
                AND DiaryDay = '".$this->Day."';";
        $rResult = mysqli_query($link, $sql);
        return mysqli_fetch_all($rResult, MYSQLI_ASSOC);
    }
    catch(Exception $e)
    {
        // no connection database
        $feedback = $e->getMessage();
    }
    mysqli_close($link);
}

然后,您将获得从 getFeedback 返回的关联数组,并且可以像平常一样循环遍历:

$feedback = getFeedback($id);
foreach ($feedback as $item) {
    echo $item['DiaryOpmerkingen'];
}

The easiest way to do this is probably to get the whole result set in one go, and then pass that around.

This might be costly in terms of performance if the result set is large, but it's probably not going to make a significant difference. It is particularly easy with the mySqli extension, because you can use mysql_fetch_all.

public function getFeedback($p_iUserid) {

    include("Connection.php"); //open db

    try
    {
        $sql = "select DiaryOpmerkingen, DiaryDoctorcomment from tblDiary 
                WHERE fk_UserId = ".$p_iUserid."
                AND DiaryDay = '".$this->Day."';";
        $rResult = mysqli_query($link, $sql);
        return mysqli_fetch_all($rResult, MYSQLI_ASSOC);
    }
    catch(Exception $e)
    {
        // no connection database
        $feedback = $e->getMessage();
    }
    mysqli_close($link);
}

You would then get an associative array returned from getFeedback, and could loop through this as normal:

$feedback = getFeedback($id);
foreach ($feedback as $item) {
    echo $item['DiaryOpmerkingen'];
}
攒眉千度 2024-11-15 16:37:04

我认为应该是:

$feedbackPatient = getFeedback($p_iUserid);
    if(mysqli_num_rows($feedbackPatient) > 0)
                        {
                            while($oUser = mysqli_fetch_assoc($feedbackPatient))
                            {
                                echo $oUser['DiaryOpmerkingen'];
                            }
                        }

为什么你对 mysqli_num_rows 和 sqli_fetch_assoc 使用 diff 结果?

I think it should be :

$feedbackPatient = getFeedback($p_iUserid);
    if(mysqli_num_rows($feedbackPatient) > 0)
                        {
                            while($oUser = mysqli_fetch_assoc($feedbackPatient))
                            {
                                echo $oUser['DiaryOpmerkingen'];
                            }
                        }

why are u using diff result for mysqli_num_rows and sqli_fetch_assoc ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文