0

I have been working on this for a while now and I am at a complete block, I am not sure if I am going about it the right way.

What I have is an RSS feed displayed on my website. I have created a search box where I require this to partially match against title/description of the XML.

  <?php
    $xml = <<<XML
    XML;
    $sxe = simplexml_load_file('http://www.music-news.com/rss/news.asp');
    $searchKeyword = $_POST["SearchBox"];
    $area = $sxe->xpath("//item[contains(title,'".$searchKeyword."')]");
    print_r($area);
   ?> 

This code when called up from the submit button of the search box will bring to a page that just displays the follow

Array ( [0] => SimpleXMLElement Object ( [title] => SimpleXMLElement Object ( ) [description] => SimpleXMLElement Object ( ) [link] => SimpleXMLElement Object ( ) [pubDate] => Mon, 27 Apr 2015 10:01:00 GMT [guid] => SimpleXMLElement Object ( ) [author] => SimpleXMLElement Object ( ) ) )

It is bringing back the correct records within the RSS feed (can be determined from the publish date). However I am trying to get it so that the search results would show as the original RSS feed shows on the site. The code for the initial RSS feed is below

<?php

$rss = simplexml_load_file('http://www.music-news.com/rss/news.asp');

foreach ($rss->channel->item as $item) 
{
echo "<h2>" .$item->title . "</h2>";
echo "<p>" . $item->pubDate . "</p>";
echo "<p>" . $item->description .  "</p>";
echo '<p><a href="'. $item->link .'">' .ReadMore. "</a><p>";
} 
?>

Any assistance would be greatly appreciated

Thanks in advance

1 Answers1

0

$area should be an array of zero to many simplexml-elements, so use:

foreach($area as $item) {
    echo "<h2>" .$item->title . "</h2>";
    echo "<p>" . $item->pubDate . "</p>";
    // and so on
} 

see a working example: https://eval.in/319904

BTW, you should sanitize $searchKeyword to prevent any kind of harmful input, see Cleaning/sanitizing xpath attributes, specifically the top rated answer has a final paragraph on sanitizing with PHP.

Community
  • 1
  • 1
michi
  • 6,565
  • 4
  • 33
  • 56