使一个值成为另一个值的链接

时间:2012-03-09 09:52:54

标签: php html simplexml href

我目前正在将holidays.xml传递给logged_in.php中的表格。

holidays.xml

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
 <channel>
 <item>
      <title>Luxurious Jamaican holidays | 40% Discount On Accommodation - Book Now!</title> 
      <link>http://www.numyspace.co.uk/~cgel1/holidays/Jamaica.html</link> 
      <description>7 nights at The Golden Palm, Montego Bay travelling from Newcastle with Fly Jamaica</description> 
      <pubDate>Sun, 13 Feb 2011 11:58:17 GMT</pubDate> 
      <guid>http://www.numyspace.co.uk/~cgel1/holidays/Jamaica.html</guid> 
  </item>
 </channel>
</rss> 

logged_in.php

       <?php
// load the xml file into a simplexml instance variable
$holiday = simplexml_load_file('holidays.xml');

// draw a table and column headers
echo "<table border=\"0\">";
/* echo " <th>Title</td>
      <th>Link</td>
      <th>Description</td>
      <th>Published Date</th>";
*/
// iterate through the item nodes displaying the contents
foreach ($holiday->channel->item as $holiday) {
    echo "<tr>";
    echo "<td>{$holiday->title}</td>";
    echo "<td>{$holiday->link}</td>";
    echo "<td>{$holiday->description}</td>";
    echo "<td>{$holiday->pubDate}</td>";
    echo "</tr>\n";
}
echo "</table>";
?>

我希望echo "<td>{$holiday->link}</td>";成为echo "<td>{$holiday->title}</td>";的链接,因此链接将是标题的href和标题。有一种简单的方法可以做到这一点吗?

2 个答案:

答案 0 :(得分:2)

echo "<td><a href=\"{$holiday->link}\">{$holiday->title}</a></td>";

答案 1 :(得分:0)

您可以在td之前使用href添加锚点,如下所示:

foreach ($holiday->channel->item as $holiday) {
    echo "<tr>";
    echo "<a href='{$holiday->link}'><td>{$holiday->title}</td></a>";
    echo "<td>{$holiday->description}</td>";
    echo "<td>{$holiday->pubDate}</td>";
    echo "</tr>\n";
}

我对simpleXML并不熟悉,我听说过但从未使用过它,所以我不确定我所做的是否正确。