简体   繁体   中英

PHP and simplexml

i use php and simplexml, my probleme is "to add a attribute with for" my code is:

for($i=1;$i<=$compteur;$i++)
    {
        $jeu->situation->question->choix->addChild('rep',"\n".$rep[$i]."\n");
        $jeu->situation->question->choix->rep->addAttribute('val',$i);

    }

Result:

                             <choix>
                <rep val="1">

                </rep>
                <rep>

                </rep>
            </choix>

he add just in a first !!!

addChild() returns the node that was added to the DOM, so do your attribute work on that

$child = $jeu->situation->question->choix->addChild('rep',"\n".$rep[$i]."\n");
$child->addAttribute('val', $i);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM