943,020 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 903
  • PHP RSS
Aug 28th, 2010
0

Retrieving multidimensional array

Expand Post »
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
PHP Syntax (Toggle Plain Text)
  1. class DB{
  2. function news($user_id){
  3. $q="select all from news where author id='$id'";
  4. $result=$db->query($q);
  5. for($i=0; $i<$r->num_rows; $i++){
  6. $arr[]=$result->fetch_array;
  7. }
  8. return $arr; //works fine!
  9. }}

news.php
PHP Syntax (Toggle Plain Text)
  1. class News{
  2. function get_news($user_id){
  3. global $db;
  4. $news=$db->news($user_id);
  5. print_r($news); // works fine!
  6. return $news;
  7. }}

index.php // how to display data from News class (news.php)
PHP Syntax (Toggle Plain Text)
  1. $post=new News;
  2. $post->get_news($user_id);
  3. .. i tried
  4. for($i=0; $i<5; $i++){
  5. foreach($post[$i] as $k=>$v){
  6. echo $v; //get error: "Cannot use object of type News as array in ..."
  7. }
  8. }
Similar Threads
Reputation Points: 10
Solved Threads: 2
Newbie Poster
newprimitive is offline Offline
20 posts
since Aug 2010
Aug 28th, 2010
1
Re: Retrieving multidimensional array
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:
PHP Syntax (Toggle Plain Text)
  1. $post=new News;
  2. $data = $post->get_news($user_id);
  3.  
  4. //to verify that you got the array you were expecting
  5. print_r($data);
  6.  
  7. for($i=0; $i<5; $i++){
  8. foreach($data[$i] as $k=>$v){
  9. echo $v; //get error: "Cannot use object of type News as array in ..."
  10. }
  11. }


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
Reputation Points: 116
Solved Threads: 243
Veteran Poster
hielo is offline Offline
1,123 posts
since Dec 2007
Aug 28th, 2010
0
Re: Retrieving multidimensional array
PHP Syntax (Toggle Plain Text)
  1. $q="select all from news where author id='$id'";
Whuch Kind of database is his?
AFAIK, it should go like this:
PHP Syntax (Toggle Plain Text)
  1. $q="SELECT * FROM news WHERE author id='$id'"
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Aug 29th, 2010
0
Re: Retrieving multidimensional array
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:
PHP Syntax (Toggle Plain Text)
  1. for($i=0; $i<5; $i++){
  2. echo $data[$i]['title'].'<br />'.$data[$i]['body'].'<br />'..etc.;
  3. }
Reputation Points: 10
Solved Threads: 2
Newbie Poster
newprimitive is offline Offline
20 posts
since Aug 2010
Aug 29th, 2010
0
Re: Retrieving multidimensional array
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:
PHP Syntax (Toggle Plain Text)
  1. for($i=0; $i<5; $i++){
  2. echo $data[$i]['title'].'<br />'.$data[$i]['body'].'<br />'..etc.;
  3. }
Worked with that Query?
I'm curious what kind of DB you are using!
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: How to handle redirect when the request is sent using Ajax?
Next Thread in PHP Forum Timeline: SUM a column from a MySQL Query





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC