I have a Module table with these column
ID_MODULE, NAMA_MODULE, ESTIMASI_HARGA, FLAG
I want to display NAMA_MODULE and ESTIMASI_HARGA on RadioButton. But I can only manage to display NAMA_MODULE with this code
<?php $items=ArrayHelper::map(Module::find()->where(['FLAG'=>1])->all(),'ID_MODULE',['NAMA_MODULE']) ?>
<?php echo $form->field($pMod, 'ID_MODULE', ['options' => ['style' => 'display:none', 'id' => 'pModule']])->inline()->checkboxList($items,$items)->label(false); ?>
Is it possible to give radio button a label with 2 column from DB with Yii?
$items = ArrayHelper::map(Module::find()->where(['FLAG'=>1])->all(),
'ID_MODULE',
function($model) {
return $model->NAMA_MODULE.'-'.$model->ESTIMASI_HARGA;
}
);
Related
My DB column name subscribed tinyint(1),
I want if checkBox is checked then value send 1 if not checked then value send 0.
This is my code-
<?php echo $form->checkBox($model,'subscribed'); ?>
Please help.
Try this:
<?php echo $form->checkBox($model,'subscribed',array('value' => '1', 'uncheckValue'=>'0')); ?>
Here, value for checked checkbox is 1 and unchecked checkbox is 0
In your view file:
<?php echo $form->checkBox($model,'attribute'); ?> //in your case 'attribute' is 'subscribed'
in your controller
<?php
//if checked, return 1;
//if unchecked, return 0;
.........
$checkbox_value=$model->attribute; //in your case 'attribute' is 'subscribed'
echo $checkbox_value;
.......
?>
Is it possible to have 2 different CGridView Tables in one admin.php ?
For example, I have a page of Services and a page of Packages.
Services are basically individual single services whereas Packages consist of individual services.
So, my question is, can I have the Service's CGridView to be displayed in Package/admin.php page? A seperate CGridView table.
Top part with list of Packages, and at the bottom, a different table, with Individual services.
If so, please guide me through it. Thanks in advance.
Updated
public function actionAdmin() {
$model = new Package('search');
$model2 = new Service('search');
$model->unsetAttributes();
$model2->unsetAttributes();
$model->active=1;
$model2->active=1;
if (isset($_GET['Package'])){
$model->setAttributes($_GET['Package']);
}
$this->render('admin', array(
'model' => $model,
'model2' => $model2,
));
}
Under _form.php:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'service-grid',
'dataProvider' => $model2->search(),
'htmlOptions'=>array('class'=>'grid-view grid-size'),
'filter' => $model2,
'columns' => array( //the appropriate columns
));
Yes this is possible and you can have any number of grid views in one page/action.
here is a example where i display two gridviews, namely Manage Subjects 1 & Manage Subjects 2
<?php
/* #var $this SubjectController */
/* #var $model Subject */
$this->breadcrumbs = array(
Yii::t('edu', 'Subjects') => array('index'),
Yii::t('edu', 'Manage'),
);
?>
<?php echo $this->renderPartial('application.views.layouts._actions', array('model' => $model)); ?>
<?php
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$.fn.yiiGridView.update('data-grid', {
data: $(this).serialize()
});
return false;
});
");
?>
<h3><?php echo Yii::t('edu', 'Manage Subjects 1'); ?></h3>
<!-- search-form -->
<div class="search-form" style="display:none">
<p>You may optionally enter a comparison operator (<b><</b>, <b><=</b>, <b>></b>, <b>>=</b>, <b><></b> or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.</p>
<?php $this->renderPartial('_search', array('model' => $model)); ?>
</div>
<!-- search-form -->
<?php echo $this->renderPartial('_grid', array('model' => $model)); ?>
<h3><?php echo Yii::t('edu', 'Manage Subjects 2'); ?></h3>
<!-- search-form -->
<div class="search-form" style="display:none">
<p>You may optionally enter a comparison operator (<b><</b>, <b><=</b>, <b>></b>, <b>>=</b>, <b><></b> or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.</p>
<?php $this->renderPartial('_search', array('model' => $model)); ?>
</div>
<!-- search-form -->
<?php echo $this->renderPartial('_grid', array('model' => $model)); ?>
Update
you need to create 2 models, see my below code, model2 is created and its a different table. you pass it to admin.php and in the gridview change model to model2 in your second gridview. thats all.
/**
* Manages all models.
*/
public function actionAdmin()
{
$model = new Subject('search');
$model2 = new Institute('search');
Yii::app()->appLog->writeLog('Manage Subjects.'); // Activity log entry
$model->unsetAttributes(); // Clear any default values
$data = TK::get('Subject');
if ($data !== null)
$model->attributes = $data;
$params = array('model' => $model, 'model2' => $model2);
if (Yii::app()->request->isAjaxRequest)
$this->renderPartial('_grid', $params);
else
$this->render('admin', $params);
}
I want to display the name of Shift instead of shift_id
I have a dropboxlist from other table that is like this
<div>
<?php echo $form->labelEx($model,'mon'); ?>
<?php echo $form->dropDownList($model, 'mon', CHtml::listData(
Shift::model()->findAll(), 'shft_id', 'name'),
array('prompt' => 'Select a Department')
); ?>
<?php echo $form->error($model,'mon'); ?>
I have two tables that is
Day: id_day,mon,tues,wed,etc
Shift: shft_id,start,end,name,status
Here is the relation in the day
'shift'=>array(self::HAS_MANY,'Shift','shft_id'),
For Shift:
'day'=>array(self::BELONGS_TO,'Day','id_day'),
It is already working. The choices in the dropbox was the name of the Shift, and puts the shft_id in the mon,tues,wed,etc. In the view of the form it looks like
id_user: 3
mon:5
tues:5
wed:6
what I wanted to be is that in the view.
id_user: 3
mon: 6am-5pm
tues: 7am-6pm
etc.etc.
I dont know what command it is. I have no idea. Help me please
Instead of User::getusername() method.
I think you can simply use this in one line.
// format models resulting using listData
<?php echo $form->dropdownlist($model,'user_id', CHtml::listData(User::model()->findAll(),'id', 'name')); ?>
below is an example which I think may solved your problem
in _form.php
<?php echo $form->dropdownlist($model,'user_id', User::getusername()); ?>
<?php echo $form->error($model,'user_id'); ?></th>
in User model
public function getusername()
{
$criteria2=new CDbCriteria;
$criteria2->select='*';
$quser=User::model()->findAll($criteria2);
foreach($quser as $r)
{
$user_id= $r->user_id;
$user_name=$r->user_name;
$user_array[$user_id]=$user_name;
}
return $user_array;
}
I'm trying to understand what you are really asking. I believe you were saying that you are already able to populate your records correctly from the dropdowns. But when you are displaying, the view is only showing the shift_id value? The relation for the day should be able to get your shift name. How are you displaying the list:
mon: 5
tue: 5
wed: 6
I'm trying to understand if you need help displaying the dropdown items properly, or how to display the shift name later after the values have been set in the database. Also, your 'Day' table has me confused. What are the field names? Are they 'day_id' and 'day'? Or do you actually name the fields after each day of the week?
If you are displaying the data through a foreach and are getting each day (let's assume it is $day) object, then the relationship to 'shift' can give you the name like this: echo $day->shift->name; if the name displays as '#am-#pm'. Otherwise you can display it like this:
echo sprintf('%d a.m. - %d p.m.',$day->shift->start,$day->shift->end);
Thanks for the people who helped me. It did lead me to the answer anyone who has the same problem here is what i did.
In my model/Day
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'monday'=>array(self::BELONGS_TO,'Shift','mon'),
'tuesday'=>array(self::BELONGS_TO,'Shift','tue'),
'wednesday'=>array(self::BELONGS_TO,'Shift','wed'),
'thursday'=>array(self::BELONGS_TO,'Shift','thurs'),
'friday'=>array(self::BELONGS_TO,'Shift','fri'),
'saturday'=>array(self::BELONGS_TO,'Shift','sat'),
'sunday'=>array(self::BELONGS_TO,'Shift','sun'),
);
}
and In my models/Shift
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'day_mon' =>array(self::HAS_MANY,'Day','mon'),
'day_tue' =>array(self::HAS_MANY,'Day','tue'),
'day_wed' =>array(self::HAS_MANY,'Day','wed'),
'day_thurs'=> array(self::HAS_MANY,'Day','thurs'),
'day_fri'=>array(self::HAS_MANY,'Day','fri'),
'day_sat'=>array(self::HAS_MANY,'Day','sat'),
'day_sun'=>array(self::HAS_MANY,'Day','sun'),
);
}
The problem is with my relations. its not set properly, so you need to set it properly then go to Day/view
<?php $this->widget('bootstrap.widgets.TbDetailView',array(
'data'=>$model,
'attributes'=>array(
'id_day',
array(
'name'=>'mon',
'value'=>CHtml::encode($model->monday->name)
),
Here is the output
If you have a detail veiw then use the following code:
widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'name',
array(
'label' => $model->shift->getAttributeLabel('shift_name'),
'value' => $model->shift->shift_name
),)); ?>
In case anybody else will be in my boat, what you will need to do is edit view.php in view/modelname/view.php
widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'name',
'country_id')); ?>
into
widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'name',
array(
'label' => $model->country->getAttributeLabel('country'),
'value' => $model->country->name
),)); ?>
in ZF1 form and form elements had a setDescription method that was outputted as <p>description here</p> in view ....
ZF2 seems that doesn't have this method
so my question is how can i add description to form elements ?
this is my view :
<div>
<?
$form = $this->form;
$form->prepare();
echo $this->form()->openTag($form);
foreach ($form->getElements() as $el) {
?>
<div class="form_element">
<?=$this->formRow($el);?>
</div>
<?
}
echo $this->partial('system/form/buttons_form_part.phtml', array('form' => $form));
echo $this->form()->closeTag();
?>
</div>
Using ZF 2.1.5, one solution might be setOptions().
In the form definiton:
$file = new Element\File('file');
$file->setLabel('Photo (.jpg, .gif, .png)');
$file->setOptions(array('description' => 'test description'));
…
When rendering the form element:
$element = $form->get(…);
var_dump($element->getOptions());
Will give you access to:
array(1) { ["description"]=> string(16) "test description" }
If you mean a label for an Element you can use the setLabel method when you create the form (in Controller).
$name = new Element('name');
$name->setLabel('Your name');
Or if you use an array to create your form elements use this:
array(
'spec' => array(
'name' => 'name',
'options' => array(
'label' => 'Your name',
),
'attributes' => array(
'type' => 'text'
),
)
),
Here is a link:
enter link description here
I want to display country name instead of country id in user profile view( country id is FK in user profile tbl) .
anyone can help me with this ?
This is my view controller/action in which I have Country model as well:
public function actionView($id)
{
$model = new TblUserProfile;
$model = TblUserProfile::model()->find('user_id=:user_id', array(':user_id' => $id));
$countrymodel = new Country;
$countrymodel = Country::model()->findAll();
// var_dump($countrymodel->name);
// die();
$this->render('view', array(
'model' => $model ,
'country' =>$countrymodel
));
}
This is my view
<div class="view">
<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>
<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('user_id')); ?>:</b>
<?php echo CHtml::encode($data->user_id); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('user_occuption')); ?>:</b>
<?php echo CHtml::encode($data->user_occuption); ?>
<br />
<b><?php
// $model = TblUserProfile::model()->find('country_id=:country_id', array(':user_id' => $id));
//echo CHtml::encode($model->getAttributeLabel('country')); ?>:</b>
<?php// echo CHtml::encode($model->name);
?>
<br />
</div>
Now I want to display country name in above view.
These are the relationships of country and user profile table
public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'user' => array(self::BELONGS_TO, 'TblUser', 'user_id'),
'country' => array(self::BELONGS_TO, 'country', 'country_id'),
'state' => array(self::BELONGS_TO, 'state', 'state_id'),
'city' => array(self::BELONGS_TO, 'city', 'city_id')
);
}
Use the following code:
<b><?php echo CHtml::encode($data->country->getAttributeLabel('name')); ?>:</b>
<?php echo CHtml::encode($data->country->name); ?>
<br />
Provided that in your country model, the country name column is name.
Since the relation is in place we can use it to access the related country for each user.
In detailview it'll change to:
// instead of 'country_id'
array(
'name'=>'country_id',
'value'=>$model->country->name
)
Read the CDetailView doc to see how you can change even more things.
I would use:
$model = TblUserProfile::model()->with('country')->find('user_id=:user_id', array(':user_id' => $id));
with('country') explained: the string country comes from the array relations key.
Then to display in the view you would use:
$model->country->CountryName;
$model contains
country contains a "model" of country joined by the relation foreign key
CountryName is the column name in the Country table
This is the recommended way to do it in the MVC way. Both my example and the one above work, but the one above has more process/logic decisions in the view, which breaks the concept of MVC where the Model should be the fat one and contain all logic/processing possible, then controller will pull this data and provide it to the view.
You can also follow this below answer:
Just put the below code in your view.php file
[
"attribute"=>"User_Country_Id",
'value' =>$model->country->conName ,
],
[
"attribute"=>"User_State_Id",
'value' =>$model->states->stsName ,
],
[
"attribute"=>"User_City_Id",
'value' =>$model->cities->ctName ,
],