Reputation: 65
I have 2 functions (Located in same file)
f1 = getBlogCommentList
f2 = getBlogReplyList
I want function f1 to call f2 $this->getBlogReplyList($post_id,$comment['id']);
, f2 function collect data from sql based on f1 sent parameters and returns array with data return $blog_replies;
. And returned data I want to use in f1, but can't understand how to reach this returned data. foreach ($blog_replies as $reply) { //Do stuff with returned data }
Notice: Undefined variable: blog_replies in D:\xampp\htdocs\models\BlogModel.php on line 146
Warning: Invalid argument supplied for foreach() in D:\xampp\htdocs\models\BlogModel.php on line 146
In Line 146 I have foreach($blog_replies as $replies)
f1 function (getBlogCommentList)
public function getBlogCommentList($post_id){
try{
$sortby = "bla bla bla";
$stmt = $this->conn->prepare("$sortby");
$stmt->execute();
$result = $stmt->fetchAll();
$blog_comments = array();
foreach($result as $comment){
$blog_comments[] = $comment;
$this->getBlogReplyList($post_id,$comment['id']);
}
foreach ($blog_replies as $reply) {
//Do stuff with returned data
}
return $blog_comments;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
f2 function (getBlogReplyLyst)
public function getBlogReplyList($post_id,$comment_id){
try{
$sortby = "bla bla bla";
$stmt = $this->conn->prepare("$sortby");
$stmt->execute();
$result = $stmt->fetchAll();
$blog_replies = array();
foreach($result as $post){
$blog_replies[] = $post;
}
return $blog_replies;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
Upvotes: 1
Views: 41
Reputation: 3621
You need to assign the output of the called function to a variable, because the variables are only accessible within your function, i.e.:
$blog_replies = $this->getBlogReplyList($post_id,$comment['id']);
foreach ($blog_replies as $reply) {
//Do stuff with returned data
}
You could also consider using class variables, this way you can access the variable in each function of your object. Note the usage of $this->blog_relies
, the variable definition at the top and the removal of the return statement
Example:
class ExampleClass {
private $blog_replies;
public function getBlogReplyList($post_id,$comment_id){
try{
$sortby = "bla bla bla";
$stmt = $this->conn->prepare("$sortby");
$stmt->execute();
$result = $stmt->fetchAll();
$this->blog_replies = array();
foreach($result as $post){
$this->blog_replies[] = $post;
}
// The return is now obsolete
// return $blog_replies;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
public function getBlogCommentList($post_id){
try{
$sortby = "bla bla bla";
$stmt = $this->conn->prepare("$sortby");
$stmt->execute();
$result = $stmt->fetchAll();
$blog_comments = array();
foreach($result as $comment){
$blog_comments[] = $comment;
$this->getBlogReplyList($post_id,$comment['id']);
}
foreach ($this->blog_replies as $reply) {
//Do stuff with returned data
}
return $blog_comments;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
}
Upvotes: 2
Reputation: 464
Modify f1 as follows:
public function getBlogCommentList($post_id){
try{
$sortby = "bla bla bla";
$stmt = $this->conn->prepare("$sortby");
$stmt->execute();
$result = $stmt->fetchAll();
$blog_comments = array();
foreach($result as $comment){
$blog_comments[] = $comment;
$blog_replies = $this->getBlogReplyList($post_id,$comment['id']);
foreach ($blog_replies as $reply) {
//Do stuff with returned data
}
}
return $blog_comments;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
Upvotes: 0