Search code examples
phphtmlsimplexmlhref

Make one value a link of another


I am Currently passing holidays.xml to a table in 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>";
?>

I would like echo "<td>{$holiday->link}</td>"; to be a link of echo "<td>{$holiday->title}</td>";, so link would be the href and title the title. Is there a simple way to do this?


Solution

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