0
votes

Firstly, sorry for my English.

I would like to create a select input in CakePHP, who will have grouped the data.

Let's suppose I have some news in categories, I want to display it in the select input, that they are grouped by category, something like this:


== first category == (not clickable)

1 news of the first category

2 news of the first category

3 news of the first category

== Second category == (not clickable)

1 news of the second category

2 news of the second category

3 news of the second category

== third category == (not clickable)

[...]

etc.


Controller:

$this->set('news', $this->News->find('all',
array(
    'CONTAIN' => array('News' => array('Category'))
    'fields' => array('News.name', 'Category.name')
)));

View:

echo $this->form->input('news', array('options' => $news));

I have no idea how to create so grouped in a list. I tried using a parameter group, but nothing came. At the moment it seems to me that would be some way to modify the $news as $options before displaying it in a view, but probably it is not the best solution.

I wonder whether there is possible to create appropriately grouped array in $news in the controller, or remain modification array before being displayed in the view.

2

2 Answers

1
votes

Have you define relation between Category and news in model?. if you have define relation than you will get data like

[0] => Array ( [Category] => Array ( [id] => 1 [Category_name] => Frist Category

            )

        [News] => Array
            (
                [0] => Array
                    (
                        [id] => 1
                        [Category_id] => 1
                        [news] => first news

                     )
                [1] => Array
                    (
                        [id] => 2
                        [Category_id] => 1
                        [news] => second news

                     )
            )
 )

So you can loop easily.

0
votes

Thanks for your answer!

It turned out that I missed the showParents parameter in the view:

echo $this->Form->input('news', array('options'=>$news, 'showParents'=>true));

It helped me to group my list and now I'm on good way.