I have created an array in my functions.php file to be accessed on another page. I then returned the array and called it on a different page. This is what I have:
In my functions.php file I have
public function getpostcontent($userid){
include('db-conx.php');
$getval = "SELECT `content`,`date` FROM posts WHERE userid = ?";
$stmt = $conn->stmt_init();
if ($stmt->prepare($getval))
{
//$userid = $_SESSION['userid'];
$stmt->bind_param("s", $userid);
$stmt->execute();
$stmt->bind_result($content, $date);
while ($stmt->fetch()) {
$displayname = getdisplayname($userid);
$array = [
"content" => $content,
"date" => $date,
];
}
return $array;
}
}
And I access the array on my other page using this.
$posts = new getposts();
$returned=$posts->getpostcontent($userid);
foreach($returned as $val)
{
echo $val['content'];
}
I've tried all the solutions I found on google and elsewhere with no luck. It works when I use print_r($val); to retrieve all elements in the array but throws the 'Illegal string offset' when I try to access them individually. Help?
print_r($val)array? Let's see what it looks like.$arrayto a one-dimensional array ingetpostcontentloop, soreturnedin your main code will be only one dimension ($returned['content']and$returned['date']).