vladys.bo
vladys.bo

Reputation: 740

Creating dynamic list element php


I need to generate li item with data- attribute while array.length < 0
Got a problem while concatenation. In result it's wrong li I need: <li data-color='red, green, blue' data-size='1, 2, 3'>
But result is: <li data- color='red, green, blue' size='1, 2, 3'>

Code:

$filters_li = '<li class="textile-item"';

                foreach($filters_labels as $label){
                    foreach($filters_fields as $field){
                        $filters_li .= 'data-'.$label.'="'. get_labels_as_tags($field, 1) . '"';
                    }
                }

                $filters_li .= '>';

Where is problem, I can't understand

Upvotes: 1

Views: 34

Answers (2)

Poiz
Poiz

Reputation: 7617

You may try trimming the $label to see your results like so :

    filters_li = '<li class="textile-item"';

                foreach($filters_labels as $label){
                    foreach($filters_fields as $field){
                        $filters_li .= 'data-'. trim($label) .'="'. get_labels_as_tags($field, 1) . '"';
                    }
                }

                $filters_li .= '>'; 

Maybe that does the trick for you

Upvotes: 1

Majid Abbasi
Majid Abbasi

Reputation: 1531

you can use from trim function, this function remove spaces by default

$filters_li .= 'data-'.trim($label).'="'. get_labels_as_tags($field, 1) . '"';

Upvotes: 1

Related Questions