web-dev-qa-db-fra.com

Comment puis-je sortir chaque élément d'un champ personnalisé de liste multiple

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
                )

        )

)

Je peux sortir la valeur en utilisant

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

Mais comment puis-je sortir le options 1 par 1. Ensuite, je peux styliser des éléments individuels

3
Eoin

Je n'ai pas testé cela, donc je peux me tromper. Mais si je me souviens bien, un objet protégé ne peut pas être récupéré par $field->fieldparams->options. Au lieu de cela, vous devrez utiliser:

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

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

Encore une fois, je n’ai pas testé cela et j’ai l’impression que cela pourrait renvoyer une erreur PHP, mais je pensais pouvoir tenter le coup.

2
Lodder

Les options de champ sont un objet de registre, donc si vous avez déjà le champ, vous pouvez saisir toutes les options avec quelque chose comme:

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

print_r cela et vous verrez la structure qu'il fournit que vous pouvez ensuite parcourir.

2
jamesgarrett