Jump to content

[SOLVED] Array and Loop Issue


monkeytooth

Recommended Posts

Im attempting to get RSS feeds from a couple site, monster being one of them..

 

So far I can get the rss/xml and break it down into an array. However from there I am stumping myself. I'm thinking I have an issue with one of the loops I am not catching. I don't know.. Overall, it outputs doubles triples quadruplicates of each item in the feed, almost randomly it seems at best.. but not really so randomly.

 

It puts out items like:

 

item #1, item #1, item #2, item #1, item #2, item #3, item #1, item #2, item #3, item #4 and so on.

 

As I am typing this I am realizing the loop is.. feeding back into itself before it runs again. So maybe this is an array problem? I don't know.. Anyone able to take a quick look at it see if they can notice whats off about this that im not noticing yet..

 

 $feedURL = 'http://rss.jobsearch.monster.com/rssquery.ashx?WT.mc_n=RSS2005_jsr&fn=47&cy=US&baseurl=jobsearch.monster.com';
$doc = new DOMDocument();
$doc->load($feedURL);
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
  $itemRSS = array ( 
   'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
   'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
   'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
   'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
   );
  array_push($arrFeeds, $itemRSS);
  //print_r($arrFeeds);
  foreach($arrFeeds as $arrItem) 
  {
   $hexitz = dechex(rand(0,255)) . dechex(rand(0,255)) . dechex(rand(0,255));
   echo "<font style=\"color:#" . hexitz . ";font-family:Verdana;font-size:8pt;\">";
   echo $arrItem['title'];
   echo "<br />";
   echo $arrItem['desc'];
   echo $arrItem['date'];
   echo "</font>";
   echo "<br /><br />";
  }
}

Link to comment
https://forums.phpfreaks.com/topic/175085-solved-array-and-loop-issue/
Share on other sites

Oy, figured out my own problem.. Forgot to remember its a bad idea to put a loop within a loop, makes for a mess.. a big one.. So a little tweaking and replacement of the loops, then adding an additional clause to make sure the arrays are unique. The final bit for those interested is:

 

 $countABC = "0";
$feedURL = 'http://rss.jobsearch.monster.com/rssquery.ashx?WT.mc_n=RSS2005_jsr&fn=47&cy=US&baseurl=jobsearch.monster.com';
$doc = new DOMDocument();
$doc->load($feedURL);
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
  $itemRSS = array ( 
   'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
   'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
   'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
   'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
   );
  array_push($arrFeeds, $itemRSS);
  //print_r($arrFeeds);
}
array_unique($arrFeeds);
//http://jobview.monster.com/getjob.aspx?JobID=83520515
   foreach($arrFeeds as $arrItem){
   $countABC = $countABC+1;
   $hexitz = dechex(rand(0,255)) . dechex(rand(0,255)) . dechex(rand(0,255));
   echo "<font style=\"color:#" . $hexitz . ";font-family:Verdana;font-size:8pt;\">" . $countABC . ".)";
   echo "<a href=\"" . $arrItem['link'] . "\" target=\"_blank\">" . $arrItem['title'] . "</a>";
   echo "<br />";
   echo $arrItem['desc'];
   echo $arrItem['date'];
   echo "</font>";
   echo "<br /><br />";
  }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.