You are here:  » Selecting records from an RSS feed

Support Forum



Selecting records from an RSS feed

Submitted by Jan on Tue, 2006-04-11 15:37 in

I'm a little familiar with php and mysql but don't see how to get records from a RSS-feed. With MagicParser I can display a large list of car's, each car contains records as brand, type, year, collor etc, etc. The list contains abouth 400 car's and is too long :).
Now comes the trick, how can I get entry's (car's) out of the RSS containig the var's of a search-form or something.
In mysql and php it is something like this:
SELECT * FROM RSS WHERE $brand=[BRAND] and $type=[TYPE]
How can I display something like this out of the RSS ( or is it XML ) link?

This is a part of the php-script I use at this moment:

    echo "<td class=\"button\">".$item["BRAND"]."</td>\n";
    echo "<tr><td class=\"normal\">".$item["TYPE"]."</td></tr>\n";
    echo "<tr><td class=\"button\">".$item["ENGINE"]."</td></tr>\n";
    echo "<tr><td class=\"normal\">".$item["YEAR"]."</td></tr>\n";
    echo "<tr><td class=\"button\">".$item["KM"]."</td></tr>\n";

Submitted by support on Tue, 2006-04-11 16:03

Hi Jan,

To do this with Magic Parser, the trick is simply to compare the field you wish to test against the value you want to match on each record within your myRecordHandler function; and only process the record if it matches.

For example, the following code will read items, but will only display the item (where you can use exactly the code used in your example) where the BRAND is "FORD" and the TYPE is "FIESTA":

<?php
  
function myRecordHandler($item)
  {
    if ((
$item["BRAND"]=="FORD") && ($item["TYPE"] == "FIESTA"))
    {
      
// display $item here
    
}
  }
  
MagicParser_parse("cars.rss","myRecordHandler","xml|RSS/CHANNEL/ITEM");
?>

Now, to extend that to only process records where, for example, BRAND matches the input to an HTML form field, submitted using the POST method with name "brand", you might do it like this:

<?php
  
function myRecordHandler($item)
  {
    if (
$item["BRAND"]==$_POST["brand"])
    {
      
// display $item here
    
}
  }
  
MagicParser_parse("cars.rss","myRecordHandler","xml|RSS/CHANNEL/ITEM");
?>

Remember that $_POST is a "superglobal", so you do not need to declare it as a global variable within your myRecordHandler() function.

Hope this helps!

Submitted by Jan on Tue, 2006-04-11 19:03

Hi David
This is great! It works ...of course:), you know what you tell.
My brains cannot exact figure out why but it feels like the possibilities of RSS are great. Is it equal with the combination of php and mysql?

Ok, next step if possible:
the select-an-option box in my search-form shows the Ford brand 40 times, as much as there are Ford-types (Fiesta, Siera, Scorpio,etc):

echo "<select name=\"brand\">\n";
  echo "<option value=\"____\" selected>-- Pleas choose --</option>\n";
  MagicParser_parse($url,"HaalGegevens2","xml|RECORDS/RECORD/");
  function HaalGegevens2($record)
  {
    echo "<option value='".$record["BRAND"]."'>".$record["BRAND"]."</option>\n";
  }
  echo "</select>";

Is there a way to show Ford only once in the drop-down box?
Something like SELECT DISTINCT in mysql?

Submitted by support on Tue, 2006-04-11 19:19

Easiest way is to hold a global array $brands, which you set to 1 each time a brand is created in the drop down menu. Then, you just check to see if an entry for the current brand has already been set and only add a new option if it is empty.

<?php
  
echo "<select name=\"brand\">\n";
  echo 
"<option value=\"____\" selected>-- Pleas choose --</option>\n";
  
MagicParser_parse($url,"HaalGegevens2","xml|RECORDS/RECORD/");
  function 
HaalGegevens2($record)
  {
    global 
$brands;
    if(!
$brands[$record["BRAND"]])
    {
      echo 
"<option value='".$record["BRAND"]."'>".$record["BRAND"]."</option>\n";
      
$brands[$record["BRAND"]] = 1;
    }
  }
  echo 
"</select>";
?>

It might be nice to make the option drop down sorted alphabetically. To do this, you would load the unqiue brands into an array without actually creating the HTML, and then sort the array, and then create the HTML:

<?php
  MagicParser_parse
($url,"HaalGegevens2","xml|RECORDS/RECORD/");
  function 
HaalGegevens2($record)
  {
    global 
$brands;
    
$brands[$record["BRAND"]] = 1;
  }
  
// sort the $brands array by key
  
ksort($brands);
  echo 
"<select name=\"brand\">\n";
  echo 
"<option value=\"____\" selected>-- Pleas choose --</option>\n";  
  foreach(
$brands as $brand => $value)
  {
    echo 
"<option value='".$brand."'>".$brand."</option>\n";
  }
  echo 
"</select>";
?>