Hello,
In OOP PHP, i got 2 classes: Database and News. In News class i call DB function and get result (multidimensional 2D array). Now i have to display that array in index.php. I call News class (lets say get_news() function) from index.php but cant retrieve data and always get message: "Cannot use object of type News as array in ..."

Here is some of my code:
database.php

class DB{
function news($user_id){
$q="select all from news where author id='$id'";
$result=$db->query($q);
for($i=0; $i<$r->num_rows; $i++){
  $arr[]=$result->fetch_array;
}
return $arr; //works fine!
}}

news.php

class News{
function get_news($user_id){
global $db;
$news=$db->news($user_id);
print_r($news);        // works fine!
return $news;
}}

index.php // how to display data from News class (news.php)

$post=new News;
$post->get_news($user_id);
.. i tried
for($i=0; $i<5; $i++){
  foreach($post[$i] as $k=>$v){
      echo $v;         //get error: "Cannot use object of type News as array in ..."
   }
}

Generally, when a function/method call returns something, and you need that something later on, you need to "receive/assign" it to something else. Your $post->get_news($user_id); returns an array, but there is NOTHING "receiving" that array. Also, just because your method get_news() returns an array, it does NOT "overwrite" the $post variable. Thus, after the method call, the $post variable is still a 'News' object. So $post[$i] is not valid - again, because $post is NOT an array - it is still a News object. So try:

$post=new News;
$data = $post->get_news($user_id);

//to verify that you got the array you were expecting
print_r($data);

for($i=0; $i<5; $i++){
  foreach($data[$i] as $k=>$v){
      echo $v; //get error: "Cannot use object of type News as array in ..."
   }
}

On another note, the only similarity between your thread and the other was that both of you have the need to retrieve data from the db. You are way ahead of the game since you already have some php code in place that needs minor fixing. The other poster didn't seem to have anything in place yet.

Regards,
Hielo

$q="select all from news where author id='$id'";

Whuch Kind of database is his?
AFAIK, it should go like this:

$q="SELECT * FROM news WHERE author id='$id'"

Thanks Hielo, i totally forgot to put some variable that would receive returned value. To display retrieved data, one simple for loop worked just fine:

for($i=0; $i<5; $i++){
    echo $data[$i]['title'].'<br />'.$data[$i]['body'].'<br />'..etc.;
}

Thanks Hielo, i totally forgot to put some variable that would receive returned value. To display retrieved data, one simple for loop worked just fine:

for($i=0; $i<5; $i++){
    echo $data[$i]['title'].'<br />'.$data[$i]['body'].'<br />'..etc.;
}

Worked with that Query? :icon_eek:
I'm curious what kind of DB you are using!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.