Jump to content
Larry Ullman's Book Forums

Coding A Message Inbox


Recommended Posts

I started to code my message inbox, i have an inbox table and i first tried generating the links for the inbox when rendering it but this seems like a huge process. So the other idea would be to create the links and save them to the inbox database table with the html code. This process would seem to be less exhaustive. So do you think saving links in the database with html code is okay?

Link to comment
Share on other sites

Here is the array i will use to render the inbox in the views, this is getting ugly, which is why i think saving html data in the database may be more appropriate. Please excuse my poor Yii coding, i am just hacking away here.


public function getInboxMessages($id)
		// Make an array for inbox messages
		$message = array();
		$criteria = new CDbCriteria();
		$criteria->condition = 'user_id=:user_id';
		$criteria->params = array(':user_id'=>$id);

		$modelInbox = $this->model()->findAll($criteria);
			throw new CHttpException(404,'The requested page does not exist.');		
			foreach($modelInbox as $message) 
					$modelSender = User::model()->findByPk($message->sender_id);
					$sender_username = $modelSender->username;
					$from = CHtml::link($sender_username, Yii::app()->createUrl('$sender_username'));
					$icon = '<i class="icon-hand-right"></i>';
					$modelOffer = Offer::model()->with('action')->findByPk($message->type_id);
					$modelAction = $modelOffer->action;
					$thread_id = $modelAction->thread_id;
					$modelThread = Thread::model()->findByPk($thread_id);
					$item_id = $modelThread->item_id;
					$modelItem = Item::model()->findByPk($item_id);
					$item_title = $modelItem->title;
					$link = CHtml::link($item_title, Yii::app()->createUrl('$item_title'));
    				$messages[] = array("from" => $from, "icon" => $icon, "link" => $link, "date" => $message->date);  
		return $messages;	

Here was the view rendering part


/* @var $this InboxController */
/* @var $dataProvider CActiveDataProvider */


<h1>Message Inbox</h1>

<table class="table table-bordered">
	<tr class="info">
		<td><strong>Date Received</strong></td>

$messages = Inbox::model()->getInboxMessages(4);

foreach($messages as $message)
	echo '<tr class="warning">';
	echo "<td>".$message['from']."</td>";
	echo "<td>".$message['icon']."</td>";
	echo "<td>".$message['link']."</td>";
	echo "<td>".$message['date']."</td>";
	echo '</tr>';

Link to comment
Share on other sites

There could be between 30 - 50 of these dynamic inbox links, so that code above would be repeated for each particular case with a case statement. Hardcoding into the view is not really possible as its all dynamic info and then the view would be doing logic, i did try to push the render return array funnction into the model then load that up in the view but it seems like too much processing. Okay ill find a way to generate the links as particular actions are taken. I was thinking of creating a widget like dropdownlist for the views, because i need to make the links bold for unread messages and regular font for read.

Link to comment
Share on other sites

I've not done something like this in YII, but doesn't it make more sense to do this in the view file? Both the user links and the PM links can be done that way. Considering the class is static, this makes more sense to me. The views looks a bit uglier, but they also make more sense.


Not to insult you, but you would of course need to change the $messages array bindings for this to work. Just an easy miss...

<?php foreach($messages as $message) : ?>
    <tr class="warning">
       <td><?=CHtml::link($sender_username, Yii::app()->createUrl('$sender_username'));?></td>
       <td><i class="<?=$message['icon'];?>"></i></td>
       <td><?=CHtml::link($item_title, Yii::app()->createUrl('$item_title'));?></td>
<?php endforeach; ?>

Same with the icon. Place the HTML where it should be. The class name is enough to change the icon if you should ever need it. If not, do that fully in HTML.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...