XML解析到HTML表 - 显示所有类别

时间:2015-07-09 11:37:42

标签: php html xml parsing

我有这个xml文档:

<offer>
<image width="450" height="300">http://dsc.invia.cz/img/affil-450x300/2014/7/10/d0/4756588.jpg</image>
<photos>
  <photo>http://dsc.invia.cz/img/affil-450x300/2015/3/5/d4/5643777.jpg</photo>
  <photo>http://dsc.invia.cz/img/affil-450x300/2015/3/5/d4/5643778.jpg</photo>
  <photo>http://dsc.invia.cz/img/affil-450x300/2015/3/5/d4/5643779.jpg</photo>
  <photo>http://dsc.invia.cz/img/affil-450x300/2015/3/5/d4/5643780.jpg</photo>
  <photo>http://dsc.invia.cz/img/affil-450x300/2015/3/5/d4/5643781.jpg</photo>
  <photo>http://dsc.invia.cz/img/affil-450x300/2015/3/5/d4/5643782.jpg</photo>
  <photo>http://dsc.invia.cz/img/affil-450x300/2015/3/5/d4/5643783.jpg</photo>
  <photo>http://dsc.invia.cz/img/affil-450x300/2015/3/5/d4/5643784.jpg</photo>
  <photo>http://dsc.invia.cz/img/affil-450x300/2015/3/5/d4/5643785.jpg</photo>
  <photo>http://dsc.invia.cz/img/affil-450x300/2015/3/5/d4/5643786.jpg</photo>
  <photo>http://dsc.invia.cz/img/affil-450x300/2015/3/5/d4/5643787.jpg</photo>
  <photo>http://dsc.invia.cz/img/affil-450x300/2015/3/5/d4/5643788.jpg</photo>
</photos>
<destination>
  <country>Egypt</country>
  <locality>Hurghada</locality>
</destination>
<hotel>Three Corners Sunny Beach</hotel>
<term>
  <from>2015-07-29</from>
  <to>2015-08-05</to>
  <length>8</length>
</term>
<price currency="CZK">15290</price>
<tax currency="CZK">0</tax>
<totalprice currency="CZK">15290</totalprice>
<discount>0</discount>
<food>All Inclusive</food>
<transportation>Letecky</transportation>
<airports>
  <airport>Praha</airport>
</airports>
<url>http://hotel.invia.cz/egypt/hurghada/three-corners-sunny-beach-resort/tour-925060/?id=64762198&amp;aid=533153</url>
<tourtypes>
  <type>Pobytové</type>
</tourtypes>
<hotelinfo>
  <id>46667</id>
  <stars>4</stars>
  <rating>7.8</rating>
  <ratingcount>62</ratingcount>
  <coords>
    <lat>27.318666465248917</lat>
    <lng>33.70912313461304</lng>
  </coords>
  <url>http://hotel.invia.cz/egypt/hurghada/three-corners-sunny-beach-resort/?aid=533153</url>
</hotelinfo>
<termtype>catalogue</termtype>

我可以解析图片,目的地,酒店,期限,期限,期限等,但我无法解析机场和照片,因为项目数量不同。

我的PHP代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title>XML feed</title>
  </head>
  <body>



<?php
$xml = new SimpleXMLElement('test.xml', 0, TRUE);
?>
<table width='100%'>
  <thead>
    <tr>
      <th>Snímka</th>
      <th>Stát</th>
      <th>Destinace</th>
      <th>Hotel</th>
      <th>Od</th>
      <th>Do</th>
      <th>Počet dní</th>
      <th>Cena</th>
      <th>Sleva</th>
      <th>Jídlo</th>
      <th>Doprava</th>
    </tr>
  </thead>
  <tbody>

<?php foreach ($xml->offer as $ponuka) :?>
    <tr>
      <td><img src='<?php echo $ponuka->image; ?>' width='100'></td>
      <td><?php echo $ponuka->destination->country; ?></td>
      <td><?php echo $ponuka->destination->locality; ?></td>
      <td><a href='<?php echo $ponuka->hotelinfo->url; ?>'><?php echo $ponuka->hotel; ?></a>, <?php echo $ponuka->hotelinfo->stars; ?>*</td>
      <td><?php echo $ponuka->term->from; ?></td>
      <td><?php echo $ponuka->term->to; ?></td>
      <td><?php echo $ponuka->term->length; ?></td>    
      <td><?php echo $ponuka->totalprice; ?>,- Kč</td>
      <td><?php echo $ponuka->discount; ?>%</td>
      <td><?php echo $ponuka->food; ?></td>
      <td><?php echo $ponuka->transportation; ?></td>
      <td><?php foreach ($xml->offer->airports as $letiska) :?><?php echo $letiska->airport; ?><?php endforeach; ?></td>
    </tr>
<?php endforeach; ?>
  </tbody>
</table>    
  </body>
</html>

如何为照片和机场编制foreach程序? 谢谢大家的帮助。

1 个答案:

答案 0 :(得分:0)

我改变了你的代码并使用了一些函数。 看看吧。

<?php

$xml = simplexml_load_file('offer.xml');

function renderOffer($xml){
    $output = '';
    foreach ($xml->offer as $offer)
        $output .= '<tr>
      <td><img src='. $offer->image .' width="100"></td>
      <td>'. $offer->destination->country .'</td>
      <td>'. $offer->destination->locality .'</td>
      <td><a href="'. $offer->hotelinfo->url .'">'. $offer->hotel .'</a>, '. $offer->hotelinfo->stars .'*</td>
      <td>'. $offer->term->from .'</td>
      <td>'. $offer->term->to .'</td>
      <td>'. $offer->term->length .'</td>    
      <td>'. $offer->totalprice .',- Kč</td>
      <td>'. $offer->discount .'%</td>
      <td>'. $offer->food .'</td>
      <td>'. $offer->transportation .'</td>
      <td>'. renderAirports($offer->airports) .'</td>
    </tr>';
    return $output;
}

function renderAirports($airports){
    $output = '';
    foreach ($airports as $letiska)
        $output .= $letiska->airport;
    return $output;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title>XML feed</title>
  </head>
  <body>

<table width='100%'>
  <thead>
    <tr>
      <th>Snímka</th>
      <th>Stát</th>
      <th>Destinace</th>
      <th>Hotel</th>
      <th>Od</th>
      <th>Do</th>
      <th>Počet dní</th>
      <th>Cena</th>
      <th>Sleva</th>
      <th>Jídlo</th>
      <th>Doprava</th>
    </tr>
  </thead>
  <tbody>
<?php print renderOffer($xml); ?>
  </tbody>
</table>    
  </body>
</html>

在tbody块

中显示
<tr>
      <td><img src=http://dsc.invia.cz/img/affil-450x300/2014/7/10/d0/4756588.jpg width="100"></td>
      <td>Egypt</td>
      <td>Hurghada</td>
      <td><a href="http://hotel.invia.cz/egypt/hurghada/three-corners-sunny-beach-resort/?aid=533153">Three Corners Sunny Beach</a>, 4*</td>
      <td>2015-07-29</td>
      <td>2015-08-05</td>
      <td>8</td>
      <td>15290,- Kč</td>
      <td>0%</td>
      <td>All Inclusive</td>
      <td>Letecky</td>
      <td>Praha</td>
    </tr>