Conclusions categories with subcategories











up vote
0
down vote

favorite
1












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?










share|improve this question






















  • 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















up vote
0
down vote

favorite
1












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?










share|improve this question






















  • 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













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 use nested 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










  • 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
















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












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







share|improve this answer























  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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
































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







share|improve this answer























  • 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















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







share|improve this answer























  • 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













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







share|improve this answer














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








share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































Popular posts from this blog

Full-time equivalent

Bicuculline

さくらももこ