Returning multiple arrays

How can I return multiple arrays.

Page 1 of 1

5 Replies - 922 Views - Last Post: 27 November 2010 - 09:04 AM Rate Topic: -----

#1 DjDark1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 23-April 10

Returning multiple arrays

Posted 26 November 2010 - 12:46 PM

Hey all,

I have a basic forum system set up (one I created myself) and one particular feature in the forum topic page is the option to "tag this topic" this essentially adds this topic to a favourite topics list. One example row in the "tagged_topics" table looks a bit like this layout:
topic_id: 56
username: DjDark1
date_added: Wednesday, November 3rd 2010, 15:40 GMT
date_added_timestamp: 1288798836
There is no issue adding data to the database but the problem arises to bring it back out.

So on my actual "tagged topics" page, I wish to retrieve every topic tagged by the user, I can do this by searching for a username and returning an array.
$q = "SELECT * FROM ".TB_FAVOURITETOPICS." where username = '$session->username'";
$result = mysql_query($q);
if(!$result || (mysql_numrows($result) < 1)){
     return null;
}
$TopicResults = mysql_fetch_array($result);


Now another function within this page is to submit a topic id and it will check that topic to see if it exists and we can access it.
$forum->checktopic($topicid); returns an array with all the topic information.



What I want to do is set up a loop that loops through all the "topic_id" in the users tagged topics, then for each id check the topic so that it returns the array then output an array for each topic. This will all be done in one function preferably.

So far I have wrote this:
   function getTaggedTopics(){
        global $session;
        $q = "SELECT * FROM ".TB_FAVOURITETOPICS." where username = '$session->username'";
        $result = mysql_query($q);
	    if(!$result || (mysql_numrows($result) < 1)){
	       return null;
	    }
        $TopicResults = mysql_fetch_array($result);
        for ($j=0; $j<count($TopicResults); $j++){
            $forum->checkTopic($TopicResults['topic_id']);
        }
   }



This does not work. I'm sorry for my very poor explanation of how this is going to work, but essentially you can see I want to check each topic_id and if successful this will return an array with the topic info such as the topic title, date posted etc... but a different array will be returned for every topic so really I want to return all these arrays for how many topics the user has tagged.


Thanks in advance for anyone that helps, if you don't need anymore elaboration, I'll try and be clearer.

Regards,

DjDark1 :)

Is This A Good Question/Topic? 0
  • +

Replies To: Returning multiple arrays

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: Returning multiple arrays

Posted 26 November 2010 - 03:13 PM

Try looping through the results.
function getTaggedTopics(){
     global $session;
     $q = "SELECT * FROM ".TB_FAVOURITETOPICS." where username = '$session->username'";
     $result = mysql_query($q);
  if(!$result || (mysql_num_rows($result) < 1)){
     return null;
  }
     while($TopicResults = mysql_fetch_array($result))
     {
        for ($j=0; $j<count($TopicResults); $j++){
            $forum->checkTopic($TopicResults['topic_id']);
        }
     }
}


This post has been edited by Dormilich: 26 November 2010 - 03:19 PM
Reason for edit:: using the proper function

Was This Post Helpful? 0
  • +
  • -

#3 DjDark1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 23-April 10

Re: Returning multiple arrays

Posted 27 November 2010 - 08:10 AM

Using the above code, I still recieve the error: "Call to a member function checkTopic() on a non-object". If I echo
$TopicResults['topic_id']
before I check it, I only get the first topic that I have tagged and no others.

I was thinking of another solution but not quite sure how I would implement it, would it not be easier to use a foreach loop like
foreach tagged topic{
check the topic
return data
}repeat loop foreach

Any other suggestions would be appreciated :)

EDIT: In case you didn't realise, what I am trying to do is on the tagged_topics.php page. Have a piece of code that says
$MyTaggedTopics = $forum->getTaggedTopics();
//then something like
//for each topic I have
echo $MyTaggedTopics['topic_title'];
//etc.. in that fashion.



Is this even possible since that $MyTaggedTopics will have multiple arrays returned?

This post has been edited by DjDark1: 27 November 2010 - 08:13 AM

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: Returning multiple arrays

Posted 27 November 2010 - 08:43 AM

I have no idea what you mean by a page. Pages are for browsers. The server deals with files.

You're using:
         $forum->checkTopic($TopicResults['topic_id']);

inside a function, so you either need to make $forum global or define and instantiate the class inside it.
Was This Post Helpful? 0
  • +
  • -

#5 DjDark1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 23-April 10

Re: Returning multiple arrays

Posted 27 November 2010 - 08:54 AM

View PostCTphpnwb, on 27 November 2010 - 02:43 PM, said:

I have no idea what you mean by a page. Pages are for browsers. The server deals with files.

You're using:
         $forum->checkTopic($TopicResults['topic_id']);

inside a function, so you either need to make $forum global or define and instantiate the class inside it.


I see what you are saying, I got this to work by changing it to:

return $this->checkTopic($TopicResults['topic_id']);


So on tagged_topics page, I currently have this contained within it:

<? $ForumTopics = $forum->getTaggedTopics(); ?>
<? print_r($ForumTopics); ?>


But this only ever prints the first topic the user has tagged, say the user tagged multiple topics, is there a way in which I could get this to work in one function or will it require two. One to get each tagged topic then one to retrieve data for each topic?
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: Returning multiple arrays

Posted 27 November 2010 - 09:04 AM

Depending on your database it could possibly be done with a single query.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1