Working with the "select" attribute type
Permalink
Hi!
In a single page I would like to access an Userattribute which is a select type.
I would like to display a list with all the available options the select attribute has.
I did it like this:
MY FIRST QUESTION IS:
Is this the "correct" way how to do this or is there a better, more clean way to achieve this?
MY SECOND QUESTION IS:
How do I proper save the value of a select attribute?
I tried some stuff like:
which is working fine. BUT: when I have two SelectOptions with the exact same name then it ALWAYS saves the first of the two Options, no matter which one of the both you pass to save (here I do it with the id form a POST request). It seems to me for saving the attribute the value instead of the id will be used to identify the selectoption. Is this a bug or I'm I wrong with my approach?
I hope you understand my post because of my english ;)
Thanks for any answer!
In a single page I would like to access an Userattribute which is a select type.
I would like to display a list with all the available options the select attribute has.
I did it like this:
$key = UserAttributeKey::getByHandle('myattribute'); $nb = AttributeType::getByHandle('select')->getController(); $nb->setAttributeKey($key); $options = $nb->getOptions(); foreach($options as $opt) { print $opt->getSelectAttributeOptionID() . " " . $opt->getSelectAttributeOptionValue(); }
MY FIRST QUESTION IS:
Is this the "correct" way how to do this or is there a better, more clean way to achieve this?
MY SECOND QUESTION IS:
How do I proper save the value of a select attribute?
I tried some stuff like:
... $sab = SelectAttributeTypeOption::getByID($_POST['myID']); $ui->setAttribute('myattribute', $sab); ...
which is working fine. BUT: when I have two SelectOptions with the exact same name then it ALWAYS saves the first of the two Options, no matter which one of the both you pass to save (here I do it with the id form a POST request). It seems to me for saving the attribute the value instead of the id will be used to identify the selectoption. Is this a bug or I'm I wrong with my approach?
I hope you understand my post because of my english ;)
Thanks for any answer!
Don't think that link works?
My issue is quite similar so I'll post here, when I use the helper it seems to save the index of the select rather than the actual text to the database, I use like below. It appears fine in my form but I get a number in the db???
My issue is quite similar so I'll post here, when I use the helper it seems to save the index of the select rather than the actual text to the database, I use like below. It appears fine in my form but I get a number in the db???
Here is how I've done this. Based on a Single Page/Controller configuration.
Single Page:
and then the controller code that saves:
I am not using the form helper, though you could.
But I would wonder why you would have a two identical select values.
and my getDateLineValue function, also in the controller:
Single Page:
<div class="article-date-box"> <select name="dateline_city" class="ccm-input-select" > <?php foreach($dlcity as $city) {?> <option value="<?php echo $city['id']?>" <?php if ($existing_dl_city){ if ($existing_dl_city == $city['value']) {?> selected="selected" <?php }}?>> <?php echo $city['value']?></option> <?php }?> </select> </div>
and then the controller code that saves:
$dcty = $this->getDatelineValue($this->post('dateline_city')); $p->setAttribute('dateline_city', $dcty);
I am not using the form helper, though you could.
But I would wonder why you would have a two identical select values.
and my getDateLineValue function, also in the controller:
private function getDatelineValue($svID) { $db = Loader::db(); $query = "SELECT value FROM atSelectOptions WHERE id = '$svID'"; $result = $db->query($query); if( $result ) { while( $row = $result->fetchRow() ) { $dlcity = $row['value']; } } return $dlcity; }
Best bet is to use the concrete5 helper found herehttp://www.concrete5.org/documentation/developers/forms/concrete5-w...
It will make displaying and saving attributes a hell of a lot easier on you!