Dynamic select distinct
Permalink
Hi all, I use "concrete5" last version, and I would like to know how to make a "Select distinct" on an attribute that I added in page. I would like to make a dynamic select list.
I do this but I have problem to build the query
$db = Loader::db();
$ak_locations = CollectionAttributeKey::getByHandle('region');
$r = $db->Execute("SELECT DISTINCT ....);
Need help!!!
$db = Loader::db();
$ak_locations = CollectionAttributeKey::getByHandle('region');
$r = $db->Execute("SELECT DISTINCT ....);
Need help!!!
if you look in the select attribute's controller (concrete/attributes/select/controller.php) around line 541, you will see a function getOptionUsageArray()
I think this is your best bet. You can either use that function and filter through the result to remove options with zero use. Or you can have a look at the sql query they're using to adapt it to your need.
One way you could modify it is by making sure it doesn't return any result with zero use so you don't have to filter it afterward.
I think this is your best bet. You can either use that function and filter through the result to remove options with zero use. Or you can have a look at the sql query they're using to adapt it to your need.
One way you could modify it is by making sure it doesn't return any result with zero use so you don't have to filter it afterward.
Thank you mnakalay, it's very helpful, and you give me a good away :-)
I did this and it's work :
I did this and it's work :
$reg = CollectionAttributeKey::getByHandle('Region'); $options = $reg->getController()->getOptionUsageArray(); if (!empty($options)) { print("<Select>"); foreach($options as $option) { printf("<option value=\"%s\">%s</option>", $option->getSelectAttributeOptionDisplayValue(), $option->getSelectAttributeOptionDisplayValue()); } print("</Select>"); }
Is there a way to sort the elements of the option ???
I have a list of pages with attribute region like :
Page1 region =montreal
Page2 region =montreal
Page3 region =quebec
Page4 region =quebec
Page5 region =Charlevoix
And I would like to dynamically populate the drop-down list with only regions where I have a page, like :
<select>
<option value="1">montreal</option>
<option value="2">quebec</option>
<option value="3">charlevoix</option>
</select>