3

print_r($item->jcfields);

Array
(
    [1] => stdClass Object
        (
            [id] => 1
            [title] => Specialisms
            [name] => specialisms
            [checked_out] => 0
            [checked_out_time] => 0000-00-00 00:00:00
            [note] => 
            [state] => 1
            [access] => 1
            [created_time] => 2018-06-16 18:01:45
            [created_user_id] => 903
            [ordering] => 0
            [language] => *
            [fieldparams] => Joomla\Registry\Registry Object
                (
                    [data:protected] => stdClass Object
                        (
                            [multiple] => 1
                            [options] => stdClass Object
                                (
                                    [options0] => stdClass Object
                                        (
                                            [name] => General Personal Training
                                            [value] => General Personal Training
                                        )

                                    [options1] => stdClass Object
                                        (
                                            [name] => Conditioning
                                            [value] => Conditioning
                                        )

                                    [options2] => stdClass Object
                                        (
                                            [name] => Strength
                                            [value] => Strength
                                        )

                                    [options3] => stdClass Object
                                        (
                                            [name] => Boxing
                                            [value] => Boxing
                                        )

                                    [options4] => stdClass Object
                                        (
                                            [name] => Rugby
                                            [value] => Rugby
                                        )

                                    [options5] => stdClass Object
                                        (
                                            [name] => Football
                                            [value] => Football
                                        )

                                    [options6] => stdClass Object
                                        (
                                            [name] => High Intensity Training (HIT)
                                            [value] => High Intensity Training (HIT)
                                        )

                                    [options7] => stdClass Object
                                        (
                                            [name] => Long Distance Running
                                            [value] => Long Distance Running
                                        )

                                    [options8] => stdClass Object
                                        (
                                            [name] => Athletics
                                            [value] => Athletics
                                        )

                                )

                        )

                    [initialized:protected] => 1
                    [separator] => .
                )

            [params] => Joomla\Registry\Registry Object
                (
                    [data:protected] => stdClass Object
                        (
                            [render_class] => 
                            [class] => 
                            [showlabel] => 1
                            [show_on] => 
                            [display] => 0
                            [display_readonly] => 2
                        )

                    [initialized:protected] => 1
                    [separator] => .
                )

            [type] => list
            [default_value] => 
            [context] => com_contact.contact
            [group_id] => 0
            [label] => Specialisms
            [description] => 
            [required] => 0
            [language_title] => 
            [language_image] => 
            [editor] => 
            [access_level] => Public
            [author_name] => Eoin
            [group_title] => 
            [group_access] => 
            [group_state] => 
            [value] => General Personal Training, Conditioning, Strength, Boxing, Athletics
            [rawvalue] => Array
                (
                    [0] => General Personal Training
                    [1] => Conditioning
                    [2] => Strength
                    [3] => Boxing
                    [4] => Athletics
                )

        )

)

I can output the value by using

foreach ($item->jcfields as $field) {
   echo $field->value;
}

But how can I output the options 1 by 1. Then I can style individual items

2 Answers 2

2

I haven't tested this so I could be wrong. But if I rightly remember, a protected object cannot be retrieved by $field->fieldparams->options. Instead, you'll need to use:

$options = $field->fieldparams->get('options');

foreach ($options as $option)
{
    echo $option->name;
    echo $option->value;
}

Again, I haven't tested this and have a feeling it might return a PHP error, but thought I'd give it a shot.

3
  • That worked, cheers.
    – Eoin
    Commented Jun 19, 2018 at 9:43
  • 1
    Oof, that was lucky :)
    – Lodder
    Commented Jun 19, 2018 at 9:51
  • Well I wish I had that luck I'd ask a lot less questions on here
    – Eoin
    Commented Jun 19, 2018 at 9:58
2

The field options are a registry object so if you've already got the field you can grab all the options with something like:

$field_options = $field->fieldparams->toArray();

print_r that and you'll see the structure it provides which you can then just foreach through.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.