Using RSS Feeds and Mod_rewrite to add Classified Listings on your Website - Four functions
(Page 3 of 4 )
There are also four functions used in rssads.php.
The first function is called "listCities." This function runs through our cities array and displays a page of cities, grouped by country. This is used on the main page of our site:
<?
function listCities($cities){
echo "<table width=100%>";
foreach($cities as $country=>$list){
echo "<tr><td colspan=3>".$country."</td></tr>";
$i = 0;
foreach($list as $k=>$city){
$i++;
if($i == 1)echo "<tr>";
echo "<td width=33%><a href=\"ads/".urlencode($city)."/\">".$city."</a></td>";
if($i == 3){
echo "</tr>";
$i = 0;
}
}
}
echo "</table>";
}
?>
Our next function is called "listCats." It works like listCities, and creates a list of categories:
<?
function listCats($city,$categories){
echo "<table width=100%>";
foreach($categories as $parent=>$list){
echo "<tr><td colspan=3 align=center>".
$parent."</td></tr>";
$i = 0;
foreach($list as $cat=>$name){
$i++;
if($i == 1)echo "<tr>";
echo "<td width=33%><a href=\"ads/".urlencode($city)."/".urlencode($cat)."/\">".$name."</a></td>";
if($i == 3){
echo "</tr>";
$i = 0;
}
}
}
echo "</table>";
}
?>
Our third function is called "display." It processes the city and category and then calls our final function:
<?
function display($city,$cat,$categories){
$ocity = $city;
$city = explode(",",$city);
$city = $city[0];
$city = str_replace(" ","",$city);
$url = "http://".$city.".craigslist.org/".
$cat."/index.rss";
foreach($categories as $parent=>$list){
if( isset($list[$cat]) ){
$tname = $parent." - ".$list[$cat];
break;
}
}
$ptitle = "craigslist | Displaying ".$tname." jobs in ".$ocity;
?>
<a href="<?=str_replace("index.rss","",$url)?>"><?=$ptitle?></a><br/>
<?
echo readRss($url);
}
?>
Our final function is the backbone of this site, "readRSS." It is a universal RSS reader function that serves quite well for fetching RSS feeds from craiglist.org for us. Basically, it fetches the RSS feed that the script tells it to fetch and returns with information related to the feeds. In our case, it returns ads:
<?
function readRss($url){
?>
<table width='100%'>
<?
$row = @implode( '',@file($url) );
if( preg_match_all("'<item[^>]*>.*?</item(s?)>'si", $row, $rowitem ) ) {
$item = $rowitem[0];
$res = "";
$total = count($item) - 1;
for( $i = 0; $i < $total; $i++ ) {
if (trim($item[$i])!='' and !ereg('<items',$item[$i])) {
eregi('<title>(.*)</title>', $item[$i], $title );
eregi('<link>(.*)</link>', $item[$i], $url );
eregi('<description>(.*)</description>', $item[$i], $desc );
echo "<tr><td>";
echo "<a href='".str_replace("&","&",$url[1]).
"'>".$title[1]."</a><span><br>".
"<div style='padding-left:10px;'>".
htmlspecialchars( trim($desc[1]) ).
"</div><br>";
echo "</td></tr>";
}
}
}
?>
</table>
<?
}
?>
Next: Putting it all together >>
More Link Trading Articles
More By Roger Stringer