I have stored gender in format(1=>male,2=>female) i want to display male if value 1 and female if value 2 in detail view and grid view please help me
I have stored gender in format(1=>male,2=>female) i want to display male if value 1 and female if value 2 in detail view and grid view please help me
In DetailView:
<?php $this->widget('zii.widgets.CDetailView', array(
'data' => $model,
'attributes' => array(
...
array(
'name' => 'gender',
'value' => $model->gender == 1 ? 'male' : 'female',
),
...
),
)); ?>
In GridView
<?php $this->widget('zii.widgets.grid.CGridView', array(
...
'columns' => array(
...
array(
'name' => 'gender',
'value' => '$data->gender == 1 ? "male" : "female"',
),
...
),
)); ?>
Or, add the interpretation as a method of your User model, so you can access it without the logic in the view:
public function getGenderLabel()
{
return $this->gender == 1 ? 'male' : 'female';
}
Then in the grid you can just:
<?php $this->widget('zii.widgets.grid.CGridView', array(
...
'columns' => array(
...
'genderLabel',
...
),
)); ?>
Thanks a lot please tell me how to change the search method if type male it should show only male
2Dana:
This is a solution too, but in such case we need to configure DataProvider to make this field sortable.
Here’s what I’d do:
Controller:
$genderFilter = array('1' => 'Males', '2' => 'Females');
$this->render('index', array(
'model' => $model,
'issueFilter' => $issueFilter,
));
In the view:
<?php echo CHtml::form('genderfilter','get'); ?>
Show:
<?php echo CHtml::dropDownList('genderFilter',
isset($_GET['genderFilter'])?(int)$_GET['genderFilter']:0,
$genderFilter,
array('empty'=>'All Sexes', 'submit'=>'')); ?>
<?php echo CHtml::endForm(); ?>
Then, in my model - in the search() function:
if (isset($_GET['genderFilter'])) {
if($_GET['genderFilter'] !== 0) $criteria->compare('gender', $_GET['genderFilter'], true);
}
Something of that sort.
in controller where should i put the code
I fix with the following is it correct
In model search()
===============
$gender_name=strtolower($this->gender);
if($gender_name=='female')
$gender_id=2;
elseif ($gender_name=='male')
$gender_id=1;
$criteria->compare('gender',$gender_id);
In cDetailview
=================
array(
'name' => 'gender',
'value' => $model->gender == 1 ? 'Male' : 'Female',
),
In Gridview
=============
array(
'name' => 'gender',
'value' => '$data->gender == 1 ? "male" : "female"',
),
at blog demo there is nice solution to convert numeric values to strings:
CREATE TABLE tbl_lookup
(
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(128) NOT NULL,
code INTEGER NOT NULL,
type VARCHAR(128) NOT NULL,
position INTEGER NOT NULL
);
--------------------------------
|id|name |code|type |position|
--------------------------------
|1 |Male |1 |Gender| 1|
--------------------------------
|2 |Female|2 |Gender| 2|
--------------------------------
copy Lookup.php from http://code.google.com/p/yii/source/browse/trunk/demos/blog/protected/models/Lookup.php
in your view:
array(
'name' => 'gender',
'value' => Lookup::item("Gender",$model->gender),
),
Another way of doing this (i rather not have a mysql lookup table for 2 values):
//in user model:
const GENDER_MALE = 1;
const GENDER_FEMALE = 2;
public function getGender()
{
switch ($this->gender)
{
case self::GENDER_MALE:
return 'male';
case self::GENDER_FEMALE:
return 'female';
}
}
/**
* @return array genders for dropdown controls
*/
public static function getGenders()
{
return array(
self::GENDER_MALE => 'male',
self::GENDER_FEMALE => 'female'
);
}
// in zii.widgets.CDetailView
'attributes' => array(
id,
array(
'label' => 'Gender',
'type' => 'raw',
'value' => $model->getGender(),
),
...
// in zii.widgets.grid.CGridView
'columns' => array(
'id',
'name',
array(
'name' => 'gender',
'value'=>'$data->getGender()',
'filter' => User::getGenders(),
'sortable' => TRUE,
),
...
Marco
Thanks marco i like your way of coding
I like this solution! but what if the $model->gender is not stored in current table, but stored in another table, for arguments sake, let’s call that table “tableX”. How would you access that tables info?
Just for clarification - I have 3 tables… 1 with username_id, username and password etc, 1 with role_id and roles (like admin, user, teacher etc.) and the other table tells me which username_id has which role… I’m eally battling to access the other referenced tables info!
Can anyone PLEASE help me!
I’m learning a lot here just by reading the posts. Thanks!
I don’t think you need to add it to the database unless you need to manage it. There are only two genders and this wont change so I would hardcode in an array and use a model method as Dana shows in her example. Database calls are slow and this is an easy optimization.