/    Sign up×
Community /Pin to ProfileBookmark

Running the same script twice on the same page???

I have an RSS reader script that I used to parse RSS feeds. On my homepage, I use it to parse one feed currently using an [B]include[/B] command, and the feed URL is a variable. However, I would like to parse two feeds on my homepage using the same script. How would I do this…?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@GarySMay 22.2006 — Hard to say without seeing more code, but it might be as simple as including again with the different feed URL as the variable.
Copy linkTweet thisAlerts:
@MindmanauthorMay 22.2006 — Here is the code on the page where the feed is displayed...

[code=php]$Feed = "http://tntclan.local/Forums/rss.php?fid=2&type=rss2.0&limit=5";[/code]

Basically it just includes the RSS reader using an include command, and the $Feed provides the actual feed URL. I just need to get the RSS reader to run through twice with two different URLs on the same page. I can post the source of the RSS reader script if you'd like.

How would I implement the "again" command properly in this case?
Copy linkTweet thisAlerts:
@GarySMay 22.2006 — Could you do something like:

[code=php]$Feed = "http://tntclan.local/Forums/rss.php?fid=2&type=rss2.0&limit=5";
$Feed2 = "http://tntclan.local/Forums/rss.php?fid=20000&type=rss2.0&limit=5"; [/code]



(I've assumed the [B]fid[/B] is the thing that distinguishes the feeds?)
Copy linkTweet thisAlerts:
@chazzyMay 22.2006 — your best bet would be to make your feed parser into a function. then call feedParser(url1) and feedParser(url2)
Copy linkTweet thisAlerts:
@MindmanauthorMay 22.2006 — Probably, but here is the RSS Reader page, how can I get it to look for another $Feed variable and run twice?

[code=php]<?php
/* PHP RSS Reader v1.1
By Richard James Kendall
Bugs to [email protected]
Free to use, please acknowledge me

Place the URL of an RSS feed in the $Feed variable.

The $rss_channel array will be filled with data from the feed,
every RSS feed is different but by and large it should contain:

Array {
[TITLE] = feed title
[DESCRIPTION] = feed description
[LINK] = link to their website

[IMAGE] = Array {
[URL] = url of image
[DESCRIPTION] = alt text of image
}

[ITEMS] = Array {
[0] = Array {
[TITLE] = item title
[DESCRIPTION] = item description
[LINK] = a link to the story
}
.
.
.
}
}

By default it retrives the Reuters Oddly Enough RSS feed. The data is put into the array
structure so you can format the information as you see fit.

>>>PAGE VARIABLES
$Feed [URL] = URL of the RSS feed
$PrintTable [True/False] = Specify whether or not the feed needs to be enclosed in a table
$Description [Text] = Description underneath the page header, if needed

*/


set_time_limit(0);

$rss_channel = array();
$currently_writing = "";
$main = "";
$item_counter = 0;

function startElement($parser, $name, $attrs) {
global $rss_channel, $currently_writing, $main;
switch($name) {
case "RSS":
case "RDF:RDF":
case "ITEMS":
$currently_writing = "";
break;
case "CHANNEL":
$main = "CHANNEL";
break;
case "IMAGE":
$main = "IMAGE";
$rss_channel["IMAGE"] = array();
break;
case "ITEM":
$main = "ITEMS";
break;
default:
$currently_writing = $name;
break;
}
}

function endElement($parser, $name) {
global $rss_channel, $currently_writing, $item_counter;
$currently_writing = "";
if ($name == "ITEM") {
$item_counter++;
}
}

function characterData($parser, $data) {
global $rss_channel, $currently_writing, $main, $item_counter;
if ($currently_writing != "") {
switch($main) {
case "CHANNEL":
if (isset($rss_channel[$currently_writing])) {
$rss_channel[$currently_writing] .= $data;
} else {
$rss_channel[$currently_writing] = $data;
}
break;
case "IMAGE":
if (isset($rss_channel[$main][$currently_writing])) {
$rss_channel[$main][$currently_writing] .= $data;
} else {
$rss_channel[$main][$currently_writing] = $data;
}
break;
case "ITEMS":
if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
$rss_channel[$main][$item_counter][$currently_writing] .= $data;
} else {
#print ("rss_channel[$main][$item_counter][$currently_writing] = $data<br>");
$rss_channel[$main][$item_counter][$currently_writing] = $data;
}
break;
}
}
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($Feed, "r"))) {
die("Could not open XML input!");
}

while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);





//HTML output

/*Unused image display coding
//RSS Image
if (isset($rss_channel["IMAGE"])) {
print ("<a href="" . $rss_channel["LINK"] . "" target="_blank"><img border="0" src="" . $rss_channel["IMAGE"]["URL"] . "" align="left" style="float:left;" alt="" . $rss_channel["IMAGE"]["TITLE"] . ""></a><br>n"); }*/

//RSS Title
print ("<font class="header2">" . ucwords($rss_channel["TITLE"]) . "</font><br>n");

//RSS Description
print ("<strong>" . $rss_channel["DESCRIPTION"] . "</strong><br><br>nn");

//Print the local description, if needed
if ($Description != "") {
print ("$Description"); }

//RSS Items counter
if (isset($rss_channel["ITEMS"])) {
if (count($rss_channel["ITEMS"]) > 0) {
for($i = 0;$i < count($rss_channel["ITEMS"]);$i++) {

//Print a table, if needed
if ($PrintTable == "True") {
print ("n<table width="498" border="3" bordercolor="#006666" cellpadding="3" cellspacing="0"><tr><td width="100%">"); }

//Print the RSS Titles and Pubdates
print ("<font class="header5"><a href="" . $rss_channel["ITEMS"][$i]["LINK"] . "" target="_blank">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</a></font><br>n" . "<font class="header4">" . $rss_channel["ITEMS"][$i]["PUBDATE"] . "</font>n");

//Print the RSS Descriptions, if needed
if ($PrintDescription == "True") {
print ("<br>n");
print (html_entity_decode($rss_channel["ITEMS"][$i]["DESCRIPTION"]));

}

//Close the table, if needed
if ($PrintTable == "False") {
print ("<br><br>nn");

} else {

print ("</td></tr></table><br>n"); }
}
} else {
print ("<strong>There are no articles in this feed.</strong>");
}
}


?>[/code]
Copy linkTweet thisAlerts:
@MindmanauthorMay 22.2006 — your best bet would be to make your feed parser into a function. then call feedParser(url1) and feedParser(url2)[/QUOTE]

How would I set this up?
×

Success!

Help @Mindman 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...