开发者

PHP SimpleXML XPath - displaying data has problem

(Moved from original question.)

I have a radio station site on localhost, and the pages are a series of include and requires - they work well.

However, on one page, a schedule one, I'm displaying within a DIV some data extracted from a SimpleXML document.

This is the PHP document's coding:

  开发者_JAVA百科      <div class="itemheading"><h1><span>Programme Guide</span></h1></div>
        <div class="itembody">
          <div id="progdays">
                        <ul>
<li><a href="sunsch.php" title="Sunday">Sunday</a></li>
<li class="currentday"><a href="monsch.php">Monday</a></li>
<li><a href="tuesch.php" title="Tuesday">Tuesday</a></li>
<li><a href="wedsch.php" title="Wednesday">Wednesday</a></li>
<li><a href="thursch.php" title="Thursday">Thursday</a></li>
<li><a href="frisch.php" title="Friday">Friday</a></li>
<li><a href="satsch.php" title="Saturday">Saturday</a></li>                                             </ul>
          </div>
<?php
require("monsch.php");
?>
                                                            </div>

Here is the content of monsch.php:

        <?php

// set name of XML file
// normally this would come through GET
// it's hard-wired here for simplicity
$file = "monsched.xml";

// load file
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");
?> 

                            <dl class="standard">
                <dt><a href="<?php echo $xml->link; ?>" title="<?php echo $xml->show; ?>"><?php echo $xml->showtime; ?> - <?php echo $xml->show; ?></a></dt>
                <dd class="itemimg"><a href="<?php echo $xml->link; ?>" title="<?php echo $xml->show; ?>"><img src="<?php echo $xml->image; ?>" width="100" height="75" alt="<?php echo $xml->show; ?>" title="<?php echo $xml->show; ?>" /></a></dd>
                <dd class="itemdesc">
<?php echo $xml->showinfo; ?>
                </dd>
                <dd class="itemlink">
                  <a href="<?php echo $xml->link; ?>" title="<?php echo $xml->moreinfo; ?>"><span></span><?php echo $xml->moreinfo; ?></a>
                </dd>
              </dl>

It works well correctly using the echo() function of PHP, but how would I get the XML file to display more than one record from it.

Here's the XML content of monsched.xml:

<?xml version="1.0"?>

presenter.php Johnny Doe johnny.jpg 00:00 John Doe through the night More about the show

I followed the tutorial at http://devzone.zend.com/article/651 (the Baz Luhrmann example at the end was what I was trying to emulate). and everything worked OK, except one problem.... how do I get the PHP file to, using SimpleXML, read from multiple records within the XML file.

If I have a lot of shows in it, how would I get it to extract all of them from the XML file like the format above.

These are the shows with descriptions and images within the XML: This is my XML formatting:

<?xml version="1.0"?>
<link>presenter1.php</link>
<show>John Doe</show>
<image>overnight.jpg</image>
<showtime>01:00</showtime>
<showinfo>John Doe plays the hits
</showinfo>
<link>presenter2.php</link>
<show>Breakfast Show</show>
<image>headphones.jpg</image>
<showtime>06:00</showtime>
<showinfo>More music breakfast
</showinfo>

All help is appreciated with this, I'm fairly new to SimpleXML and XPath, but trying to get the Baz Luhrmann example to work for multiple entries is tricky - anyone able to figure it out?

The tutorial was otherwise helpful, though, but didn't answer all my questions.


The problem in your case is taht you need diferent XML structure:

<?xml version="1.0"?>
<podcasts>
  <podcast>
    <link>presenter1.php</link>
    <show>John Doe</show>
    <image>overnight.jpg</image>
    <showtime>01:00</showtime>
    <showinfo>John Doe plays the hits
    </showinfo>
  </podcast>
  <podcast>
    <link>presenter2.php</link>
    <show>Breakfast Show</show>
    <image>headphones.jpg</image>
    <showtime>06:00</showtime>
    <showinfo>More music breakfast
    </showinfo>
  </podcast>
</podcasts>

After that use the following PHP code:

$xml = simplexml_load_file($file)
foreach($xml->podcast as $row) {
  echo $row->link;
  ?>
  <dl class="standard">
            <dt><a href="<?php echo $xml->link; ?>" title="<?php echo $xml->show; ?>">
            <?php echo      $xml->showtime; ?> - <?php echo $xml->show; ?></a></dt>
            <dd class="itemimg"><a href="<?php echo $xml->link; ?>" title="<?php echo $xml->show; ?>"><img src="<?php echo $xml->image; ?>" width="100" height="75" alt="<?php echo $xml->show; ?>" title="<?php echo $xml->show; ?>" /></a></dd>
            <dd class="itemdesc">
               <?php echo $xml->showinfo; ?>
            </dd>
            <dd class="itemlink">
              <a href="<?php echo $xml->link; ?>" title="<?php echo $xml->moreinfo; ?>"><span></span><?php echo $xml->moreinfo; ?></a>
            </dd>
          </dl>

  <?php 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜