Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Dedicated server web hosting provided by Guru-host.eu.
Recursion

 
Post new topic   Reply to topic    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
ping
Smarty Rookie


Joined: 07 Jan 2005
Posts: 7
Location: NoVa

PostPosted: Tue Mar 15, 2005 6:54 pm    Post subject: Recursion Reply with quote

I'm writing a little function plugin for my postnuke module. I want to make it a recursive function, that is, call itself from within itself if a certain condition is true, then return the same information. I think I've got everything set up but the call back to the function. Is it possible to do this from within the a function?
_________________
One Ping Only
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
boots
Administrator


Joined: 16 Apr 2003
Posts: 5613
Location: Toronto, Canada

PostPosted: Tue Mar 15, 2005 7:08 pm    Post subject: Reply with quote

If its in your plugin, then it is just PHP and so yes, of course. You just need to figure out a way to deal with the flags to stop the recursion. If it is in template, there is a plugin for that presented in the Tips & Tricks forum. It would probably be helpful if you posted some relevant code Wink
Back to top
View user's profile Send private message
ping
Smarty Rookie


Joined: 07 Jan 2005
Posts: 7
Location: NoVa

PostPosted: Tue Mar 15, 2005 7:23 pm    Post subject: Reply with quote

The thread I saw seemed to have almost the opposite problem. I can get it to call the function again (yay!) but it's not passing the variable.

Code:
function smarty_function_track_proposalsdisplayitem($params, &$smarty)
{
    extract($params);
   unset($params);
   
   ...
   
   if ($item['original']<>0){
      
      $item=$item['orgitem'];
      $displayitem.=smarty_function_track_proposalsdisplayitem($item);
   }
   
    return $displayitem;
}


I'm pretty sure I'm passing the parameters wrong, but I've no idea how to fix that. Any advice?
_________________
One Ping Only
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
ping
Smarty Rookie


Joined: 07 Jan 2005
Posts: 7
Location: NoVa

PostPosted: Tue Mar 15, 2005 8:10 pm    Post subject: Reply with quote

I took a step outside the box to solve this one. The Postnuke stuff was confusing me, so I added another function to the mandatory function and used recursion on THAT function. So I came up with this elegant little bit of coding:

Code:
//My happy recursive function
function recurseit($item)
{
   $tableit = "<table>";
   $tableit .= "<tr><td align=\"right\">"._TITLE.":</td><td align=\"left\">".$item['p_title']."</td></tr>\n";
   $tableit .= "<tr><td align=\"right\">"._DURATION.": </td><td align=\"left\">".$item['duration']."</td></tr>\n";
   $tableit .= "<tr><td align=\"right\">"._INSTITUTE.": </td><td align=\"left\">".$item['institute']."</td></tr>\n";
   $tableit .= "<tr><td align=\"right\">"._ADDRESS.": </td><td align=\"left\">".$item['address']."</td></tr>\n";
   $tableit .= "<tr><td align=\"right\">"._PHONE.": </td><td align=\"left\">".$item['phone']."</td></tr>\n";
   $tableit .= "<tr><td align=\"right\">"._FAX.": </td><td align=\"left\">".$item['fax']."</td></tr>\n";
   $tableit .= "</table>";
   
   if ($item['original']<>0){
      $tableit.="<h4>Original proposal</h4>";
      $item=$item['orgitem'];
      $tableit.=recurseit($item);
   }
   
    return $tableit;   
}
 
/**
 * Smarty function to display admin links for the track_proposals module
 * based on the user's permissions
 *
 */
function smarty_function_track_proposalsdisplayitem($params, &$smarty)
{
    extract($params);
   unset($params);
   
   $displayitem=recurseit($item);
   
    return $displayitem;
}

?>

_________________
One Ping Only
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP