I need listbox of multiple selection in yii, i have code of form area
but its saving to database as a word "Array" in field, How to handle
this problem?
how to get back while view and update and grid view also
<?php echo $form->dropDownList($model,'clients',
CHtml::listData(client::model()->findAll(array('order'=>'id')), 'id', 'name'),
array('empty'=>'','multiple'=>'multiple','style'=>'width:400px;','size'=>'10'));
?>
Thank you.
For me works this:
'multiple'=>true
Your code must be something like that:
<?php echo $form->dropDownList($model,'clients',
CHtml::listData(client::model()->findAll(array('order'=>'id')), 'id', 'name'),
array('empty'=>'','multiple'=>true ,'style'=>'width:400px;','size'=>'10'));
?>
$htmlOptions = array('size' => '5', 'multiple' => 'true','style'=>'width: 333px');
$model->field_id = array_of_data_to_be_selected
$form->listBox($model,'field_id',$listData, $htmlOptions);
If it is a relation you may want to use this : http://yiiext.github.com/activerecord-relation-behavior/ which takes care of saving array into many to many relation junction table.
Otherwise, like Orlymee said, you need to save each item of the array by looping through it or you can serialize the array or implode it into comma separated values and do the reverse of whatever method you chose to save, while viewing.
keep this code in controller
$arr = implode(",",$model->attributes['hobbies']);
$model->hobbies=$arr;
in controler create,update in the first if condition
in database you can see the values with comma as delimiter
How does this work in CHtml::listBox()
if(!empty($htmlOptions['multiple']))
{
if(substr($name,-2)!=='[]')
$name.='[]';
}
So you can try this
<?php echo $form->dropDownList($model,'clients',
CHtml::listData(client::model()->findAll(array('order'=>'id')), 'id', 'name'),
array(
>>> 'name'=>CHtml::resolveName($model, 'clients').'[]',
'empty'=>'',
'multiple'=>'multiple',
'style'=>'width:400px;',
'size'=>'10',
)
);?>
But it's better to use CHtml::listBox()
Related
CHtml::dropDownList('name','select',$listData,$htmlOptions);
Everything is OK until I faced one issue. I've got an array, which looks like this:
array(
array('ua', 'Ukraine', '380'),
array('ru', 'Russia', '7'),
...
array('kz', 'Kazakhstan', '7'),
);
$listData is an array of (value=>label). First I walked through array and made (code=>country) array as $listData. But I found that different countries may have the same code. I can use first "two letter geo" as key, and $listData will be an unique array.
And what if I need the same value but under different labels?
It seems that the only Yii solution is to concatenate labels under one key(value).
Or use pure html and echo each option separate.
I think the sensible way to do this is with a new model, with a new id (INTEGER PRIMARY KEY AUTOINCREMENT), and hold the data in separate fields (country, geo, code.)
Create your listdata to be an array using only the new id and the country so the array looks something like this:
array( 0=>'Ukraine', 1=>'Russia', 2=>'Kazakhstan' );
This way you will only send the id, and figure out the data (geo, code, country) on the receiver side.
But I must ask about this:
It seems that the only Yii solution is to concatenate labels under one key(value).
How do you want a option-tag to look like exactly?
<option value="ua" code="380">Ukraine</option> // Like this?
Then you should set htmlOptions similar to this:
array(
'ua'=>array('code'=>'380'),
'ru'=>array('code'=>'7'),
...
);
in a dropdown I would like to find data in 2 levels. Maybe my logic is wrong, but as I remember I have done such things before, the only difference was that I got always 1 simple result back, but now, I should handle an array. Here's my code:
echo $form->dropDownList($model, 'szeriaGyartmanyId', GxHtml::listDataEx(
SzeriaGyartmany::model()->findAllAttributes(
null, true, 'rajz_osszetett_technologia_id IN (:rajz_osszetett_technologia_id) AND keszDb<db', array(
':rajz_osszetett_technologia_id' => RajzOsszetettTechnologia::model()->findAllAttributes(
null, true, 'osszetett_technologia_id = :osszetett_technologia_id', array(
':osszetett_technologia_id' => OsszetettTechnologia::model()->find("name='Horganyzás alatt'")->id
)
)->id
)
)
), array('style' => 'width: auto', 'prompt' => ''));
the core gives back one single ID, it's no problem, but the second level gives back an array (or array of objects? I'm not sure). The point is, is it possible here somehow to implode resulting rajz_osszetett_technologia_ids, or do I have to do completely differently? I've tried to implode it right in place, but I got an error: Argument must be an array. So that's why I guess the result is an array of objects.
Is it clear what I would like to achieve? For me it seems kinda obvious to do it somehow like this, but maybe my logic is completely wrong. Can somebody please point me to the right direction?
Thanks a lot!
BR
c
GxActiveRecord::findAllAttributes(null,true..) returns an array of objects with only the required properties set. In order to obtain an array of ids as required you need to wrap it in GxHtml::listDataEx() and then use array_keys to obtain only the keys.
echo $form->dropDownList($model, 'szeriaGyartmanyId', GxHtml::listDataEx(
....
':rajz_osszetett_technologia_id' => implode(',',
array_keys(
GxHtml::listDataEx(
RajzOsszetettTechnologia::model()->findAllAttributes(
....
)
)
)
)
....
)
Perhaps a custom query would be easier and clearer than this.
I am creating project in yii framework. I am having table as-
Qbquestion QbquestionOption
-questionId -optionId
-question -questionId
-userId -option
-isPublished -isAnswer
In QbquestionOption controller i want to access Qbquestion tables fields. I had written quesry as-
$Question=Qbquestion::model()->findAllByAttributes(array("questionId"=>$number));
where $number is some random number.
When i am using its fields as= $Question->isPublished then its giving error as "trying to get access to property of non-object"
Statement var_dump($Question) is showing all record and all values of Qbquestion table. So how can i access records?
Please help me
$Question is an array of models you cannot call model function on that..
what you can do is:
$questions = Qbquestion::model()->findAllByAttributes(array("questionId"=>$number));
foreach($questions as $question)
$question->isPublished()
or you can use findByAttribute to get single result..
findAllByAttributes returns an array of objects.
If you just want one question, use findByAttributes instead, then it should work as you want.
Try As I don't know your relation n all. Just give a try. I'll post other answers if it don't works. Also tel me in which table isPublished field is? If it in other table as your title mentioned you need to change it as echo $qRec->OtherTableRelationArrayKey->isPublished;
foreach($Question AS $qRec)
{
echo $qRec->isPublished;
echo "<br />";
}
I am using an yii dropDownList for creating a drop-down in my form. I want one value for example '78'=>'selected' gets default selected in drop-down .My drop down is
dropDownList($testcontroller,'testid',CHtml::listData(Phases::model()->findAllByAttributes(array('pid'=>$pid)), 'id', 'phaseName'));
Can any one help me in doing this
Thanks ;)
dropDownList($testcontroller,
'testid',
CHtml::listData(Phases::model()->findAllByAttributes(array('pid'=>$pid)),
'id',
'phaseName'),
array('options' => array('78'=>array('selected'=>true))));
if its coming from database, use something like below (in case of list box or multiple allowed dropDownList use multiple=>true)
foreach ($selections as $eachValue)
$selectedOptions[$eachValue] = array('selected'=>'selected');
echo $form->dropDownList($model,
'testid',
CHtml::listData(Phases::model()->findAllByAttributes(array('pid'=>$pid)),
'id',
'phaseName'),
array('multiple'=>'true','prompt'=>'select ','options'=>$selectedOptions));
More details about dropDownList : dropDownList() method
suppose you want to show drop down of menu_name from Menu model and selected value on the basses of parent_menu_id then you can use it like this
<?php echo $form->dropDownList($model,'parent_menu_id', CHtml::listData(Menu::model()->findAll(), 'menu_id', 'menu_name'), array('empty'=>'--please select--')); ?>
This can be done by passing this in your html options of dropdown..
dropDownList($testcontroller,
'testid',
CHtml::listData(Phases::model()->findAllByAttributes(array('pid'=>$pid)),
'id',
'phaseName'),
array('options' => array('78'=>array('selected'=>true))));
and the value 78 will be selected.
for prompt one can do something like this:
dropDownList($testcontroller,'testid', CHtml::listData(Phases::model()->findAll(), 'id', 'phasename'), array('empty'=>'Select an option'));
check this for help. Thanks.
I have a php function that generates a comma separated list of Post IDs that specify the order those posts should display on my WordPress site.
I'm looking for a place I can insert an SQL query like:
SELECT * FROM wp_posts WHERE ID IN ('1', '2', '3')
Which .php file should I modify? How should I phrase the SQL?
Paste your php function in file (root wordpress folder)/wp-content/themes/(name of your theme)/functions.php
SQL query should be like.
$sql=$wpdb->prepare("SELECT * FROM wp_posts WHERE ID IN ('1', '2', '3');");
$results=$wpdb->get_results($sql);
echo ">>>>".$result[0]->something."<br />";//blah blah...
WoW,i did not mention $wpdp as global,still code was working fine on my system!,anyways
i would suggest you write "global $wpdb" in your function.
Now the main question is from where should you call this function;
you have to call it from theme/(your theme)/index.php
Try figuring out how the posts are generated in your current theme,and you might get what you need to do!
First off you should use wp_query for this. It is designed to query posts.
<?php
$args = array(
'post__in' => array ( 1,2,3) //the Ids
);
$query = new WP_Query($args);
if ($query->have_posts()){
while ($query->have_posts()) {
$query->the_post();
echo get_the_ID().'<br/>';
}
} else {
echo 'no posts found';
}
About the placement
I advise to do it in a custom template, make a simple template with the code above, create a page assign it that template and make the page private.
global $wpdb; $results = $wpdb->get_results("SELECT * FROM {$wpdb->posts} WHERE ID IN (1, 2, 3) ORDER BY ID");
You can use that in your theme's functions.php file.