Nested Sub Categories

hi all,

ive this code that print all the categories with parent_id= 0

and now i want to get nested the relative sub categories (id != 0)

how can i get that ?

i saw this but seems that the extension its not avaible to download now

Indent For Nested Checkboxes


<?php


echo CHtml::activeCheckBoxList(

    $model,

    'categoryIds',

    Chtml::listData(Category::model()->findAll('parent_id = 0'),'id','nome')

    );

	

	?>

thank you all for the help !! :) :)

do you mean:

  • fetch all the parents childs tree ?

  • fetch childs from a given root node ?

  • fetch chils node from a given node ?

You choose to organise your category tree with a parent_id field in your database so maybe this wiki article can help you

i mean all the childs of every single node…

thank you for the article…im going to read it…

you could write a function in Category

public function getChilds() {

return Category::model()->findAll(‘parent_id = :id’,array(’:id’=> $this->id))

}

and for each parent with Category::model()->findAll(‘parent_id = 0’) run the above function to get the their childs

for examble $parents=Category::model()->findAll(‘parent_id = 0’)

foreach ($parents as $parent) {

$parent->getChilds() //now you have and related childs

}

thank you konapaz!

so i add this function to my Category model


 	public function getChilds() {

	return Category::model()->findAll('parent_id = :id',array(':id'=> $this->id)) ;

	}

can you help me how to put the foreach loop in my form ?

this its my code:


<?php


echo CHtml::activeCheckBoxList(

    $model,

    'categoryIds',

    Chtml::listData(Category::model()->findAll('parent_id = 0'),'id','nome')

   

   

    );

	

	

	?>

thank you again :)

I think you should use CTreeView

http://www.yiiframework.com/doc/api/1.1/CTreeView

http://www.yiiframework.com/wiki/215/adding-class-and-links-to-ctreeview-node/

foreach ($parents as $parent) {

$childs=$parent->getChilds();

//–add code for parent item in CTreeView in root

foreach ($childs as $child) {

//–add code for child item in CTreeView in parent item

}

}

Read the wiki how achieve that :)

hi all,

im still tryng to resolve this issue…

in the form viem ive this code:




echo CHtml::activeCheckBoxList(

    $model,

    'categoryIds',

   $parents = Chtml::listData(Category::model()->findAll('parent_id = 0'),'id','nome')   );


foreach ($parents as $parent) {


 Category::model()->getChilds() ;

}

   

in the Category model ive:


public function getChilds() {

return Category::model()->findAll('parent_id = :id',array(':id'=> $this->id));

}

but it doesnt work!! where im wrong ?

thank you all!! :)

You have incorrect code: Category::model()->getChilds() ;

foreach ($parents as $parent) {

//Category::model()->getChilds() ; /this not get child of the parent BUT

$child= $parent->getChilds() ; //to get the childs of Parent :)

}

thank you a lot for the reply but it still does not work…it doesnt print anything…

i can figure out where its the mistake…

i now have this code:

CATEGORY model


	public function getChilds() {

		

return Category::model()->findAll('parent_id = :id',array(':id'=> $this->id));


  }

_form.php:




echo CHtml::activeCheckBoxList(

    $model,

    'categoryIds',

 $parents = Chtml::listData(Category::model()->findAll('parent_id = 0'),'id','nome')   );


foreach ($parents as $parent) {

//$child= $parent->getChilds() ;//with this i get an error..so ive changed in

 $child = Category::model()->getChilds() ;

}



check your variables with var_dump.

Also check if you have categories with valid parent_id (check your database)

thanks konapaz…yes i put:


var_dump($this->id);

inside getChilds function

and it is NULL…I cannot understand why…

database categories are OK

:)

//this is wrong! $parents = Chtml::listData(Category::model()->findAll(‘parent_id = 0’),‘id’,‘nome’) );

$parents = Category::model()->findAll(‘parent_id = 0’); //correct :)

foreach ($parents as $parent) {

var_dump ($parent); //check that :)

$child= $parent->getChilds() //is correct if above is correct

the $child = Category::model()->getChilds(); //is wrong because not loaded any ‘parent’

}


echo CHtml::activeCheckBoxList(

    $model,

    'categoryIds',

   //$parents = Chtml::listData(Category::model()->findAll('parent_id = 0'),'id','nome')   );

 $parents = Category::model()->findAll('parent_id = 0')); 

 

  foreach ($parents as $parent) {

var_dump ($parent); //check that <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/smile.gif' class='bbc_emoticon' alt=':)' />

$child= $parent->getChilds(); //is correct if above is correct

//the $child = Category::model()->getChilds(); //is wrong because not loaded any 'parent'

} 



thank you for your help…but i got this error now…


Object of class Category could not be converted to string 


public static function tag($tag,$htmlOptions=array(),$content=false,$closeTag=true)

139     {

140         $html='<' . $tag . self::renderAttributes($htmlOptions);

141         if($content===false)

142             return $closeTag ? $html.' />' : $html.'>';

143         else

144             return $closeTag ? $html.'>'.$content.'</'.$tag.'>' : $html.'>'.$content;

145     }

so something in this code its worng :huh: :huh:

I see that if i delete





echo CHtml::activeCheckBoxList(

    $model,

    'categoryIds',






before


 $parents = Category::model()->findAll('parent_id = 0'); 

it works…thank you very much konapaz…im going to see if all its ok… :)

oflodor,

Try to run only the below code without CHtml::activeCheckBoxList to ensure getting of correct data

$parents = Category::model()->findAll(‘parent_id = 0’));

foreach ($parents as $parent) {

$child = $parent->getChilds();

var_dump($child);

}

And if its ok the next step it will be to include in some widget view (will see)

yes it works…and now you advise to create a widget ? :blink:

mmm im trying…if you have some suggestion… :D

thanks a lot!!

Welcome :)

The widgets reside in folder structure

protected/components/yourWidget.php

protected/components/view/yourWidgetView.php (name of file is not restricted)

yourWidget.php sould has the below code

class yourWidget extends CWidget {

public function init(){…initialize code here…}

public function run() {

…your render code here…like… $this>render(‘yourWidgetView’); …}

}

and in your view of controller/action you have to put this code

$this->widget(‘mywidget’);

:)

for more details look a related post

http://www.yiiframework.com/forum/index.php/topic/37154-how-to-render-a-module/page__p__179184__fromsearch__1

or wiki

http://www.yiiframework.com/wiki/23/how-to-create-a-breadcrumb-widget/

thank you a lot konapaz!! ill let you know about… :) :) :)

Hi oflodor

you should "start a new topic" for your widget issues because is different by "Nested Sub Categories"

Thank you :)

you are right!! thanks…