Question

I'm using CActiveDataProvider to populate a webpage showing messages between users. I have a view php file that uses 'zii.widgets.CListView' in conjunction with the CActiveDataProvider.

I'm using a partial _item.php file to render each individual message. The thing is currently each message renders with solid line above each message, per specified by the _item.php file.

<hr style="border-bottom:solid 1px #efefef; border-top:solid 0px #fff;" />

I want to show this line only when the message being displayed previously is from a different user. I reason that to do this I need to be able to get information from the dataprovider about the previous item (or alternatively, the following item). How do I accomplish this?

What it looks like:


user 1: foobar blah blah


user 2: asdlkfj;ajd


user 2: aljs;dfjlkjk

What I want it to look like:


user 1: foobar blah blah


user 2: asdlkfj;ajd

user 2: aljs;dfjlkjk

This is what my controller looks like:

$dataProvider = new CActiveDataProvider('MailboxMessage', array(
                    'criteria' => array(
                    'condition' => 'conversation_id=:cid',
            'params' => array(
            ':cid' => $_GET['id']
            ),
                ),
        'sort' => array(
        'defaultOrder' => 'created DESC' // this is it.
         ),
                'pagination' => array('pageSize' =>20),
               ));  
Was it helpful?

Solution

I assume your message history that have order like this

user1: Hi Pete!
--------------------------------
user2: Hi Michael!
user2: Do you think about our plan yet?
--------------------------------
user1: Yes, I do.

Those records in the Message table look like

-----------------------------------------------------------
msg_id  | msg                                | user_id (FK) 
-----------------------------------------------------------    
12004     Hi Pete!                             1
12005     Hi Michael!                          2
12006     Do you think about our plan yet?     2
12007     Yes, I do.                           1

Add one property $show_line into your Message model and don't forget to make it as safe attribute

$list_msg = Message:model->findAll(); // could be changed by your way to fetch all of messages & sort them by order of message

if(count($list_msg)>2){
for($i=0; $i<count($list_msg);$i++){
    if($i < count($list_msg)-1){
      //check if owner of current message item is owner of next message also
      $list_msg[$i]->show_line = $list_msg[$i]->user_id == $list_msg[$i+1]->user_id; // user_id in my case is FK on Message table. I am not sure what it was in your db but you can customize it to appropriately
    }
  }
}

//$dataProvider =  new CArrayDataProvider('Message');
//$dataProvider->setData($list_msg);

$dataProvider=new CArrayDataProvider($list_msg, array(
    'id'=>'msg',
    'sort'=>array(
        .....
    ),
    'pagination'=>array(
        'pageSize'=>20,
    ),
));

Set that DataProvider into your list view Then in your item view, you shall capture the boolean show_line to display or hide the hr line

<?php if($data->show_line) {?> <hr .../> <?php } ?>

Above is one way how to make it work, it couldn't match your code exactly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top