1

What is the way to load more than one XML (different sources) and show a mixed content?

My Code is now loading only one XML now

<?php 

            $mobile = simplexml_load_file('mobile.xml');
            $electronics = simplexml_load_file('electronics.xml');

                foreach($mobile->product as $product) 
                { 
                    echo '<div class="col-md-3">'."\n";
                    echo '<h4 class="img-responsive"><a href="'.$product->url.'">'.$product->name.'</a></h4>'."\n";
                    echo '<p><a href="'.$product->url.'"><img src="'.$product->image->large.'" class="img-responsive" /></a></p>'."\n";
                    echo '<p>'.$product->Price.'</p>'."\n";
                    echo '</div><!--end painting_record-->'."\n";

                } 
                foreach($electronics->product as $product) 
                { 
                    echo '<div class="col-md-3">'."\n";
                    echo '<h4 class="img-responsive"><a href="'.$product->url.'">'.$product->name.'</a></h4>'."\n";
                    echo '<p><a href="'.$product->url.'"><img src="'.$product->image->large.'" class="img-responsive" /></a></p>'."\n";
                    echo '<p>'.$product->Price.'</p>'."\n";
                    echo '</div><!--end painting_record-->'."\n";

                }
?>

Thanks

  • You should provide XML example files for that and also perhaps which output you get (perhaps simplified) and which output you are looking for instead. To mix two `Simplexml` elements for one `foreach` please see as well here: [Combine two XML PHP](http://stackoverflow.com/q/10619315/367456) - It has an answer that shows how it is done. – hakre Oct 13 '13 at 15:57

1 Answers1

0

Mix them how? It looks like your code would show them all in one list with mobile on the top and electronics at the bottom. I guess you could do something like (paracoding here)...

foreach($mobile->product as $product) { array_push($newList,$product); }
foreach($electronics->product as $product) { array_push($newList,$product); }

Then do a foreach on $newList, but sorting them however you want.

Dan Goodspeed
  • 3,484
  • 4
  • 26
  • 35