28, May 2012

Recursive function - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Recursive function
« previous next »
Pages: [1] Print
Instabuck - The easy way to sell digital products online

Author Topic: Recursive function  (Read 839 times)
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1525
6359 credits
Members referred : 8


Gimme all your cookies!!!


« on: May 05, 2006, 11:03:09 am »

Hi Guys,

It has been a long day and I think that I have been staring at this 24" LCD too long Cool

Have a look at this code and see if you can see anything wrong with it and if you can improve... I am trying to get the function to return an array of category ID's, but have ended up trying to get it to work using a string; but even that is not working! Grrr...

Code:
<?php
function getDirCrumbIDs($catID$str null)
{
   global 
$db;

   
$sql "select * from category where categoryid = '$catID' limit 1";

   if (
$row $db->get_row($sql))
   {
      
getDirCrumbIDs($row->CategoryParentID"$catID|".$str);
   }

   return 
$str;
}

echo 
getDirCrumbIDs($_GET['cat']);

?>


Trying to just echo the returned string, but it is outputting nothing. If I echo the $str in the function as it runs through all the categories, it definitely prints the correct values. The last echo is just not outputting anything.

Any ideas?
« Last Edit: May 05, 2006, 11:05:15 am by wineo »


Last blog : Canonical URL Links / Tags
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #1 on: May 05, 2006, 11:20:28 am »

That's because the last record has no parent. Right?

(I am out of office right now, and I couldn't take a closer look)

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1525
6359 credits
Members referred : 8


Gimme all your cookies!!!


« Reply #2 on: May 05, 2006, 04:47:11 pm »

If I add "echo $str;" before the line that returns $str it echos out a value that I can use, but it is not echoing it out on the last echo...


Last blog : Canonical URL Links / Tags
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #3 on: May 05, 2006, 05:25:34 pm »

Try this :

Code:
<?php
function getDirCrumbIDs($catID$str null)
{
   global 
$db;

   
$sql "select * from category where categoryid = '$catID' limit 1";

   if (
$row $db->get_row($sql))
   {
      
getDirCrumbIDs($row->CategoryParentID"$catID|".$str);
      return 
$str;
   }
}

echo 
getDirCrumbIDs($_GET['cat']);

?>


Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1525
6359 credits
Members referred : 8


Gimme all your cookies!!!


« Reply #4 on: May 05, 2006, 05:58:05 pm »

Nothing happens this way either... I think that way you have it, the function will never return a value.


Last blog : Canonical URL Links / Tags
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #5 on: May 05, 2006, 06:04:35 pm »

Hmm, it's not my day Smiley

I will check it again later. Recursive functions were never something that I was good.....

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1525
6359 credits
Members referred : 8


Gimme all your cookies!!!


« Reply #6 on: May 05, 2006, 06:11:49 pm »

I have altered it to a methid that works but I think that it really the best way of doing it...

Code:
<?php

function getDirCrumbIDs($catID)
{
   global 
$db;

   
$sql "select * from category where categoryid = '$catID' limit 1";

   if (
$row $db->get_row($sql))
   {
       
$string .= "$catID|";
       
$string .= getDirCrumbIDs($row->CategoryParentID);
   }

   return 
$string;
}

echo 
getDirCrumbIDs($_GET['cat']);

If anyone can comment on this method, please do so. It works but I just feel that it is an improper method...

Thanks!

... and thanks Nick for looking at it!
« Last Edit: May 05, 2006, 06:14:06 pm by wineo »


Last blog : Canonical URL Links / Tags
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=2426
Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Recursive function
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2012, 10:51:45 am





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.