Yii Framework Forum: List View Grouping - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

List View Grouping Rate Topic: -----

#1 User is offline   yort 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 17-June 11

Posted 08 August 2011 - 10:22 AM

How can I group restaurant menu items by their category? So I end up with: (see set up below)

Category Title 1
  • Menu Item Title 1
  • Menu Item Title 2
  • Menu Item Title 3

Category Title 2
  • Menu Item Title 4
  • Menu Item Title 5
  • Menu Item Title 6

ie-
Breakfast
  • Fried Eggs
  • Pancakes
  • Waffles

Lunch
  • Hamburger
  • Sandwich
  • Soup


I have two tables: menu_item (id, title, menu_category_id) and menu_category (id, title)

Each menu item is assigned a menu category

I have a relationship in my Menu model called "category" that links Menu and MenuCategory

$dataProvider=new CActiveDataProvider('Menu', array(
    'criteria'=>array(
        'with'=>array('category'),
    ),
));


Everything works fine but the default CListView displays like this:

Category Title 1
  • Menu Item Title 1

Category Title 1
  • Menu Item Title 2

Category Title 1
  • Menu Item Title 3

Category Title 2
  • Menu Item Title 4

Category Title 2
  • Menu Item Title 5

Category Title 2
  • Menu Item Title 6

0

#2 User is offline   Mohsin Shoaib 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 25-April 11
  • Location:Pakistan

Posted 08 August 2011 - 12:04 PM

I also have same problem and I solved it in this way. Don't know if it is an appropriate way but it works.

<?php
/* get all categories */
$categories = Category::model()->findAll();
foreach ($categories as $category) 
{
    echo '<div class="title-style">';
    echo $category->name;
    echo '</div>';
    
    $this->widget('zii.widgets.CDetailView', array(
        'data' => $model->findByAttributes(array('category_id' => $category->id)),
        'attributes' => array(
            'menu_name',
        ),
    ));
}
?>

0

#3 User is offline   yort 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 17-June 11

Posted 08 August 2011 - 03:19 PM

View PostMohsin Shoaib, on 08 August 2011 - 12:04 PM, said:

I also have same problem and I solved it in this way. Don't know if it is an appropriate way but it works.


Thanks for the example Mohsin. I ended up doing something very similar but... but it does seem like there should be a better way.

This is what I did:
public function actionIndex()
{
    $criteria=new CDbCriteria;
    $criteria->select='DISTINCT t.title';
    $criteria->condition='t.active=1';
    $criteria->order='menu_position ASC';
    $criteria->join='INNER JOIN menu ON menu.menu_category_id=t.id';

    $category=new CActiveDataProvider('MenuCategory', array(
        'criteria'=>$criteria,
    ));

    $criteria=new CDbCriteria;
    $criteria->condition='t.active=1';
    $criteria->order='category.menu_position ASC, category_position ASC';
    $criteria->with=array('category');

    $menu=new CActiveDataProvider('Menu', array(
        'criteria'=>$criteria,
     ));

    $this->render('index',array(
        'dataProvider'=>$menu,
        'categories'=>$category,
    ));
}

First I made a data provider with unique categories that have also been used in the menu table. Then made a data provider with my menu and their respective category.

Then in my view I looped like you.
<?php foreach ($categories->data as $category) { ?>
    <h2><?php echo CHtml::link(CHtml::encode($category->title),'',array('name'=>strtolower(CHtml::encode($category->title)))); ?></h2>

    <?php foreach ($dataProvider->data as $item) { ?>
        <?php if ($item->category->title == $category->title) { ?>
            <div class="view menuitem">
                <p class="price">
                    $<?php echo CHtml::encode($item->price); ?>
                </p>
                <h4><?php echo CHtml::link(CHtml::encode($item->title),Yii::app()->createUrl('menu/'.$item->id),array('name'=>strtolower(CHtml::encode($item->title)))); ?></h4>
                <p><?php echo $item->description; ?></p>
            </div>
        <?php } ?>
    <?php } ?>
<?php } ?>

1

#4 User is offline   Say_Ten 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 109
  • Joined: 17-September 10

Posted 09 August 2011 - 08:23 AM

View PostMohsin Shoaib, on 08 August 2011 - 12:04 PM, said:

I also have same problem and I solved it in this way. Don't know if it is an appropriate way but it works...


Nothing wrong with this solution. Only thing I'd add is a with(), and maybe a together(), in with the find. That way it only performs one SQL query rather than one per category. As this can avoid some query overhead and be quicker, more noticeable if you connect over TCP though.
0

#5 User is offline   Hung Xuan Hoang 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 14-January 13

Posted 20 January 2013 - 02:48 AM

I am doing something similar to this problem. I have a table Comic in which I stores: ComicID (PK) and Comic_Title. And I want to display all available comics grouped by the first letter of comic's title like this:

A.

Air Craft [specific space] Alibaba [specific space] Amma

B.

Boys [specific space] Boys and Girls [specific space] BBB

Can anyone suggest me a way to display comics like that?
0

#6 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 20 January 2013 - 04:54 AM

Dear Friend

If you have model Book and a field name as title, then we can do the following.

$books=Book::model()->findAll(array(
'order'=>"title ASC"
));


Then the following code will serve the purpose

$alphabet="";


foreach($books as $book)
{
    $bookFirstLetter=strtoupper(substr($book->title,0,1));
    if($bookFirstLetter!==$alphabet)
    {   
         echo "</br>".$bookFirstLetter."</br>";
         $alphabet=$bookFirstLetter;
    }

    echo $book->title." </t> ";
}


The code is very nascent; apply your own aesthetics.

Regards.
0

#7 User is offline   Hung Xuan Hoang 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 14-January 13

Posted 21 January 2013 - 10:55 AM

Ok thanks :). I got the idea. I am working on CSS to display my comics as my expected way.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users