0

I am using php simplexml and foreach to loop a xml file. the code just like

$xml = simplexml_load_file(myfile.xml);
foreach($xml->contents as $list){
     $name= $list->name;
         $price=$list->price;
         $prob=$list->Price;

    echo $name, $price, $prob;
}

If I have 100 items, how can I insert a class for every 5 items?

the output like

<div id="showup"> 
$name, $price, $prob (1)
$name, $price, $prob (2)
$name, $price, $prob (3)
$name, $price, $prob (4)
$name, $price, $prob (5)
</div>
<div id="showup"> 
$name, $price, $prob (6)
$name, $price, $prob (7)
$name, $price, $prob (8)
$name, $price, $prob (9)
$name, $price, $prob (10)
</div>
...............

Do I have to use while instead of foreach? the advantage for foreach is that, I dont need to count how many items in the xml file. or I can use both

hope someone could help, thanks :-)

1 Answer 1

1

try:

<?php $counter = 0; ?>
<div id="showup">
<?php foreach($xml->contents as $list) {

    $name= $list->name;
    $price=$list->price;
    $prob=$list->Price;

   echo $name, $price, $prob;
   $counter++;
   if( $counter % 5 == 0) { 
?>
</div><div id='showup'> 
<?php } } ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.