mawi27
(Mwi Nor)
November 11, 2010, 8:39am
1
Hi guys,
in my model i have a status field with 4 possible values. In my view i want to display different image / alt / title for each value, like:
//model
const TRAVEL_OK = 0;
const TRAVEL_MONEY = 1;
const TRAVEL_TIME = 2;
const TRAVEL_PREMIUM = 3;
public function getTravelStatus()
{
$user = User::model()->loadUser();
if ($user->property = xxx)
return self::TRAVEL_TIME;
...
}
//view
<b><?php echo 'status'; ?>:</b>
<?php if($data->travelStatus == 0): ?>
<img src='images/ico_ok.gif' alt='ok' title='ok' /><a href="...">xyz</a>
<?php elseif($data->travelStatus == 1): ?>
<img src='images/ico_money.gif' alt='money' title='money' />
...
<br />
My question, is there any better way to build the view than in my given example with the if…elseif construct? I don’t really like it in the view (same with switch…case)
How do you do this kind of logic?
TIA,
Marco
mawi27
(Mwi Nor)
November 11, 2010, 8:44am
2
Sorry, i put this in the wrong forum. I am using Yii 1.1.x actual version.
Marco
There are plenty of options…
Here is one (good if you update the images on a CMS):
you can create a lookup table in your database that is related to the travelStatus
create a property in your model class that says (for example) getStatusImage
Inside this function you get the related image based on status property
Another one:
Don’t use a table, just a hardcoded array in the model or in params (config main.php file) - (good if you dont update status very often or at all)
create the property and repeat the procedures above
Another way (good if shared among other models):
Create a behavior and attach it to the models using it
Call its function or property statusImage and… well… i think is enough
I am sure there are plenty of them… I just throw some that comes to my mind. Good luck
andy_s
(Arekandrei)
November 11, 2010, 10:34am
4
I suggest to add a new method to your model:
public function getTravelStatusText()
{
switch ($this->getTravelStatus())
{
case self::TRAVEL_OK: return 'ok';
case self::TRAVEL_MONEY: return 'money';
case self::TRAVEL_TIME: return 'time';
case self::TRAVEL_PREMIUM: return 'premium';
}
}
and in the view:
<b><?php echo 'status'; ?>:</b>
<img src="images/ico_<?php echo $data->travelStatusText; ?>.gif" alt="<?php echo $data->travelStatusText; ?>" title="<?php echo $data->travelStatusText; ?>" /><a href="...">xyz</a>
mawi27
(Mwi Nor)
November 11, 2010, 10:58am
5
Antonio,
i didn’t see the obvious! Simply returning an array with the Status and the image from the model solved my problem.
Thanks for pointing me in the right direction!
Marco
mawi27
(Mwi Nor)
November 11, 2010, 11:17am
6
@andy_s ,
thanks for this idea, too. Hmm, maybe i rather use your suggestion than having one array between all the single scalar properties of my model…
mawi27
(Mwi Nor)
November 11, 2010, 11:33am
7
My code looks much better now:
//model
public function getTravelStatus()
{
if ($...)
return self::TRAVEL_MONEY;
if ($...)
return self::TRAVEL_TIME;
if ($...)
return self::TRAVEL_PREMIUM;
return self::TRAVEL_OK;
}
public function getTravelIcon()
{
switch ($this->getTravelStatus())
{
case self::TRAVEL_OK:
return '<img src="ico_book.gif" alt="" title="book" />';
case self::TRAVEL_MONEY:
return '<img src="ico_money.gif" alt="" title="money" />';
case self::TRAVEL_TIME:
return '<img src="ico_time.gif" alt="" title="time" />';
case self::TRAVEL_PREMIUM:
return '<img src="ico_premium.gif" alt="" title="premium" />';
}
}
//view
<b><?php echo 'status'; ?>:</b>
<?php echo $data->travelIcon; ?>
<?php echo ($data->travelStatus == City::TRAVEL_OK) ? CHtml::link('book', array('travel', 'id'=>$data->id)) : ''; ?>
<br />
Thanks again for pointing me in the right direction.
Marco