Conclusions categories with subcategories
up vote
0
down vote
favorite
How do I put all the parent categories on one page with links to their child categories, the structure is something like this:
On the same page are all parent categories; When you click on a category, its new categories are displayed on a new page.
CategoryController:
public function actionIndex(){
return $this->render('index', [
'model' => $this->outTree($id)
]);
}
public function getAllArray(){
$all = Category::find()->all();
$array = array();
foreach($all as $v){
$array[$v->parentId] = $v;
}
return $array;
}
public function outTree($parentId) {
$array = $this->getAllArray();
if(isset($array[$parentId])){
echo '<div>';
foreach($array[$parentId] as $v){
echo '<div>'.$v->name.'</div>';
echo '<div>'.$v->id.'</div>';
echo '<div>'.$v->parentId.'</div>';
$this->outTree($v->id);
}
echo '</div>';
}
}
protected function findModel($alias)
{
if (($model = Category::findOne(['alias' => $alias, 'active' => 1])) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
Relationship of parent categories with children:
public function getParent()
{
return $this->hasOne(Category::className(), ['id' => 'parentId']);
}
How do I make such a conclusion in the view, now the output goes in the controller? At the moment, all parent categories and their child categories are displayed on the same page. Do I need to register something in UrlManager?
php yii2
add a comment |
up vote
0
down vote
favorite
How do I put all the parent categories on one page with links to their child categories, the structure is something like this:
On the same page are all parent categories; When you click on a category, its new categories are displayed on a new page.
CategoryController:
public function actionIndex(){
return $this->render('index', [
'model' => $this->outTree($id)
]);
}
public function getAllArray(){
$all = Category::find()->all();
$array = array();
foreach($all as $v){
$array[$v->parentId] = $v;
}
return $array;
}
public function outTree($parentId) {
$array = $this->getAllArray();
if(isset($array[$parentId])){
echo '<div>';
foreach($array[$parentId] as $v){
echo '<div>'.$v->name.'</div>';
echo '<div>'.$v->id.'</div>';
echo '<div>'.$v->parentId.'</div>';
$this->outTree($v->id);
}
echo '</div>';
}
}
protected function findModel($alias)
{
if (($model = Category::findOne(['alias' => $alias, 'active' => 1])) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
Relationship of parent categories with children:
public function getParent()
{
return $this->hasOne(Category::className(), ['id' => 'parentId']);
}
How do I make such a conclusion in the view, now the output goes in the controller? At the moment, all parent categories and their child categories are displayed on the same page. Do I need to register something in UrlManager?
php yii2
are you mean you need to display parent category in index page, and if user click on parent you need to go another action and load child, or go to current action with defferant view?!, if any one from that you can determine a parent by any selector/custom href and then go to new action and load child only, or go to current action and load another view render...
– Anees Hikmat Abu Hmiad
Nov 10 at 12:58
Yes, I just need to link to get child categories, how do I do it, can you please describe here?
– sergey - dev
Nov 10 at 13:06
how many levels of category parent you have?, also when you display parent, you display a child in current view, or just a parent and then go to page and display child?
– Anees Hikmat Abu Hmiad
Nov 10 at 13:14
1
only 4 levels of nesting, when showing the parent, the child does not need to show, but then go to the page and show the child
– sergey - dev
Nov 10 at 13:22
well i would suggest you to usenested set model
for easily managing these prooblems
– Muhammad Omer Aslam
Nov 10 at 13:57
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
How do I put all the parent categories on one page with links to their child categories, the structure is something like this:
On the same page are all parent categories; When you click on a category, its new categories are displayed on a new page.
CategoryController:
public function actionIndex(){
return $this->render('index', [
'model' => $this->outTree($id)
]);
}
public function getAllArray(){
$all = Category::find()->all();
$array = array();
foreach($all as $v){
$array[$v->parentId] = $v;
}
return $array;
}
public function outTree($parentId) {
$array = $this->getAllArray();
if(isset($array[$parentId])){
echo '<div>';
foreach($array[$parentId] as $v){
echo '<div>'.$v->name.'</div>';
echo '<div>'.$v->id.'</div>';
echo '<div>'.$v->parentId.'</div>';
$this->outTree($v->id);
}
echo '</div>';
}
}
protected function findModel($alias)
{
if (($model = Category::findOne(['alias' => $alias, 'active' => 1])) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
Relationship of parent categories with children:
public function getParent()
{
return $this->hasOne(Category::className(), ['id' => 'parentId']);
}
How do I make such a conclusion in the view, now the output goes in the controller? At the moment, all parent categories and their child categories are displayed on the same page. Do I need to register something in UrlManager?
php yii2
How do I put all the parent categories on one page with links to their child categories, the structure is something like this:
On the same page are all parent categories; When you click on a category, its new categories are displayed on a new page.
CategoryController:
public function actionIndex(){
return $this->render('index', [
'model' => $this->outTree($id)
]);
}
public function getAllArray(){
$all = Category::find()->all();
$array = array();
foreach($all as $v){
$array[$v->parentId] = $v;
}
return $array;
}
public function outTree($parentId) {
$array = $this->getAllArray();
if(isset($array[$parentId])){
echo '<div>';
foreach($array[$parentId] as $v){
echo '<div>'.$v->name.'</div>';
echo '<div>'.$v->id.'</div>';
echo '<div>'.$v->parentId.'</div>';
$this->outTree($v->id);
}
echo '</div>';
}
}
protected function findModel($alias)
{
if (($model = Category::findOne(['alias' => $alias, 'active' => 1])) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
Relationship of parent categories with children:
public function getParent()
{
return $this->hasOne(Category::className(), ['id' => 'parentId']);
}
How do I make such a conclusion in the view, now the output goes in the controller? At the moment, all parent categories and their child categories are displayed on the same page. Do I need to register something in UrlManager?
php yii2
php yii2
asked Nov 10 at 12:06
sergey - dev
162
162
are you mean you need to display parent category in index page, and if user click on parent you need to go another action and load child, or go to current action with defferant view?!, if any one from that you can determine a parent by any selector/custom href and then go to new action and load child only, or go to current action and load another view render...
– Anees Hikmat Abu Hmiad
Nov 10 at 12:58
Yes, I just need to link to get child categories, how do I do it, can you please describe here?
– sergey - dev
Nov 10 at 13:06
how many levels of category parent you have?, also when you display parent, you display a child in current view, or just a parent and then go to page and display child?
– Anees Hikmat Abu Hmiad
Nov 10 at 13:14
1
only 4 levels of nesting, when showing the parent, the child does not need to show, but then go to the page and show the child
– sergey - dev
Nov 10 at 13:22
well i would suggest you to usenested set model
for easily managing these prooblems
– Muhammad Omer Aslam
Nov 10 at 13:57
add a comment |
are you mean you need to display parent category in index page, and if user click on parent you need to go another action and load child, or go to current action with defferant view?!, if any one from that you can determine a parent by any selector/custom href and then go to new action and load child only, or go to current action and load another view render...
– Anees Hikmat Abu Hmiad
Nov 10 at 12:58
Yes, I just need to link to get child categories, how do I do it, can you please describe here?
– sergey - dev
Nov 10 at 13:06
how many levels of category parent you have?, also when you display parent, you display a child in current view, or just a parent and then go to page and display child?
– Anees Hikmat Abu Hmiad
Nov 10 at 13:14
1
only 4 levels of nesting, when showing the parent, the child does not need to show, but then go to the page and show the child
– sergey - dev
Nov 10 at 13:22
well i would suggest you to usenested set model
for easily managing these prooblems
– Muhammad Omer Aslam
Nov 10 at 13:57
are you mean you need to display parent category in index page, and if user click on parent you need to go another action and load child, or go to current action with defferant view?!, if any one from that you can determine a parent by any selector/custom href and then go to new action and load child only, or go to current action and load another view render...
– Anees Hikmat Abu Hmiad
Nov 10 at 12:58
are you mean you need to display parent category in index page, and if user click on parent you need to go another action and load child, or go to current action with defferant view?!, if any one from that you can determine a parent by any selector/custom href and then go to new action and load child only, or go to current action and load another view render...
– Anees Hikmat Abu Hmiad
Nov 10 at 12:58
Yes, I just need to link to get child categories, how do I do it, can you please describe here?
– sergey - dev
Nov 10 at 13:06
Yes, I just need to link to get child categories, how do I do it, can you please describe here?
– sergey - dev
Nov 10 at 13:06
how many levels of category parent you have?, also when you display parent, you display a child in current view, or just a parent and then go to page and display child?
– Anees Hikmat Abu Hmiad
Nov 10 at 13:14
how many levels of category parent you have?, also when you display parent, you display a child in current view, or just a parent and then go to page and display child?
– Anees Hikmat Abu Hmiad
Nov 10 at 13:14
1
1
only 4 levels of nesting, when showing the parent, the child does not need to show, but then go to the page and show the child
– sergey - dev
Nov 10 at 13:22
only 4 levels of nesting, when showing the parent, the child does not need to show, but then go to the page and show the child
– sergey - dev
Nov 10 at 13:22
well i would suggest you to use
nested set model
for easily managing these prooblems– Muhammad Omer Aslam
Nov 10 at 13:57
well i would suggest you to use
nested set model
for easily managing these prooblems– Muhammad Omer Aslam
Nov 10 at 13:57
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You can do it by this:
Your Controller:
public function actionIndex($parentId = null){
$models = Category::find()->andWhere(['parent_id' => $parentId])->all();
return $this->render('index', [
'models' => $models
]);
}
public function actionGetChilds($parentId){
$models = Children::find()->andWhere(['parent_id' => $parentId])->all();
return $this->render('childs', [
'models' => $models
]);
}
You View Page(index):
foreach($models as $model):
echo Html::a($model->name, Url::to(['index', 'parentId' => $model->parent_id]));
endforeach;
Your View Page(child):
foreach($models as $model):
echo Html::a($model->name, Url::to(['get-childs', 'parentId' => $model->parent_id]));
endforeach;
Note: I think you another relation with name GetChildern in Category
Model, you can use it in index action to get all child for category by
this$model->childern
thank you, now hatched all parent categories, and to make me to lead on to their child?
– sergey - dev
Nov 10 at 13:42
welcome mate, their child exsits in Category table or another table? or its just last level from category?...
– Anees Hikmat Abu Hmiad
Nov 10 at 13:44
the child is included in the same table of categories, I need to link to display all child categories
– sergey - dev
Nov 10 at 13:47
how you can determine record is a child?
– Anees Hikmat Abu Hmiad
Nov 10 at 13:48
there is a link ' id '= > 'parentId', that is, by clicking on the parent category on the new page displays all child categories whose parentId matches the ID of the parent category
– sergey - dev
Nov 10 at 13:50
|
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can do it by this:
Your Controller:
public function actionIndex($parentId = null){
$models = Category::find()->andWhere(['parent_id' => $parentId])->all();
return $this->render('index', [
'models' => $models
]);
}
public function actionGetChilds($parentId){
$models = Children::find()->andWhere(['parent_id' => $parentId])->all();
return $this->render('childs', [
'models' => $models
]);
}
You View Page(index):
foreach($models as $model):
echo Html::a($model->name, Url::to(['index', 'parentId' => $model->parent_id]));
endforeach;
Your View Page(child):
foreach($models as $model):
echo Html::a($model->name, Url::to(['get-childs', 'parentId' => $model->parent_id]));
endforeach;
Note: I think you another relation with name GetChildern in Category
Model, you can use it in index action to get all child for category by
this$model->childern
thank you, now hatched all parent categories, and to make me to lead on to their child?
– sergey - dev
Nov 10 at 13:42
welcome mate, their child exsits in Category table or another table? or its just last level from category?...
– Anees Hikmat Abu Hmiad
Nov 10 at 13:44
the child is included in the same table of categories, I need to link to display all child categories
– sergey - dev
Nov 10 at 13:47
how you can determine record is a child?
– Anees Hikmat Abu Hmiad
Nov 10 at 13:48
there is a link ' id '= > 'parentId', that is, by clicking on the parent category on the new page displays all child categories whose parentId matches the ID of the parent category
– sergey - dev
Nov 10 at 13:50
|
show 2 more comments
up vote
0
down vote
You can do it by this:
Your Controller:
public function actionIndex($parentId = null){
$models = Category::find()->andWhere(['parent_id' => $parentId])->all();
return $this->render('index', [
'models' => $models
]);
}
public function actionGetChilds($parentId){
$models = Children::find()->andWhere(['parent_id' => $parentId])->all();
return $this->render('childs', [
'models' => $models
]);
}
You View Page(index):
foreach($models as $model):
echo Html::a($model->name, Url::to(['index', 'parentId' => $model->parent_id]));
endforeach;
Your View Page(child):
foreach($models as $model):
echo Html::a($model->name, Url::to(['get-childs', 'parentId' => $model->parent_id]));
endforeach;
Note: I think you another relation with name GetChildern in Category
Model, you can use it in index action to get all child for category by
this$model->childern
thank you, now hatched all parent categories, and to make me to lead on to their child?
– sergey - dev
Nov 10 at 13:42
welcome mate, their child exsits in Category table or another table? or its just last level from category?...
– Anees Hikmat Abu Hmiad
Nov 10 at 13:44
the child is included in the same table of categories, I need to link to display all child categories
– sergey - dev
Nov 10 at 13:47
how you can determine record is a child?
– Anees Hikmat Abu Hmiad
Nov 10 at 13:48
there is a link ' id '= > 'parentId', that is, by clicking on the parent category on the new page displays all child categories whose parentId matches the ID of the parent category
– sergey - dev
Nov 10 at 13:50
|
show 2 more comments
up vote
0
down vote
up vote
0
down vote
You can do it by this:
Your Controller:
public function actionIndex($parentId = null){
$models = Category::find()->andWhere(['parent_id' => $parentId])->all();
return $this->render('index', [
'models' => $models
]);
}
public function actionGetChilds($parentId){
$models = Children::find()->andWhere(['parent_id' => $parentId])->all();
return $this->render('childs', [
'models' => $models
]);
}
You View Page(index):
foreach($models as $model):
echo Html::a($model->name, Url::to(['index', 'parentId' => $model->parent_id]));
endforeach;
Your View Page(child):
foreach($models as $model):
echo Html::a($model->name, Url::to(['get-childs', 'parentId' => $model->parent_id]));
endforeach;
Note: I think you another relation with name GetChildern in Category
Model, you can use it in index action to get all child for category by
this$model->childern
You can do it by this:
Your Controller:
public function actionIndex($parentId = null){
$models = Category::find()->andWhere(['parent_id' => $parentId])->all();
return $this->render('index', [
'models' => $models
]);
}
public function actionGetChilds($parentId){
$models = Children::find()->andWhere(['parent_id' => $parentId])->all();
return $this->render('childs', [
'models' => $models
]);
}
You View Page(index):
foreach($models as $model):
echo Html::a($model->name, Url::to(['index', 'parentId' => $model->parent_id]));
endforeach;
Your View Page(child):
foreach($models as $model):
echo Html::a($model->name, Url::to(['get-childs', 'parentId' => $model->parent_id]));
endforeach;
Note: I think you another relation with name GetChildern in Category
Model, you can use it in index action to get all child for category by
this$model->childern
edited Nov 10 at 14:04
answered Nov 10 at 13:36
Anees Hikmat Abu Hmiad
1,155824
1,155824
thank you, now hatched all parent categories, and to make me to lead on to their child?
– sergey - dev
Nov 10 at 13:42
welcome mate, their child exsits in Category table or another table? or its just last level from category?...
– Anees Hikmat Abu Hmiad
Nov 10 at 13:44
the child is included in the same table of categories, I need to link to display all child categories
– sergey - dev
Nov 10 at 13:47
how you can determine record is a child?
– Anees Hikmat Abu Hmiad
Nov 10 at 13:48
there is a link ' id '= > 'parentId', that is, by clicking on the parent category on the new page displays all child categories whose parentId matches the ID of the parent category
– sergey - dev
Nov 10 at 13:50
|
show 2 more comments
thank you, now hatched all parent categories, and to make me to lead on to their child?
– sergey - dev
Nov 10 at 13:42
welcome mate, their child exsits in Category table or another table? or its just last level from category?...
– Anees Hikmat Abu Hmiad
Nov 10 at 13:44
the child is included in the same table of categories, I need to link to display all child categories
– sergey - dev
Nov 10 at 13:47
how you can determine record is a child?
– Anees Hikmat Abu Hmiad
Nov 10 at 13:48
there is a link ' id '= > 'parentId', that is, by clicking on the parent category on the new page displays all child categories whose parentId matches the ID of the parent category
– sergey - dev
Nov 10 at 13:50
thank you, now hatched all parent categories, and to make me to lead on to their child?
– sergey - dev
Nov 10 at 13:42
thank you, now hatched all parent categories, and to make me to lead on to their child?
– sergey - dev
Nov 10 at 13:42
welcome mate, their child exsits in Category table or another table? or its just last level from category?...
– Anees Hikmat Abu Hmiad
Nov 10 at 13:44
welcome mate, their child exsits in Category table or another table? or its just last level from category?...
– Anees Hikmat Abu Hmiad
Nov 10 at 13:44
the child is included in the same table of categories, I need to link to display all child categories
– sergey - dev
Nov 10 at 13:47
the child is included in the same table of categories, I need to link to display all child categories
– sergey - dev
Nov 10 at 13:47
how you can determine record is a child?
– Anees Hikmat Abu Hmiad
Nov 10 at 13:48
how you can determine record is a child?
– Anees Hikmat Abu Hmiad
Nov 10 at 13:48
there is a link ' id '= > 'parentId', that is, by clicking on the parent category on the new page displays all child categories whose parentId matches the ID of the parent category
– sergey - dev
Nov 10 at 13:50
there is a link ' id '= > 'parentId', that is, by clicking on the parent category on the new page displays all child categories whose parentId matches the ID of the parent category
– sergey - dev
Nov 10 at 13:50
|
show 2 more comments
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238773%2fconclusions-categories-with-subcategories%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
are you mean you need to display parent category in index page, and if user click on parent you need to go another action and load child, or go to current action with defferant view?!, if any one from that you can determine a parent by any selector/custom href and then go to new action and load child only, or go to current action and load another view render...
– Anees Hikmat Abu Hmiad
Nov 10 at 12:58
Yes, I just need to link to get child categories, how do I do it, can you please describe here?
– sergey - dev
Nov 10 at 13:06
how many levels of category parent you have?, also when you display parent, you display a child in current view, or just a parent and then go to page and display child?
– Anees Hikmat Abu Hmiad
Nov 10 at 13:14
1
only 4 levels of nesting, when showing the parent, the child does not need to show, but then go to the page and show the child
– sergey - dev
Nov 10 at 13:22
well i would suggest you to use
nested set model
for easily managing these prooblems– Muhammad Omer Aslam
Nov 10 at 13:57