Author Topic: cakephp: recursive function not passing variables properly  (Read 1180 times)

0 Members and 1 Guest are viewing this topic.

Offline thewooleymammothTopic starter

  • Devotee
  • Posts: 757
  • Gender: Male
  • I am the soul collecter
    • View Profile
    • My Portfolio
cakephp: recursive function not passing variables properly
« on: November 11, 2009, 01:40:12 PM »
i have a recursive function which takes an array of arrays of data from the database and turns them into a menu of catagories which can be expanded. To accomplish this im using a recursive function that checks if its parent catagory is a catagory that is being expanded if so, it expands the catagory by calling the function again if not it is skipped. The problem is when i pass the new parent variable to the new function, it uses the old parent variable instead.

I know my explanation is confusing, but i dont know how to express it any better, i will post my code and an example output and hopefully it will make sense, if it doesnt, please let me know

Controller page
Code: [Select]
<?php
class ProductsController extends AppController {

var $name 'Products';
    var 
$uses = array('Product''Catagory');
    function 
index()
    {
        
$this->redirect('/Products/browse/');
    }
    function 
_menu($ex=NULL)
    {
        
/*
         *Generate Menu
        */
       // $ex=explode('-', $ex);
        
$catss=$this->Catagory->find('all');
        if(
$ex!=NULL)
            
$ex=explode("-"$ex);
        else
        {
          
$ex=array();
        }
       foreach(
$catss as $cats){
          foreach(
$cats as $key=>$cat)
          {
            
$id=$cat['id'];
            
$menu[$id]=$cat;
          }
          
//$this->set('ex', $ex);
          //$this->set('menu', $menu);
        
}
        function 
disp($menu$p$ex=array(), $html='')
        {
              foreach(
$menu as $key=>$m){
                if(
trim($m['parents'])==trim($p) && !empty($p)){
                  
$np=$m['parents'].$m['names'].'/';
                  
$html.="<div style='margin-left: 10px;'>";
                  if(
in_array($m['id'], $ex)){
                    
$expand='';
                    foreach(
$ex as $key=>$e)
                    {
                        if(
$e != $m['$id'])
                        {
                          
$expand.="$e-";
                        }
                        
$expand=str_replace($m['id'].'-'''$expand);
                    }
                    
$html.= "<a href='/Products/Browse/$expand/".$m['id']."/'>".$m['names'].'</a> '.$p.' => '.$np.'
                                    <br />
                                    '
;

                      
$html.=disp($menu$np$ex$html);
                    }
                    else
                    {
                      
$expand='';
                      foreach(
$ex as $key=>$e)
                      {
                            
$expand.="$e-";
                      }
                      
$expand.=$m['id'];
                      
$html.= "<a href='/Products/Browse/$expand/".$m['id']."/'>".$m['names'].'</a>
                                        <br />
                                </div>
                                        '
;
                  }
                  
$html.= "
                        </div>
                      "
;
                      return 
$html;
                }
              }
        }
    
$out=disp($menu'root/'$ex);
    
$this->set('out'$out);
    }
    function 
browse($ex=NULL$prim=NULL){
      
$this->_menu($ex);

    }
}
?>

output for /products/browse/
Code: [Select]
<div style='margin-left: 10px;'>
   <a href='/Products/Browse/1/1/'>flowers</a>
    <br />
</div>
output for /Products/Browse/1/1
Code: [Select]
<div style="margin-left: 10px;">
   <a href="/Products/Browse//1/">flowers</a> root/ => root/flowers/
   <br/>
   <div style="margin-left: 10px;">
      <a href="/Products/Browse//1/">flowers</a> root/ => root/flowers/
      <br/>
      <div style="margin-left: 10px;">
         <a href="/Products/Browse/1-2/2/">red</a>
         <br/>
      </div>
   </div>
</div>
output for /products/browse/1-2/
Code: [Select]
<div style="margin-left: 10px;">
   <a href="/Products/Browse/2-/1/">flowers</a> root/ => root/flowers/
   <br/>
   <div style="margin-left: 10px;">
      <a href="/Products/Browse/2-/1/">flowers</a> root/ => root/flowers/
      <br/>
         <div style="margin-left: 10px;">
            <a href="/Products/Browse/1-/2/">red</a> root/flowers/ => root/flowers/red/
            <br/>
            <div style="margin-left: 10px;">
               <a href="/Products/Browse/2-/1/">flowers</a>root/ => root/flowers/
               <br/>
                  <div style="margin-left: 10px;">
                     <a href="/Products/Browse/1-/2/">red</a> root/flowers/ => root/flowers/red/
                     <br/>
                     <div style="margin-left: 10px;">
                        <a href="/Products/Browse/1-2-3/3/">Epic</a>
                        <br/>
                     </div>
                  </div>
               </div>
            </div>
i cant figure out why it will sometimes pass the variable and sometimes it will not. Any help appreciated, i know its confusing so please ask if you dont understand