Jump to content
Alejandro

Including unselected field options in field output

Recommended Posts

A while back someone asked if he could include, in addition to the selected options fora field, also the unselected ones, but with a different styling. I came up with a solution using the PHP formatting setting in the custom field so I am sharing it here in case someone else finds it useful.

$options = $DB->setQuery('
    SELECT 
        FieldOption.text
    FROM 
        #__jreviews_fields AS Field
    LEFT JOIN
        #__jreviews_fieldoptions AS FieldOption ON Field.fieldid = FieldOption.fieldid
    WHERE 
        Field.name = "'.$field['name'].'"
    ORDER BY
        FieldOption.text ASC
')->loadColumn();

$selected = array();
foreach ($output AS $key=>$val)
{
    $selected[strip_tags($val)] = $val;
}
$newOutput = array();
$selectedKeys = array_keys($selected);
foreach ($options AS $key=>$val)
{
    if (in_array(strip_tags($val), $selectedKeys)) {
        $options[$key] = $selected[strip_tags($val)];
    }
    else {
        $options[$key] = sprintf('<span class="jrUnselectedOption">%s</span>',$val);
    }
}
return array_values($options);

The unselected options will be wrapped in the span with the "jrUnselectedOption" CSS class so you can style that whichever way you want.

Share this post


Link to post

hello, i used this for a checkboxes field

 

$options = $DB->setQuery('
    SELECT 
        FieldOption.text
    FROM 
        #__jreviews_fields AS Field
    LEFT JOIN
        #__jreviews_fieldoptions AS FieldOption ON Field.fieldid = FieldOption.fieldid
    WHERE 
        Field.name = "'.$field['name'].'"
    ORDER BY
        FieldOption.text ASC
')->loadColumn();

$selected = array();
foreach ($output AS $key=>$val)
{
    $selected[strip_tags($val)] = $val;
}
$newOutput = array();
$selectedKeys = array_keys($selected);
foreach ($options AS $key=>$val)
{
    if (in_array(strip_tags($val), $selectedKeys)) {
        $options[$key] = sprintf('✔ %s',$val);
    }
    else {
        $options[$key] = sprintf('<span class="jrUnselectedOption"><font color="#A4A4A4">   %s</font></span>',$val);
    }
}
   
return array_values($options);

i want to set following before the output

<b>Anmeldung</b><br>

in an other field i worked with that code

return 'Anmeldung'. $total;

but in this code this doesnt work. i dont find a way, that i get the "Anmeldung" before the array_values(options). i think i must adapt somethin on "return array_values($options);", but i dont know what 😞

Edited by www.HDsports.at

Share this post


Link to post

Hi

The easiest solution would be to create a banner custom field where you can add any text you want and also include the entire output of this field.

Share this post


Link to post

Hello Alejandro,

yes, but i want to show this field only when minimum 1 opinion was checked. when i create a banner custom field, than it shows the output also, when there is no opinion checked.

Share this post


Link to post

Then you will need to create your own output for the field based on the $output array. I didn't test the code below, but you need to do something like that by converting the array into a string.

return '<b>Anmeldung</b><br><ul><li>'.implode('</li><li>',$options).'</li></ul>';

You can find more info on PHP implode and if you prefer, you can also use a foreach statement.

Share this post


Link to post

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Please refer to our Privacy Policy.