how can I save value as ivar from dropDownlist in yii? here my code, which doesn't work :/
declaration of ivar:
public $blaaa;
and my dropdownlist code:
echo CHtml::dropDownList(
'categoryDropDownList',
'',
CHtml::listData(Category::model()->findAll(), 'id', 'name'),
array(
'empty' => 'Select Category',
'onchange'=>function () {
$this->blaaa = 'js:this.value';
}
));
I tried also with ajax, but it also doesnt work:
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('category/actionBla'),
'data' => array('id' => 'js:this.value')
where
public function bla()
{
if (isset($_POST['id'])) {
$this->blaaa = $_POST['id'];
}
thanks in advance for help!
In ajax section, change url as below
'url' => CController::createUrl('category/bla'),
In controller, your function name should be
public function actionBla()
I solved my problem with echo'ing CHtml::dropDownList in form:
echo CHtml::form();
echo CHtml::dropDownList(
'categoryDropDownList',
'',
CHtml::listData(Category::model()->findAll(), 'id', 'name'),
array(
'empty' => 'Select Category'
));
echo CHtml::endForm();
and now activeForm bother about sending parameters via POST
Related
I'am trying to implement a dropdownlist inside the gridview, but it is not working.
Here's my code
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->roadies_search(),
'filter' =>$model,
'columns'=>array(
array(
'name'=>'hname_search',
'header'=>'Hospital Name',
'value'=>'($data->AssociatedHospitals) ? $data->AssociatedHospitals : ""',
),
array(
'type'=>'raw',
'header'=>'Users',
'id' => 'test',
'value'=>function(){
$res=Yii::app()->db->createCommand("SELECT name,id FROM med_user WHERE rank>3")->queryAll();
$html = '<select name="user" style="width:100%;">
<option selected>Select User</option>
';
foreach ($res as $key) {
$html.='<option value="'.$key['id'].'">'.$key['name'].'</option>
';
}
$html.='</select>';
return $html;
},
),
)
));
I have tried it using the following function too:
public function getUsername(){
$res=Yii::app()->db->createCommand("SELECT name,id FROM med_user WHERE rank>3")->queryAll();
return CHtml::dropDownList('usersselectlist'.$this->id,'user_id',CHtml::listData($res, 'id', 'name'));
}
I want to show all the users as a dropdown, for every row in the gridview.
EDIT
I figured out that the dropdown works fine for firefox, but the problem is with chrome. It is not showing the selected values.
Here's an example
Chrome: http://i.imgur.com/eAvBRwO.png
Firefox: http://i.imgur.com/7wBc1GX.png
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->roadies_search(),
'filter' =>$model,
'columns'=>array(
array(
'name'=>'hname_search',
'header'=>'Hospital Name',
'value'=>'($data->AssociatedHospitals) ? $data->AssociatedHospitals : ""',
),
array(
'type'=>'raw',
'header'=>'Users',
'id' => 'test',
'value'=>function(){
// but better prepare data in model and set here from controller
$dataReader = Yii::app()
->db
->createCommand("SELECT name,id FROM med_user WHERE rank>3")
->query();
$dropList = array();
foreach ($dataReader as $row) {
$id = $row['id'];
$dropList[$id] = $row['name'];
}
return CHtml::dropDownList('my', null/* or selected id */, $dropList);
},
),
)
));
It's really bad way. You will do SQL query for each row, why not use it outside of grid?
$res=Yii::app()->db->createCommand("SELECT name,id FROM med_user WHERE rank>3")->queryAll();
$select_data = CHtml::listData($res, 'id', 'name');
......
array(
'type'=>'raw',
'header'=>'Users',
'id' => 'test',
'value'=>function($data) use ($select_data){
return CHtml::dropDownList('usersselectlist'.$data->id','',$select_data);
},
),
.......
Something like this should work.
I am using CAutoComplete to select multiple comma separated values. Now I am able to pass only the selected values, I need to pass the id of selected values also when the form is submitted.
Can you please help me to send the id on submit as an array or comma separated.
In my form I am using:
<?php
$this->widget('CAutoComplete', array(
'model' => $model,
'attribute' => 'skills',
'url' => array('serviceRequest/Suggest'),
'multiple' => true,
'htmlOptions' => array('size' => 34),
));
?>
Controller:
public function actionSuggest()
{
if (isset($_GET['q']) && ($keyword = trim($_GET['q'])) !== '')
{
$tags = Skills::model()->suggest($keyword);
if ($tags !== array())
echo implode("\n", $tags);
}
}
Model:
public function suggest($keyword,$limit=20)
{
$tags=$this->findAll(array(
'condition'=>'skills LIKE :keyword',
// 'order'=>'Name',
'limit'=>$limit,
'params'=>array(
':keyword'=>'%'.strtr($keyword,array('%'=>'\%', '_'=>'\_', '\\'=>'\\\\')).'%',
),
));
$names=array();
foreach($tags as $tag)
$names[]=$tag->skills;
return $names;
}
you can pass any option that the JUI autocomplete widget supports by including an options array:
$this->widget('CAutoComplete', array(
// your other settings here
'options' => array(
'select' => new CJavaScriptExpression('function(e, ui) { alert("hi"); }')
),
));
Read here for more: http://www.yiiframework.com/doc/api/1.1/CAutoComplete
Hello i created a separate search form having input box and button.
in my model i want to search products by category wise...
but problem is that when input box is empty and clicking on search buttons it displays all entries from the database table..
controller code is-
class AddController extends Controller
{
public function actionAddsearch()
{
$model_form = new Add("search");
$attributes = Yii::app()->getRequest()->getPost("Add");
if(!is_null($attributes))
{
$model_form->setAttributes(Yii::app()->getRequest()->getPost("Add"));
}
$this->render("searchResults", array(
"model" => $model_form,
"models" => $model_form->searchAdd(),
));
}
model code--
class Add extends CActiveRecord
{
public function searchAdd()
{
$criteria = new CDbCriteria();
$criteria->compare("addname", $this->category, TRUE, "OR");
$criteria->compare("category", $this->category, TRUE, "OR");
return Add::model()->findAll($criteria);
}
view code- addsearch.php
<div class="search-bar">
<?php
$form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
"action"=>$this->createUrl("add/addsearch"),
'type'=>'search',
)
);
echo $form->textFieldRow($model,'category');
echo "        ";
$this->widget('bootstrap.widgets.TbButton',array(
'buttonType'=>'submit',
'type'=>'success',
'size'=>'large',
'label'=>'Search For Products ',
));
$this->endWidget();
?>
</div>
searchResults.php
<?php
echo "<h4>Results for Your Search</h4>";
foreach($models as $model):
$this->beginWidget('bootstrap.widgets.TbDetailView',array(
'type'=>'bordered condensed',
'data' => array(
'Shop Name' =>CHtml::link(CHtml::encode($model->addname),array('add/view','id'=> $model->addid)),
'Category' => $model->category
),
'attributes' => array(array('name' => 'Shop Name', 'label' => 'Name of Shop','value'=>CHtml::link(CHtml::encode($model->addname),
array('add/view','id'=>$model->addid)),'type'=>'raw'),
array('name' => 'Category', 'label' => 'Category of Shop'),
),
)
);
echo "<br><hr><br>";
$this->endWidget();
endforeach;
?>
what is wrong in the code??
I want to display no any product when text box is empty..
thanks in advance
change this
if(!is_null($attributes))
{
$model_form->setAttributes(Yii::app()->getRequest()->getPost("Add"));
}
$this->render("searchResults", array(
"model" => $model_form,
"models" => $model_form->searchAdd(),
));
To
if($attributes)
{
$model_form->setAttributes(Yii::app()->getRequest()->getPost("Add"));
$this->render("searchResults", array(
"model" => $model_form,
"models" => $model_form->searchAdd(),
));
}
1) Is the 'caterory' attribute safe on search scenario ? (in rules)
2) category is an attribute of the table schema ?
In my view I have a link which is calling a function in Controller. The function in controller is making a pdf. It is supposed to make pdf of only $model->id. But I am unable to send the value of $model->id into controller via my link.
View
<?php
echo CHtml::link('Save/Print',array('print'),array('class'=>'btnPrint btn btn-info','target'=>'new'));
?>
<?php
$this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'name',
'father_name',
'cnic',
'customername',
),
));
?>
Controller
public function actionPrint($id) {
ini_set('max_execution_time',360);
ini_set('memory_limit', '128M');
$mPDF1 = Yii::app()->ePdf->mpdf('','A4');
$mPDF1->SetHTMLHeader('<h3 style="text-align: center;">'.mb_strtoupper(str_replace('Hello','',Yii::app()->name),'UTF-8').'</h3>');
// $id=35;
$records = Candidate::model()->findByPk($id);
$html = '';
$html .= $this->renderPartial('view', array('model'=>$records),true);
$mPDF1->WriteHTML($html, false);
$mPDF1->Output();
}
How will I send the value of id?
go like this for singel link
// $model->id is the id you want to send
echo CHtml::link(
'Save/Print',
Yii::app()->createUrl('Save/Print' , array('id' => $model->id)),
array('class'=>'btnPrint btn btn-info','target'=>'_blank'));
if you want it in a grid
$this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
array(
'name' => 'id',
'value' => 'echo CHtml::link(
"Save/Print",
Yii::app()->createUrl("Save/Print" , array("id" => $data->id)),
array("class"=>"btnPrint btn btn-info","target"=>"_blank"));',
'type' => 'raw',
),
'name',
'father_name',
'cnic',
'customername',
),
));
Hey guys I'm having trouble running an update query through a form in codeigniter. There is no error returned, but the query does not update in the database. It should be noted there's another form on the same page running an insert query that works fine.
View:
<?php
$modify = array(
'id' => 'modify-form',
'class' => ''
);
echo form_open('main/modify_idea', $modify);
$ta_modify = array(
'id' => $row->id,
'name' => 'ta_modify',
'label' => 'ta_modify',
'placeholder' => $row->idea,
'method' => 'Post'
);
echo form_textarea($ta_modify);
echo form_hidden('facebook_id', $session['id']);
echo "<div id='button'>";
echo form_submit('submit', 'Post');
echo "</div>";
echo form_close();
?>
Controller:
public function modify_idea() {
$this->load->model('idea');
$this->idea->modify_idea();
redirect('main/members');
}
Model:
public function modify_idea() {
$data = array(
'id' => $this->input->post('id'),
'idea' => $this->input->post('ta_modify'),
'facebook_id' => $this->input->post('facebook_id')
);
$query = $this->db->query("UPDATE idea SET `idea` = ? WHERE `id` =?", array($data['idea'], $data['id']));
return $query;
}
I've utilized the following resources but still no luck:
CodeIgniter MySQL query not working
CodeIgniter MySQL Query not returning any data, even though there definitely is data to be returned!
http://www.tutorialspoint.com/mysql/mysql-update-query.htm
https://www.codeigniter.com/userguide3/database/query_builder.html#deleting-data
As always, thanks for the help!
Pass your post data from controller to model
Change your controller code:
public function modify_idea() {
$this->load->model('idea');
$data = array(
'id' => $this->input->post('id'),
'idea' => $this->input->post('ta_modify'),
'facebook_id' => $this->input->post('facebook_id')
);
$this->idea->modify_idea($data);
redirect('main/members');
}
in model do this
public function modify_idea($data) {
$query = $this->db->query("UPDATE idea SET `idea` = ? WHERE `id` =?",
array($data['idea'], $data['id']));
return $query;
}
Please pass your post values to model from controller. Then it will work.