954,178 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Retrieving multidimensional array

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 ..."
   }
}
newprimitive
Newbie Poster
21 posts since Aug 2010
Reputation Points: 10
Solved Threads: 2
 

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

hielo
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 243
 
$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'"
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

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'].''.$data[$i]['body'].''..etc.;
}
newprimitive
Newbie Poster
21 posts since Aug 2010
Reputation Points: 10
Solved Threads: 2
 

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'].''.$data[$i]['body'].''..etc.;
}

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

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: