/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] RSS Feed page with problems

I have aworking copy of this here: [url]http://www.childsinc.com/rss[/url]
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;
}

?>
[/code]

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@sitehatcheryApr 05.2006 — Hmm... not familiar with your formatting:

"SELECT type FROM {$_GET['source']}"

have you tried: "SELECT type FROM " . $_GET['source'] . ""
Copy linkTweet thisAlerts:
@rch10007authorApr 05.2006 — your suggestion is the same thing as what i use.

the main problem is with the array being returned form the DB function. i can't seem to get but one entry formt eh DB, the last one??
Copy linkTweet thisAlerts:
@sitehatcheryApr 05.2006 — [code=php]
echo "<input type='hidden' name='source' value='".$_GET['source']."' />";

$result=mysql_result(mysql_query("SELECT type FROM ".$_GET['source'].""),0,0);

echo "<select name=type>";

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "<option name=$typerow[". $row['id']."] value='".$row['id'].">".$row['type']."</option>";

}

echo "</select>";

[/code]


Where id is the your primary key field.
Copy linkTweet thisAlerts:
@rch10007authorApr 05.2006 — sitehatchery has a new nickname = "Egg_Head";

lol, i like your site design!

ok, from your suggestion, i would have to redesign the db function.

the following code works just fine BUT it has alot of extra code, specifically the db connection and queries. i am trying to make the following code into a streamlined script adn eventually make a class out of it.

the biggest problem is using a function to return a query result as an array and then using the results from the function to manipulate the results. the following is what is being run at http://www.childsinc.com/rss

[code=php]
<?php session_start();
include("db_config.php");
if(isset($_POST['sources']))
{
echo "<p><a href='index.php'>START OVER</a></p>n";
$name_torep = array("/cnn/i", "/fox/i", "/msnbc/i");
$name_rep = array("CNN", "FOX", "MSNBC");
$new_value = preg_replace($name_torep, $name_rep, ucwords(str_replace("_", " ", $_POST['sources'])));
echo "<p>Please select the type of feed offered by <strong>".$new_value."</strong></p>";
$_SESSION['rss_source'] = $_POST['sources'];
$conn = mysql_connect($db_host, $db_user, $db_pass);
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
if (!mysql_select_db("$db_name")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$sql = "SELECT type
FROM {$_POST['sources']}";
$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, nothing to print so am exiting";
exit;
}
echo "<form action='".$_SERVER['PHP_SELF']."' method='post' name='type'>n<select name='types'>n";
while($row = mysql_fetch_assoc($result))
{
foreach($row as $key => $value)
{
echo '<option value="'.$value.'">'.$value.'</option>'."n";
}
}
mysql_free_result($result);
mysql_close($conn);
echo "</select>n<input type='submit' name='submit' value='Get it!' />n</form>n";
}

elseif(isset($_POST['types']))
{
$got_type = stripslashes($_POST['types']);
echo "<p><a href='index.php'>START OVER</a></p>n";
$conn = mysql_connect($db_host, $db_user, $db_pass);
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
if (!mysql_select_db("$db_name")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$sql = "SELECT url
FROM {$_SESSION['rss_source']}
WHERE type = ".'"'.$got_type.'"';
$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 "$sql - No rows found, nothing to print so am exiting";
exit;
}
while($row = mysql_fetch_assoc($result))
{
foreach($row 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";

}
}
mysql_free_result($result);
mysql_close($conn);
}

else
{
$name_torep = array("/cnn/i", "/fox/i", "/msnbc/i");
$name_rep = array("CNN", "FOX", "MSNBC");
echo "<p>Please select a source for your RSS Feed:</p>n";
$conn = mysql_connect($db_host, $db_user, $db_pass);
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
if (!mysql_select_db("$db_name")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$sql = "SHOW TABLES FROM $db_name";
$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, nothing to print so am exiting";
exit;
}
echo "<form action='".$_SERVER['PHP_SELF']."' method='post' name='source'>n<select name='sources'>n";
while($row = mysql_fetch_assoc($result))
{
foreach($row as $key => $value)
{
echo "<option value='".$value."'>";
$new_value = preg_replace($name_torep, $name_rep, ucwords(str_replace("_", " ", $value)));
echo $new_value."</option>n";
}
}
mysql_free_result($result);
mysql_close($conn);
echo "</select>n<input type='submit' name='submit' value='Get Source!' />n</form>n";
}

?>
[/code]


I was told that i should be using GET instead of POST to perform this code, so the code in the previous posts is the result of trying to streamline and fix this code into the GET method as well as trying to put all teh db work into 1 function.

does this make sense?
Copy linkTweet thisAlerts:
@rch10007authorApr 05.2006 — So i solved one of my issues - i placed my hidden field inside the select statement instead of right after form, duh!

BUT, now i have a problem with the GET and the url.

i am getting the source and type together in the url but the type looks like this inside the url: (What%27s+News+-+US) instead of what it's supposed to look like: (What's News - US).

the GET method is changing it (i assume)

how do i get it from the url back in the form was sent so that i can use it in the db search?
Copy linkTweet thisAlerts:
@sitehatcheryApr 05.2006 — don't worry about that. "%27" is the ANSII representation for "-". The browser actually did that and not your $_GET code. It doesn't make any difference at all, the browser completely understands it.
Copy linkTweet thisAlerts:
@rch10007authorApr 05.2006 — yea, come to find out i was missing another statement that kept me from even searching - it wasn't the url at all
Copy linkTweet thisAlerts:
@sitehatcheryApr 05.2006 — excuse me, ASCII
×

Success!

Help @rch10007 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...