在与其所属类不同的页面上打印数组

发布于 2024-11-09 04:59:38 字数 745 浏览 0 评论 0原文

我有这个数组,我想在其他地方打印它。 我已将其存储在 user.class.php 中,但我想将其打印在 Feedback.php 中。

我该怎么做?因为它一直告诉我它不知道“$myarray”。

请帮忙:)谢谢。

public function getFeedback($p_iUserid) {
    include("Connection.php"); //open db

    try
    {
        $sql = "SELECT FeedbackPatient FROM tblFeedback 
                WHERE fk_UserId = ".$p_iUserid."";

        $result = mysqli_query( $link, $sql );

        while( $row=mysqli_fetch_assoc($result) )
        {
            $myarray[] = $row['FeedbackPatient'];
            print_r($myarray);
        }
        mysqli_free_result( $result );
    }
    catch(Exception $e)
    {
        // no connection database
        $feedback = $e->getMessage();
    }
    mysqli_close($link);
}

I have this array and I want to print it somewhere else.
I have stored it in a user.class.php but I want to print it in feedback.php.

How do I do this? Because it keeps on telling me that it doesn't know '$myarray'.

Please help :) Thanks.

public function getFeedback($p_iUserid) {
    include("Connection.php"); //open db

    try
    {
        $sql = "SELECT FeedbackPatient FROM tblFeedback 
                WHERE fk_UserId = ".$p_iUserid."";

        $result = mysqli_query( $link, $sql );

        while( $row=mysqli_fetch_assoc($result) )
        {
            $myarray[] = $row['FeedbackPatient'];
            print_r($myarray);
        }
        mysqli_free_result( $result );
    }
    catch(Exception $e)
    {
        // no connection database
        $feedback = $e->getMessage();
    }
    mysqli_close($link);
}

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

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

发布评论

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

评论(3

开始看清了 2024-11-16 04:59:38

我只是将其作为函数的返回:

public function getFeedback($p_iUserid) {
    include("Connection.php"); //open db

    $myarray = array();
    try
    {
        $sql = "SELECT FeedbackPatient FROM tblFeedback 
                WHERE fk_UserId = ".$p_iUserid."";

        $result = mysqli_query( $link, $sql );

        while( $row=mysqli_fetch_assoc($result) )
        {
            $myarray[] = $row['FeedbackPatient'];
        }
        mysqli_free_result( $result );
    }
    catch(Exception $e)
    {
        // no connection database
        $feedback = $e->getMessage();
    }
    mysqli_close($link);
    return $myarray;
}

然后在 feedback.php 中:

$myarray = $user->getFeedback($user_id);
if($myarray) {
  // do something with $myarray
  print_r($myarray);
}

I would just make it a return of the function:

public function getFeedback($p_iUserid) {
    include("Connection.php"); //open db

    $myarray = array();
    try
    {
        $sql = "SELECT FeedbackPatient FROM tblFeedback 
                WHERE fk_UserId = ".$p_iUserid."";

        $result = mysqli_query( $link, $sql );

        while( $row=mysqli_fetch_assoc($result) )
        {
            $myarray[] = $row['FeedbackPatient'];
        }
        mysqli_free_result( $result );
    }
    catch(Exception $e)
    {
        // no connection database
        $feedback = $e->getMessage();
    }
    mysqli_close($link);
    return $myarray;
}

Then in feedback.php:

$myarray = $user->getFeedback($user_id);
if($myarray) {
  // do something with $myarray
  print_r($myarray);
}
虚拟世界 2024-11-16 04:59:38

如果 myarrayuser 类的属性,那么在您的 Feedback.php 页面中,您将需要:

  1. user 类的实例(例如 $user = new user();)
  2. 使用以下语法访问 myarray$user->myarray

If myarray is an attribute of the class user, then in your feedback.php page, you would need:

  1. An instance of the class user (e.g. $user = new user();)
  2. Access myarray by using this syntax: $user->myarray
花心好男孩 2024-11-16 04:59:38

另一种方法是将数组序列化并存储在会话变量中:

$data = serialize($myarray);
$_SESSION['data'] = $data;

var_dump($_SESSION['data']);

var_dump(unserialize($_SESSION['data']));

Another way is to serialize and store the array in a session variable:

$data = serialize($myarray);
$_SESSION['data'] = $data;

var_dump($_SESSION['data']);

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