Re: recursion question
| From: | Andreas Pour | Date: | Wed, 30 Aug 2000 16:38:39 +0000 |
| Subject: | Re: recursion question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-14431@lists.php.net to get a copy of this message | ||
"Robert J. Cissell" wrote:
>
> I'm trying to write my first recursive function but I'm not doing too well. I have a
> mysql database with the fields of topic_id, topic_name, and parent_topic_id. If I have a topic_id
> with a parent_topic_if of 5, I want the function to get the topic_name and parent_topic_id for 5,
> and again and again until it reaches a parent_topic_id of 0, which would be my top level. As it
> goes, it would store these values in an array which I can run through later to build a path from the
> top to whatever topic_id i started out at. What I can't figure is how to set up the recursive
> function to do so. I have no problem with the database stuff, but I need help with the function
> stuff. Any ideas or suggestions are greatly appreciated.
>
> Robert Cissell
Hmm, untested idea:
function getAttributes(id, &$name, $parent_id)
{
// code to get info for topic id from database and return
// values in 'name' and 'parent_id'
}
function getTopics(&$list, $id)
{
if ($id == 0)
return;
getAttributes($id, $name, $parent);
$list[] = array($name, $parent); // arrange as you wish
return getTopics($list, $parent);
}
$list = array();
getTopics($list, $id_of_interest);