I have aworking copy of this here: [url]http://www.childsinc.com/rss
however, i have changed that page to use GET instead of POST since bokeh told me i was wrong to use POSt this way. another difference in this script is that i moved the DB work into a function but i can’t seems to figure out why the array returned has only the last element in the DB.
[code=php]
<?php
session_start();
include(“db_config.php”);
echo “<p><a href='”.$_SERVER[‘PHP_SELF’].”‘>START OVER</a></p>n”;
$rep_str = array(“/cnn/i”, “/fox/i”, “/msnbc/i”);
$with_str = array(“CNN”, “FOX”, “MSNBC”);
echo “<p>Please select a source for your RSS Feed:</p>n”;
echo “<form action='”.$_SERVER[‘PHP_SELF’].”‘ method=’get’>n<select name=’source’>n”;
foreach(db_query(“SHOW TABLES FROM $db_name”) as $key => $value)
{
echo “<option value='”.$value.”‘>”;
$new_value = preg_replace($rep_str, $with_str, ucwords(str_replace(“_”, ” “, $value)));
echo $new_value.”</option>n”;
}
echo “</select>n<input type=’submit’ value=’Get Source!’ />n</form>n”;
if(isset($_GET[‘source’]))
{
$new_value = preg_replace($rep_str, $with_str, ucwords(str_replace(“_”, ” “, $_GET[‘source’])));
echo “<p>Please select the type of feed offered by <strong>”.$new_value.”</strong></p>”;
echo “<form action='”.$_SERVER[‘PHP_SELF’].”‘ method=’get’>n”;
echo “<input type=’hidden’ name=’source’ value='”.$_GET[‘source’].”‘ />n”;
echo “<select name=’type’>n”;
foreach(db_query(“SELECT `type` FROM `{$_GET[‘source’]}`”) as $key => $value)
{
echo ‘<option value=”‘.$value.'”>’.$value.'</option>’.”n”;
}
echo “</select>n<input type=’submit’ value=’Get it!’ />n</form>n”;
}
if(isset($_GET[‘source’]) && isset($_GET[‘type’]))
{
foreach(db_query(“SELECT `url` FROM `”.$_GET[‘source’].”` WHERE `type` = “.'”‘.$_GET[‘type’].'”‘) as $key => $value)
{
$rssstring = file_get_contents($value);
preg_match_all(“#<title>(.*?)</title>#s”,$rssstring,$titel);
preg_match_all(“#<image>(.*?)</image>#s”,$rssstring,$images);
$img_num=count($images[0]);
preg_match_all(“#<item>(.*?)</item>#s”,$rssstring,$items);
$n=count($items[0]);
for($i=0;$i<$n;$i++)
{
$rsstemp= $items[0][$i];
preg_match_all(“#<title>(.*?)</title>#s”,$rsstemp,$titles);
$title[$i]= $titles[1][0];
preg_match_all(“#<pubDate>(.*?)</pubDate>#s”,$rsstemp,$dates);
$date[$i]= $dates[1][0];
preg_match_all(“#<link>(.*?)</link>#s”,$rsstemp,$links);
$link[$i]= $links[1][0];
}
for($i=0;$i<$img_num;$i++)
{
$rssimg= $images[0][$i];
preg_match_all(“#<url>(.*?)</url>#s”,$rssimg,$urls);
$img_url[$i]= $urls[1][0];
preg_match_all(“#<width>(.*?)</width>#s”,$rssimg,$widths);
$img_width[$i]= $widths[1][0];
preg_match_all(“#<height>(.*?)</height>#s”,$rssimg,$heights);
$img_height[$i]= $heights[1][0];
preg_match_all(“#<description>(.*?)</description>#s”,$rssimg,$descrip);
$img_alt[$i]= $descrip[1][0];
preg_match_all(“#<link>(.*?)</link>#s”,$rssimg,$img_links);
$img_link[$i]= $img_links[1][0];
}
echo “n<p><a href=’$img_link[0]’ target=’blank’><img src=’$img_url[0]’ width=’$img_width[0]’ height=’$img_height[0]’ title=’$img_alt[0]’ border=’0′ /></a></p>nn”;
echo “<h4>”.$titel[1][0].”</h4>n”;
echo “<table cellspacing=’5′>n<tr><th>Date</th><th>Time</th><th>Content</th></tr>n”;
for($i=0;$i<$n;$i++)
{
$timestamp=strtotime($date[$i]);
$cal=date(‘Y/m/d’, $timestamp);
$clock=date(‘H:i’, $timestamp);
if(!empty($title[$i])) echo “<tr>n<td>”.$cal.”</td><td>”.$clock.”</td>n<td><a href=”.$link[$i].”>”.$title[$i].”</a></td>n</tr>n”;
}
echo “</table>n”;
}
}
function db_query($sql) {
include(“db_config.php”);
$conn = mysql_connect($db_host, $db_user, $db_pass);
if (!$conn)
{
echo “Unable to connect to the database: ” . mysql_error();
exit;
}
if (!mysql_select_db(“$db_name”))
{
echo “Unable to select db_name: ” . mysql_error();
exit;
}
$result = mysql_query($sql);
if (!$result)
{
echo “Could not successfully run query ($sql) from DB: ” . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0)
{
echo “No rows found.”;
exit;
}
while($row = mysql_fetch_assoc($result))
{
foreach($row as $key => $value)
{
$results[$key] = $value;
}
}
mysql_free_result($result);
mysql_close($conn);
return $results;
}
?>