0

I have a question...How to pass a variable from a function to another in php.. My first function:

public function get($obj_id)
{
    $exclude = array();
    $query = $this->db->query("SELECT
                                a.id as id_article ,
                                a.title,
                                a.content,
                                a.date,
                                a.views,
                                a.smallimage,
                                a.largeimage,
                                tin.id,
                                t.id,
                                group_concat(t.name) as tags,
                                group_concat(t.id) as id_tag
                                from tags_in_news tin
                                inner join articles a on a.id = tin.news_id
                                inner join tags t on t.id = tin.tag_id
                                and a.id = $obj_id
                                group by a.id");
if ($query->num_rows()>0) 
{
    foreach ($query->result() as $cr) 
    {
        array_push($exclude,$cr->id_article);
        $newsIds = implode(',',array_values($exclude));
    }
}
else
{
    $newsIds = 0;
}
    echo "<pre>"; print_r($newsIds); echo "</pre>"; 
return $query->row_array();
}

and the second function:

public function getLastArticles()
{

    $this->load->database();


    $last_articles = $this->db->query("SELECT
                                * FROM articles WHERE id NOT IN (".($newsIds).")
                                 ORDER BY date desc LIMIT 3");
    if ($last_articles->num_rows())
    {
        $last_articles = $last_articles->result_array();
    }
    else
    {
        $last_articles = NULL;
    }

    return $last_articles;
}

When I write print_r($newsIds) I get correct id but whnen I want to using in a second method the error is Undefined variable: newsIds help me please..Thx

user3611170
  • 33
  • 1
  • 8
  • because `$newsIds` is not declared in second function. The variable declared inside function will have scope only for that function.so you need to make `$newsIds` as a global variable or declare `$newsIds` again in second function – krishna May 14 '14 at 09:53
  • I now, but how I can get the $newsIds value in the second function? – user3611170 May 14 '14 at 09:55
  • make $newsIds as global variable – krishna May 14 '14 at 09:55
  • I tried but no result – user3611170 May 14 '14 at 09:56
  • What do these two functions have to do with one another? How are they connected? Who is calling that second function from where? You somehow need to pass the data along in a logical fashion, returning it from the first and passing it to the second. – deceze May 14 '14 at 09:56
  • I resolved, I declared $newsIds global – user3611170 May 14 '14 at 09:59

0 Answers0