Print array on a different page than the class it's into
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);
}
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);
}
If myarray
is an attribute of the class user
, then in your feedback.php page, you would need:
- An instance of the class
user
(e.g.$user = new user();
) - Access
myarray
by using this syntax:$user->myarray
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']));
精彩评论