问题
I'm using Yii nested set behavior, which helps me to keep my categories nested as seen here (nevermind title rows, they are in russian):
And all I want to do is to have Bootstrap nested menu, which should be like this:
$criteria = new CDbCriteria;
$criteria->order = 'root, lft';
$categories = Category::model()->findAll($criteria);
foreach($categories as $i => $category) {
$items[$i]['label'] = $category->title;
$items[$i]['url'] = $category->url;
$items[$i]['active'] = false;
$items[$i]['items'] = array(
array('label'=>'123', 'url'=>'#'),
array('label'=>'123', 'url'=>'#'),
array('label'=>'123', 'url'=>'#', 'items'=>array(
array('label'=>'1234', 'url'=>'#'),
array('label'=>'1234', 'url'=>'#'),
array('label'=>'1234', 'url'=>'#', 'items'=>array(
array('label'=>'1234', 'url'=>'#'),
array('label'=>'1234', 'url'=>'#'),
array('label'=>'1234', 'url'=>'#'),
)),
)),
);
}
$this->widget('bootstrap.widgets.TbMenu', array(
'type'=>'pills',
'stacked'=>false, // whether this is a stacked menu
'items'=>$items
));
I don't understand how to get this done, btw I read this topic and just don't know how actually apply this function to my problem. Appreciate any help.
回答1:
This is the function that I use to format as json object, you can modify it to generate a php array.
protected function formatJstree(){
$categories = $this->descendants()->findAll();
$level=0;
$parent = 0;
$data = array();
foreach( $categories as $n => $category )
{
$node = array(
'data'=> "{$category->title}",
'attr'=>array('id'=>"category_id_{$category->category_id}")
);
if($category->level == $level){
$data[$parent]["children"][] = $node;
}
else if($level != 0 && $category->level > $level){
if(!isset($data[$n]["children"])){
$data[$n]["children"] = array();
}
$data[$parent]["children"][] = $node;
}
else
{
$data[] = $node;
$parent = $n;
}
$level=$category->level;
}
return $data;
}
回答2:
Finally, my own recursive solution (works with multiple roots):
public function getTreeRecursive() {
$criteria = new CDbCriteria;
$criteria->order = 'root, lft';
$criteria->condition = 'level = 1';
$categories = Category::model()->findAll($criteria);
foreach($categories as $n => $category) {
$category_r = array(
'label'=>$category->title,
'url'=>'#',
'level'=>$category->level,
);
$this->category_tree[$n] = $category_r;
$children = $category->children()->findAll();
if($children)
$this->category_tree[$n]['items'] = $this->getChildren($children);
}
return $this->category_tree;
}
private function getChildren($children) {
$result = array();
foreach($children as $i => $child) {
$category_r = array(
'label'=>$child->title,
'url'=>'#',
);
$result[$i] = $category_r;
$new_children = $child->children()->findAll();
if($new_children) {
$result[$i]['items'] = $this->getChildren($new_children);
}
}
return $result_items = $result;
}
回答3:
Creating multi-level category system with PHP & Yii (MVC Framework) in not just a simple undertaking. 1. Create Function in models
`
function getRootCategory($cur_cat='') {
$sql='select id, course_name, parent_id from course where parent_id="0" and status=0';
$command=Yii::app()->db->createCommand($sql);
$return =$command->queryAll();
foreach($return as $rootCat){
if ($rootCat['id']==$cur_cat){
$test= 'selected=selected';
}else{
$test='';
}
$id=$rootCat['id'];
echo "".$rootCat['course_name'].'';
$this->sub_cat($rootCat['id'] , '', $cur_cat );
}
}
function sub_cat($parentID=0, $space='',$cur_cat ) {
$sql="select id, course_name, parent_id from course where parent_id='$parentID' and status=0";
$command=Yii::app()->db->createCommand($sql);
$return =$command->queryAll();
$count=count($return);
if($parentID==0){ $space=''; }else{ $space .=" - "; }
if($count > 0){
foreach($return as $subcat){
if ($subcat['id']==$cur_cat){$test='selected=selected';}else{$test='';}
$ids=$subcat['id'];
echo "".$space.$subcat['course_name'].'';
$this->sub_cat($subcat['id'],$space, $cur_cat );
}
}
}
` Now create this code in view/file.php
<?php
echo ‘<select id=”parent_id” class=”select” name=”Course[parent_id]” >’;
echo “<option value=’0′ >–Select Exam–</option>”;
echo Course::model()->getRootCategory($model->parent_id);
// ($model->parent_id) means selected text box
echo ‘</select>’; ?>
More Details click on this url Visit http://it-expert.in/create-multi-level-category-using-recursive-function-in-yii/
来源:https://stackoverflow.com/questions/13113727/yii-nested-set-to-dropdown-menu