SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 39 of 39
  1. #26
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by oikram
    How can we deal with the iteration in order to allow the display of both?
    Inside the controller you would pass the parent_id to getSubCategorias().

    PHP Code:
    function seccao($id) {
        
        
    /*
        * Fetch category data from model
        */
        
    $cat $this->M_Categoria->getCategoria($id)

        
    /*
        * ?
        */   
        
    $data['menuprincipal'] = $this->M_Categoria->getCategoriasPrincipais();

        
    /*
        * Fetch category tree lower level categories - 2 levels 
        */
        
    $data['menucategorias'] = $this->M_Categoria->getSubCategorias($cat->parent_id_cat,2);
        
        
    /*
        * load data in template
        */
        
    $data['main'] = 'v_categoria';
        
    $data['titulo'] = "As nossas soluções para ".$cat['nome_cat'];
        
    $this->load->vars($data);
        
    $this->load->view('geral_tpl');


    Once that is done you should be able to place the below into the view. Again, I'm not very familiar with CodeIgnitor but the concept is the same. Call the function and it will build the HTML.

    PHP Code:
    function parse_into_menu($arrMenu,$strChildKey,$strNameKey,$strIdKey,$strScript,$intRunner=0) {
        
        
    $strNL "\n";
        
    $intMenu count($arrMenu);
        
        for(
    $i=0;$i<$intMenu;$i++) {
            if(
    $i==0) { 
                echo 
    str_repeat("\t",$intRunner),'<ul>',$strNL
            }
            
            if(!empty(
    $arrMenu[$i][$strChildKey])) {
                echo 
    str_repeat("\t",$intRunner+1),'<li><a href="',$strScript,'?id=',$arrMenu[$i][$strIdKey],'">',$arrMenu[$i][$strNameKey],'</a>',$strNL;
                
    parse_into_menu($arrMenu[$i][$strChildKey],$strChildKey,$strNameKey,$strIdKey,$strScript,$intRunner+2);
                echo 
    str_repeat("\t",$intRunner+1),'</li>',$strNL;
            } else {
                echo 
    str_repeat("\t",$intRunner+1),'<li><a href="',$strScript,'?id=',$arrMenu[$i][$strIdKey],'">',$arrMenu[$i][$strNameKey],'</a></li>',$strNL;
            }
            
            if(
    $i==($intMenu-1)) { 
                echo 
    str_repeat("\t",$intRunner),'</ul>',$strNL
            }
        }
        
    }

    parse_into_menu($menucategorias,'children','nom_cat','id_cat',$_SERVER['PHP_SELF']); 
    Your most likely going to need to modify the URL. Currently it just uses PHP_SELF and places an id in the query string. However, I don't believe that is how CodeIgnitor works considering it uses a self integrated clean URL system.

  2. #27
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe I do that. After passing the controller, I can get some data on my view.

    (I will not change nothing on this page now):
    http://www.nuvemk.com/c_categoria/seccao/1

    If we click on Agricultura, or any other category, the menu of subcategories will display perfectly.

    Now, If we click on Fitof&#225;rmacos, you will notice that the sub-subcategories will appear, however, the subcategories have disappear. :s I need them to stay there, when the sub-subcategories are show.


    I'm almost dead, but online. I realise that probably I'm not explaining myself properly. I will be glad to clarify everything about my doubts.

    Update: I've not seen your last post completly on writing time of this post, I will read your view example.


    Thanks a lot,
    M&#225;rcio

  3. #28
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm feeling like an idiot, can you help me to feel even more like one:

    1) This is a function to parse the data that is returned as an hierarchical array as a menu. Hence, despite being somewhat complex function, it's ok to leave it in the view... yes?

    2) Here we count the items on our array:
    PHP Code:
    $intMenu count($arrMenu); 
    All items or only those of the first index? [0]?

    3) What does the intRunner do?

    4) Boolean question: This will just build the link right?
    PHP Code:
    if(!empty($arrMenu[$i][$strChildKey])) {
                echo 
    str_repeat("\t",$intRunner+1),'<li><a href="',$strScript,'?id=',$arrMenu[$i][$strIdKey],'">',$arrMenu[$i][$strNameKey],'</a>',$strNL
    5) This is recursion? All the operations that has been made till here, will be stored and the "main" function will execute again? Or, only the informations that are made inside this recursion will be saved... ?
    PHP Code:
    parse_into_menu($arrMenu[$i][$strChildKey],$strChildKey,$strNameKey,$strIdKey,$strScript,$intRunner+2); 
    6) Why is this intRunner getting +1 and +2? Maybe the question about want intRunner is may answer this.

    7)
    PHP Code:
     if($i==($intMenu-1)) { 
                echo 
    str_repeat("\t",$intRunner),'</ul>',$strNL
            } 
    Why are we taking 1 off our final array count here?



    Thanks a lot for your time,
    Márcio

  4. #29
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    1.) It is display logic so it belongs either in the view or as a helper. Placing it in the view makes things simple. However, you could create a recursive menu helper.

    2.) correct

    3.) $intRunner tracks each depth -1. Except for the first which is 0. So the second level from the top would be 2 (2-1) = 1.

    4.) If the links has children then it echos out the links anchor and calls the same function again incrementing the runner by two and passing the children of the link.

    5.) See 4

    6.) I couldn't get the tabs to behave appropriately without incrementing by 2. Hypothetically it should be one though. So the actual depth is represented by $intRunner - 1 if $intRunner is not equal 0. 0 references the root level.

    7.) A array with 7 items will have a count of 7 but begins at index 0. So indexes 0 – 6 exists. Thus, the last item is represented by $intMenu-1.

  5. #30
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    2.) correct.
    2.1)
    All items, including other arrays inside? Or only those items of the first index? [0]?

    Quote Originally Posted by oddz View Post
    3.) $intRunner tracks each depth -1. Except for the first which is 0. So the second level from the top would be 2 (2-1) = 1.
    3.1) Ok. So $intRunner is a top player here, the fact that we use depth -1 is also to do with the fact that the arrays start index is 0? (this very common when working with arrays I see).

    Quote Originally Posted by oddz View Post
    4.) If the links has children then it echos out the links anchor and calls the same function again incrementing the runner by two and passing the children of the link.
    So, he knows that we have or we don't have childrens by looking on our already build multidimensional array. I was wrongly thought that when we have this:
    PHP Code:
    $arrMenu[$i][$strChildKey
    we were access the $arrMenu ant the index key of $i and the value of $strChildKey.
    4.1)Each time we have something like $array[bla][ble][bli] all those are all keys? Same here:
    PHP Code:
    $menucategorias[0]['children'][0]['nome_cat']; 
    ?

    4.2)
    Since on our hierarchical array we already have a key called children, could we also see if children is empty or not? instead of using strChildKey for that?


    In the meanwhile, now that I more or less understood the code, I will give it a try.


    Again, thanks a lot for your help on this,
    M&#225;rcio

  6. #31
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok... have change the links to accommodate some of CodeIgniter features.

    I'm having this:
    http://www.nuvemk.com/c_categoria/seccao/1

    When we click "Agricultura" we get:
    # Fitofármacos

    * Herbicidas
    * Insecticidas
    * Fungicidas

    It's nice because, now I can see all the links availabe on different deeps levels. However, the main point here is to not show the sublinks right away, those sublinks:
    Herbicidas
    Insecticidas
    Fungicidas
    should appear on the right side (this is css, no relevant here) of Fitofármacos only when Fitofármacos is clicked and not before. However, Fitofármacos, Sementes e Adubos (main subcategories of agricultura category), should always be present.


    Sleep needed. Tomorrow I will try more properly the examples providaded.




    Regards,
    Márcio

  7. #32
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After we click on a parent link I'm still unable to show BOTH the parent list links and the child list links.

    PHP Code:
    function parse_into_menu($arrMenu,$strChildKey,$strNameKey,$strIdKey,$intRunner=0) {
        
        
        
    $strNL "\n";
        
    $intMenu count($arrMenu);
        
        
    //allow the use of uri->segment inside this function:
        
    $CI =& get_instance();
        
        
        for(
    $i=0;$i<$intMenu;$i++) 
        {
            if(
    $i==0
            { 
                echo 
    '<ul>',$strNL
            }
            
            if(!empty(
    $arrMenu[$i][$strChildKey])) 
            {
                echo 
    '<li>'
                
                
    .anchor("c_categoria/seccao/".$arrMenu[$i][$strIdKey],$arrMenu[$i][$strNameKey]),$strNL;
                
                echo 
    '</li>',$strNL;
                 
                
    //try number 35235235253: if the third element of the URI segment is equal to the value of my child key, show my childs:
                
    if($CI->uri->segment(3)==$arrMenu[$i][$strIdKey])
                {

                    echo 
    '<li>';
                    
    parse_into_menu($arrMenu[$i][$strChildKey],$strChildKey,$strNameKey,$strIdKey,$intRunner+1);
                    echo 
    '</li>';
                }
                
            }
            else 
            
            {
                echo 
    '<li>'.anchor("c_categoria/seccao/".$arrMenu[$i][$strIdKey],$arrMenu[$i][$strNameKey]).'</li>',$strNL;
            }
            
            if(
    $i==($intMenu-1)) { 
                echo 
    '</ul>',$strNL
            }
        }
        
    }

    parse_into_menu($menucategorias,'children','nome_cat','id_cat'); 

    I'm still getting either the parent or, if the parent as childs, the childs. But I'd like to display both, parent list links and child list links, when we click on the parent. :s

    Any clue about what am I missing here?

    Note:
    The ci part on this code:
    PHP Code:
    $CI->uri->segment(3
    just grabs the $id that is on the third segment of our uri.

    The print_r of the array return the following:
    Code:
    Array ( 
    	[0] => Array ( 
    		[id_cat] => 6 
    		[nome_cat] => Fitof&#225;rmacos 
    		[parent_id_cat] => 1 
    		[children] => Array (
    			        [0] => Array ( 
    				[id_cat] => 9
    				[nome_cat] => Herbicidas 
    				[parent_id_cat] => 6 
    				[children] => Array ( ) ) 
    	
    				[1] => Array (
    				[id_cat] => 10 
    				[nome_cat] => Insecticidas 
    				[parent_id_cat] => 6 
    				[children] => Array ( ) ) 
    		
    				[2] => Array ( 
    				[id_cat] => 11 
    				[nome_cat] => Fungicidas 
    				[parent_id_cat] => 6 
    				[children] => Array ( ) 
    			)
    		)
     	
    	
    	 )
    
    	 [1] => Array ( 
    		[id_cat] => 7 
    		[nome_cat] => Adubos 
    		[parent_id_cat] => 1 
    		[children] => Array ( )
               	) 
    
    	[2] => Array (
     		[id_cat] => 8 
    		[nome_cat] => Sementes 
    		[parent_id_cat] => 1 
    		[children] => Array ( ) 
    	)
    
    )


    Anything....

  8. #33
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    I believe the below will do it. You were so close – just left out printing the link for a menu with children.

    PHP Code:
    function parse_into_menu($arrMenu,$strChildKey,$strNameKey,$strIdKey,$intRunner=0) {
        
        
    $strNL "\n";
        
    $intMenu count($arrMenu);
        
        
    //allow the use of uri->segment inside this function:
        
    $CI =& get_instance();    
        
        for(
    $i=0;$i<$intMenu;$i++) 
        {
            if(
    $i==0
            { 
                echo 
    '<ul>',$strNL
            }
            
            if(!empty(
    $arrMenu[$i][$strChildKey])) 
            {
                echo 
    '<li>',anchor("c_categoria/seccao/".$arrMenu[$i][$strIdKey],$arrMenu[$i][$strNameKey]),$strNL;
                 
                
    //try number 35235235253: if the third element of the URI segment is equal to the value of my child key, show my childs:
                
    if($CI->uri->segment(3)==$arrMenu[$i][$strIdKey])
                {

                    
    parse_into_menu($arrMenu[$i][$strChildKey],$strChildKey,$strNameKey,$strIdKey,$intRunner+1);
                }

                 echo 
    '</li>';
                
            }
            else 
            
            {
                echo 
    '<li>'.anchor("c_categoria/seccao/".$arrMenu[$i][$strIdKey],$arrMenu[$i][$strNameKey]).'</li>',$strNL;
            }
            
            if(
    $i==($intMenu-1)) { 
                echo 
    '</ul>',$strNL
            }
        }
        
    }

    parse_into_menu($menucategorias,'children','nome_cat','id_cat'); 

  9. #34
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh my... oh my... I hope you are right, I hope you are right...

    Ok... I have seen... line by line and the diference between the code posted earliner and this one post by you is:

    Earlier:
    PHP Code:
    if(!empty($arrMenu[$i][$strChildKey]))         
    {            
      echo 
    '<li>'.anchor("c_categoria/seccao/".$arrMenu[$i][$strIdKey],$arrMenu[$i][$strNameKey]),$strNL;                        
      echo 
    '</li>',$strNL//removed.
        
    if($CI->uri->segment(3)==$arrMenu[$i][$strIdKey])
        {                
          echo 
    '<li>';                
          
    parse_into_menu($arrMenu[$i][$strChildKey],$strChildKey,$strNameKey,$strIdKey,$intRunner+1);               
          echo 
    '</li>';  //removed.          
        

        
    //add </li> here.                   

    You:
    PHP Code:
    if(!empty($arrMenu[$i][$strChildKey]))         
    {            
      echo 
    '<li>',anchor("c_categoria/seccao/".$arrMenu[$i][$strIdKey],$arrMenu[$i][$strNameKey]),$strNL;                                    
      if(
    $CI->uri->segment(3)==$arrMenu[$i][$strIdKey])            
      {                
         
    parse_into_menu($arrMenu[$i][$strChildKey],$strChildKey,$strNameKey,$strIdKey,$intRunner+1);            
      }             
      echo 
    '</li>';                    

    How could those <li> replacements made such a difference?

    If have said:
    just left out printing the link for a menu with children
    But this <li> replacements shouldn't deal with it correct?

    I supose I have to do something like this, in order to add a link on my children: (call parse_into_menu function recursivly inside anchor function?):

    PHP Code:

    anchor
    ("c_categoria/seccao/".parse_into_menu($arrMenu[$i][$strChildKey],$strChildKey,$strNameKey,$strIdKey,$intRunner+1),$strNL
    Please advice...


    Anyway, I will run to the place where my code is, and try to see the effects on those chances...


    M&#225;rcio

  10. #35
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    Nop.

    When we click on one of the parent links (Fitofarmacos).
    I'm still unable to show this parent and the other parents (Fitofarmacos, Adubos, Sementes) and the child elements (herbicidas, fungicidas, insecticidas) side by side on the page. I see one, or see other, never both.

  11. #36
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok... If I go to one of my child categories, I and make a print_r of the array:
    http://www.nuvemk.com/c_categoria/seccao/6

    I notice that on that print_r result, there's no parent categories being generated. So... I presume, no matter what recursion or foreach or whatever I will use, I will never be able to grab the parent elements of the array, since they don't exist.
    So, instead of deal with it on the view, and try to kill myself on the view, maybe the issue may rely on the model?

    When I choose a child of a specific Id, I want to show also, the parents of that specific id.


    Right now, we are getting only the children of a specific id, but not the parents... Correct?:

    PHP Code:
    public function getSubCategorias($id_cat/*,$intDepth=0*/
        {    
            
            
    $strSQL 'SELECT c.id_cat,c.nome_cat,c.parent_id_cat FROM categoria c';
            
    $query $this->db->query($strSQL);
            
            
    $arrQuery $query->result_array();
            
            return 
    $this->_parseResultTree($id_cat,$arrQuery/*,$intDepth*/$intRunner=0);
            
        }


         private function 
    _parseResultTree($intParentId,&$arrRows/*,$intDepth*/,$intRunner
         {
            
            
    // stop at this depth, portanto, só devolve o array, caso a Depth seja igual ao Runner.
            /*if($intDepth == $intRunner) 
            {
                return array();
            }*/
        
            
    $arrChildren = array();

            for(
    $i=0;$i<count($arrRows);$i++) 
            {
                if(
    $intParentId == $arrRows[$i]['parent_id_cat']) 
                {
                    
    $arrChildren array_merge($arrChildren,array_splice($arrRows,$i--,1));
                }
            }
        
            
    $intChildren count($arrChildren);
            if(
    $intChildren != 0
            {
                for(
    $i=0;$i<$intChildren;$i++) 
                {
                    
    $arrChildren[$i]['children'] = $this->_parseResultTree($arrChildren[$i]['id_cat'],$arrRows/*,$intDepth*/,$intRunner++);
                }        
            }
        
            return 
    $arrChildren;
        
        } 
    Does what I say makes any sense? Should I drown myself on the model?

  12. #37
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To do that, I need to understand the model function you have done oddz:

    Can I please have your time on watching my comments, and see if, they are precise? They should be precise, otherwise, miss-comprehension may arrive.

    PHP Code:
    public function getSubCategorias($id_cat
        {    
            
            
    $strSQL 'SELECT c.id_cat,c.nome_cat,c.parent_id_cat FROM categoria c';
            
    $query $this->db->query($strSQL);
            
            
    $arrQuery $query->result_array();
            
            
    //grab the array that is comming from our query, pass the $id_cat that comes from the URI (question: why do we need to grab this $id_cat? Shouldn't the $arrQuery be enough?, )
            //and puts the intRunner to 0. (since I have no more deapth stuff in here, I can safely remove the $intRunner as well no?)
            
    return $this->_parseResultTree($id_cat,$arrQuery$intRunner=0);
            
        }


         private function 
    _parseResultTree($intParentId,&$arrRows,$intRunner
         {
            
           
        
            
    /*
             * Please confirm if my interpretation is ok... :)
             */
            
            //define arrChildren as array.
            
    $arrChildren = array();
            
            
    //for all elements on the array
            
    for($i=0;$i<count($arrRows);$i++) 
            
            {
                
    //if intParentId (that in our case is a specific id comming from the URI segment) is equal to a specific parent_id_cat id of that element... 
                
    if($intParentId == $arrRows[$i]['parent_id_cat']) 
                {
                    
    //arrChild will be filled in. How?
                    //It will merge himself, so it will add to himself is own values,
                    //adding $arrRows on the last retrieved element of $arrChildren?  
                    
    $arrChildren array_merge($arrChildren,array_splice($arrRows,$i--,1));
                }
            }
            
            
    //take the number of children array total elements.
            
    $intChildren count($arrChildren);
            
            
    //if that number is diferent then zero, hence, if any children exists
            
    if($intChildren != 0
            {
                
    //for each of those children...
                
    for($i=0;$i<$intChildren;$i++) 
                {
                    
    //equals que children key to another copy of $arrChildren from a specifc id_cat, with some elements, where runner increases a value... AHHH!!!!!...
                    
    $arrChildren[$i]['children'] = $this->_parseResultTree($arrChildren[$i]['id_cat'],$arrRows,$intRunner++);
                }        
            }
        
            return 
    $arrChildren;
        
        } 

    Thanks a lot,
    Márcio

  13. #38
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe this is the line:

    PHP Code:

    $arrChildren 
    array_merge($arrChildren,array_splice($arrRows,$i--,1)); 
    Is this the line responsible for displaying ONLY the children? If so, this is the line that needs to be changed, and I need to merge to arrChildren (that is returned), also, the parents...

    Any help?

  14. #39
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try number 52352352352

    if I have:

    http://mysite.com/mycontroller/1/

    I will get all children where parent_id is equal to 1.
    Echoing on the page:
    Child1
    Child2
    Child3

    Here is the print_r of that array returned :
    PHP Code:
    Array ( [0] => Array ( [id_cat] => [nome_cat] => Fitofármacos [parent_id_cat] => [children] => Array ( [0] => Array ( [id_cat] => [nome_cat] => Herbicidas [parent_id_cat] => [children] => Array ( ) ) [1] => Array ( [id_cat] => 10 [nome_cat] => Insecticidas [parent_id_cat] => [children] => Array ( ) ) [2] => Array ( [id_cat] => 11 [nome_cat] => Fungicidas [parent_id_cat] => [children] => Array ( ) ) ) ) [1] => Array ( [id_cat] => [nome_cat] => Adubos [parent_id_cat] => [children] => Array ( ) ) [2] => Array ( [id_cat] => [nome_cat] => Sementes [parent_id_cat] => [children] => Array ( ) ) ) 
    If I click on one of those childs, I will get another address, let's say:
    http://mysite.com/mycontroller/6/

    and, if they have childs, I'm getting this array returned:
    PHP Code:
    Array ( [0] => Array ( [id_cat] => [nome_cat] => Herbicidas [parent_id_cat] => [children] => Array ( ) ) [1] => Array ( [id_cat] => 10 [nome_cat] => Insecticidas [parent_id_cat] => [children] => Array ( ) ) [2] => Array ( [id_cat] => 11 [nome_cat] => Fungicidas [parent_id_cat] => [children] => Array ( ) ) ) 

    As you may notice, from the first printed array to the second printed array, I'm losing the parents.

    Instead of having:
    parent>child

    I'm getting only:
    child.


    My point is to not lose the parents.

    The same happens if I click on a childrens childrens.
    Instead of having:
    Parent>child>child's child

    I'm getting only:
    Child's child.


    So my question is:

    how can I keep track of the parents?


    Just a push...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •