Fork me on GitHub

Calling a function from within itself -solved  Bottom

  • I have a function in the templates folder of my module. I want to make it recursive, so that, under certain conditions, it calls itself, then returns the value to itself to add at the end of the string I'm creating. How do I call it from within itself? Do I need to call the whole thing, or is there a special PN call, or ... what?

    This is the code I have right now:

    Code

    function smarty_function_track_proposalsdisplayitem($params, &$smarty)
    {
        extract($params);
        unset($params);
       
        $displayitem = "<table>";
        $displayitem .= "<tr><td align=\"right\">"._TITLE.":</td><td align=\"left\">".$item['p_title']."</td></tr>\n";
        $displayitem .= "<tr><td align=\"right\">"._DURATION.": </td><td align=\"left\">".$item['duration']."</td></tr>\n";
        $displayitem .= "<tr><td align=\"right\">"._INSTITUTE.": </td><td align=\"left\">".$item['institute']."</td></tr>\n";
        $displayitem .= "<tr><td align=\"right\">"._ADDRESS.": </td><td align=\"left\">".$item['address']."</td></tr>\n";
        $displayitem .= "<tr><td align=\"right\">"._PHONE.": </td><td align=\"left\">".$item['phone']."</td></tr>\n";
        $displayitem .= "<tr><td align=\"right\">"._FAX.": </td><td align=\"left\">".$item['fax']."</td></tr>\n";
        $displayitem .= "</table>";
       
        if ($item['original']<>0){
            displayitem.=/*A miracle happens*/;
        }
        return $displayitem;
    }
  • So I poked it a few times, and I came up with this to replace the above if statement:

    Code

    if ($item['original']<>0){
            $displayitem.="<h4>Original proposal</h4>";
            $item=$item['orgitem'];
            $displayitem.=smarty_function_track_proposalsdisplayitem($item);
        }


    Problem: I am pretty sure I'm not passing the information correctly. I may not even be calling the function properly. Any help?
  • This was getting too complicated to solve, and I couldn't find the answers I needed, so I thought "outside the box," or in this case, the function. I added another function to the file called "recurseit" that basically did what I had been (unsuccessfully) programming the Smarty function to do, and it returns the final result to the Smarty function, which returns it to the template, and life is happiness. Here's the code for the file:

    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;
    }

    ?>
  • 0 users

This list is based on users active over the last 60 minutes.