Ajax RSS Reader to display feeds automatically

I want to display feeds automatically on my website as it loads without relying on users to select from the options. Thoroughly going through the site, I found the same question on AJAX RSS Reader, but not answered, I reasoned the question wasn’t detailed enough to allow members to give answers. The original source code is here https://www.w3schools.com/php/php_ajax_rss_reader.asp and I am also posting the codes to be detailed as possible HTML:



<form>
<select onchange="showRSS(this.value)">
<option value="">Select an RSS-feed:</option>
<option value="Google">Google News</option>
<option value="ZDN">ZDNet News</option>
</select>
</form>
<br>
<div id="rssOutput">RSS-feed will be listed here...</div>
</body>
</html>

PHP:
<?php
//get the q parameter from URL
$q=$_GET[“q”];

//find out which feed was selected
if($q=="Google") {
  $xml=("http://news.google.com/news?ned=us&topic=h&output=rss");
} elseif($q=="ZDN") {
  $xml=("https://www.zdnet.com/news/rss.xml");
}

$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);

//get elements from "<channel>"
$channel=$xmlDoc->getElementsByTagName('channel')->item(0);
$channel_title = $channel->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$channel_link = $channel->getElementsByTagName('link')
->item(0)->childNodes->item(0)->nodeValue;
$channel_desc = $channel->getElementsByTagName('description')
->item(0)->childNodes->item(0)->nodeValue;

//output elements from "<channel>"
echo("<p><a href='" . $channel_link
  . "'>" . $channel_title . "</a>");
echo("<br>");
echo($channel_desc . "</p>");

//get and output "<item>" elements
$x=$xmlDoc->getElementsByTagName('item');
for ($i=0; $i<=2; $i++) {
  $item_title=$x->item($i)->getElementsByTagName('title')
  ->item(0)->childNodes->item(0)->nodeValue;
  $item_link=$x->item($i)->getElementsByTagName('link')
  ->item(0)->childNodes->item(0)->nodeValue;
  $item_desc=$x->item($i)->getElementsByTagName('description')
  ->item(0)->childNodes->item(0)->nodeValue;
  echo ("<p><a href='" . $item_link
  . "'>" . $item_title . "</a>");
  echo ("<br>");
  echo ($item_desc . "</p>");
}
?>

The news feeds in this code should display automatically as the website loads.

So if you want it to load before a user selects anything, you need to choose the default channel. The if block just needs an else that will be used before any other selection is made.

I have tried to fix it by choosing “Google” as the default channel in the code, added an else to one if block at a time and run the code at each time I changed the if block I added the else to but didn’t get the solution. How do I get it done?

if(!isset($_GET['q'])) {
  $xml=("http://news.google.com/news?ned=us&topic=h&output=rss");
}
Sponsor our Newsletter | Privacy Policy | Terms of Service