PHP & foreach nasıl kullanılır

0 Cevap php

Orada PHP ve XML bilen herkes? Sonra bir göz atın!

This is my PHP code:

<? $xml = simplexml_load_file("movies.xml");
foreach ($xml->movie as $movie){ ?>

<h2><? echo $movie->title ?></h2>
<p>Year: <? echo $movie->year ?></p>
<p>Categori: <? echo $movie->regions->region->categories->categorie ?></p>
<p>Country: <? echo $movie->countries->country ?></p>

<? } ?>

Bu MYS XML dosyasıdır:

<?xml version="1.0" encoding="UTF-8"?>
<movies>
 <movie>
  <title>A movie name</title>
  <year>2010</year>
     <regions>
   <region>
    <categories>
      <categorie id="3">Animation</categorie>
      <categorie id="5">Comedy</categorie>
      <categorie id="9">Family</categorie>
     </categories>
    <countries>
     <country id="123">USA</country>
    </countries>
   </region>
  </regions>
 </movie>
 <movie>
  <title>Little Fockers</title>
  <year>2010</year>
     <regions>
   <region>
    <categories>
     <categorie id="5">Comedy</categorie>
             </categories>
        <countries>
         <country id="123">USA</country>
    </countries>
   </region>
  </regions>
 </movie>
</movies>

Yukarıdaki kod sonucudur:

<h2>A movie name</h2>
<p>Year: 2010</p>
<p>Category: Animation</p>
<p>Country: USA</p>

<h2>Little Fockers</h2>
<p>Year: 2010</p>
<p>Category: Comedy</p>
<p>Country: USA</p>

Ben (ilk film kategorisine bakın) bu gibi olmak istiyorum:

<h2>A movie name</h2>
<p>Year: 2010</p>
<p>Category: Animation, Comedy, Family</p>
<p>Country: USA</p>

<h2>Little Fockers</h2>
<p>Year: 2010</p>
<p>Category: Comedy</p>
<p>Country: USA</p>

Not: Ayrıca ben kelimeler arasında virgül almak için merak ediyorum, fakat son sözcük bir virgül olmadan ...

0 Cevap