Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Help with this php code for RSS feed
Old 10-20-2007, 02:23 PM Help with this php code for RSS feed
Skilled Talker

Posts: 79
Name: ian
Trades: 0
I want to display an RSS feed on my website and used this code:

Quote:
<?php
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
$tag = $name;
} elseif ($name == "ITEM") {
$insideitem = true;
}
}
function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link;
if ($name == "ITEM") {
printf("<dt><b><a href='%s'>%s</a></b></dt>",
trim($link),htmlspecialchars(trim($title)));
printf("<dt>%s</dt><br><br>",htmlspecialchars(trim($description))) ;
$title = "";
$description = "";
$link = "";
$insideitem = false;
}
}
function characterData($parser, $data) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
switch ($tag) {
case "TITLE":
$title .= $data;
break;
case "DESCRIPTION":
$description .= $data;
break;
case "LINK":
$link .= $data;
break;
}
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://michaelthompson.org/news/goo-world.xml","r")
or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);
?>
It works fine, however further down the page I want to set up a seperate RSS feed and tried to use the same code again and got this error:
Cannot redeclare startelement() (previously declared in /home/collegec/public_html/testmain.php:121) in /home/collegec/public_html/testmain.php on line 193

The page URL is http://www.collegecolosseum.com/testmain.php

Any idea why this is happening?

Also, is there any way for me to shorten the "description" part of the feed to a certain number of characters?

Thanks.
collegec is offline
Reply With Quote
View Public Profile Visit collegec's homepage!
 
 
Register now for full access!
Old 10-20-2007, 03:04 PM Re: Help with this php code for RSS feed
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
Whats on line 193 of your code?

Also, in future use the [php][/php] when showing PHP code, it makes life much more simple
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 10-21-2007, 12:47 AM Re: Help with this php code for RSS feed
Skilled Talker

Posts: 79
Name: ian
Trades: 0
Quote:
Originally Posted by rogem002 View Post
Whats on line 193 of your code?

Also, in future use the when showing PHP code, it makes life much more simple
Sorry, didn't know about the code.

line 193 just has a }

line 121 has function startElement($parser, $name, $attrs) {
collegec is offline
Reply With Quote
View Public Profile Visit collegec's homepage!
 
Old 10-21-2007, 02:42 AM Re: Help with this php code for RSS feed
rion's Avatar
Experienced Talker

Posts: 31
Name: Rion
Location: Portland, Oregon
Trades: 0
you don't need to rewrite the functions

just call them again down the page...once you declare the function once on the page (or any inculded file) you don't need to write the function again
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
rion is offline
Reply With Quote
View Public Profile Visit rion's homepage!
 
Old 10-21-2007, 03:21 AM Re: Help with this php code for RSS feed
Skilled Talker

Posts: 79
Name: ian
Trades: 0
Alright so for the first RSS feed I would just use the code above as is. However, what portion should I use for the second feed?
collegec is offline
Reply With Quote
View Public Profile Visit collegec's homepage!
 
Old 10-21-2007, 05:05 AM Re: Help with this php code for RSS feed
Foundationflash's Avatar
Ultra Talker

Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
Trades: 0
I would guess, having never tried this myself:
PHP Code:
$xml_parser xml_parser_create();
xml_set_element_handler($xml_parser"startElement""endElement");
xml_set_character_data_handler($xml_parser"characterData");
$fp fopen("A different feed","r")
or die(
"Error reading RSS data.");
while (
$data fread($fp4096))
xml_parse($xml_parser$datafeof($fp))
or die(
sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)), 
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser); 
Though you can probably keep some of the bits from the first section going for optimal speed.
__________________
Foundation Flash tutorials :
Please login or register to view this content. Registration is FREE


New Dreamed Up Web Design:
Please login or register to view this content. Registration is FREE

Last edited by Foundationflash; 10-21-2007 at 05:06 AM..
Foundationflash is offline
Reply With Quote
View Public Profile Visit Foundationflash's homepage!
 
Old 10-21-2007, 01:02 PM Re: Help with this php code for RSS feed
Skilled Talker

Posts: 79
Name: ian
Trades: 0
Hey thats great it worked!

I did have to add <?php at the beginning and ?> at the end.

One more question. Is there anyway to specify the feed description to a certain length? say 70 characters?
collegec is offline
Reply With Quote
View Public Profile Visit collegec's homepage!
 
Old 10-23-2007, 07:55 AM Re: Help with this php code for RSS feed
Foundationflash's Avatar
Ultra Talker

Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
Trades: 0
Duh! You didn't actually say where you were going to put it.

As for the second question, I would take the line:

PHP Code:
printf("<dt>%s</dt><br><br>",htmlspecialchars(trim($description)))  ; 
and alter it to (though I haven't tried this myself):

PHP Code:
printf("<dt>%s</dt><br><br>",htmlspecialchars(trim(substr($description,0,70))))  ; 
I would also use <br />s which validate instead of <br>s

Hope that works!

p.s. If either of this post or my last one were indeed useful, use the "Like my post?" button the the left of them. Much appreciated.
__________________
Foundation Flash tutorials :
Please login or register to view this content. Registration is FREE


New Dreamed Up Web Design:
Please login or register to view this content. Registration is FREE
Foundationflash is offline
Reply With Quote
View Public Profile Visit Foundationflash's homepage!
 
Old 10-23-2007, 10:58 AM Re: Help with this php code for RSS feed
Experienced Talker

Posts: 34
Name: DesignersForHire
Location: India
Trades: 0
Why you are not using RSS feed creater which can be downloaded free?
__________________
Joseph

Please login or register to view this content. Registration is FREE
michael_1980 is offline
Reply With Quote
View Public Profile Visit michael_1980's homepage!
 
Old 10-23-2007, 11:57 AM Re: Help with this php code for RSS feed
Foundationflash's Avatar
Ultra Talker

Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
Trades: 0
Quote:
Why you are not using RSS feed creater which can be downloaded free?
I assume you mean creator. Also, many people would prefer to actually learn PHP and not just download stuff (especially when there's a BUY button). Plus this is just about displaying feeds not creating them.
__________________
Foundation Flash tutorials :
Please login or register to view this content. Registration is FREE


New Dreamed Up Web Design:
Please login or register to view this content. Registration is FREE
Foundationflash is offline
Reply With Quote
View Public Profile Visit Foundationflash's homepage!
 
Old 10-23-2007, 12:46 PM Re: Help with this php code for RSS feed
Skilled Talker

Posts: 79
Name: ian
Trades: 0
Quote:
Originally Posted by Foundationflash View Post

p.s. If either of this post or my last one were indeed useful, use the "Like my post?" button the the left of them. Much appreciated.
Thanks for the help. Your code worked flawlessly.

talkupation added!
collegec is offline
Reply With Quote
View Public Profile Visit collegec's homepage!
 
Old 10-26-2007, 03:11 AM Re: Help with this php code for RSS feed
Skilled Talker

Posts: 79
Name: ian
Trades: 0
One last thing (promise). How would I go about adding a bullet to each listed item?

Foundationflash - I sent you a PM, hope you don't mind.
collegec is offline
Reply With Quote
View Public Profile Visit collegec's homepage!
 
Old 10-26-2007, 10:15 AM Re: Help with this php code for RSS feed
Foundationflash's Avatar
Ultra Talker

Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
Trades: 0
Of course not... As I say in my reply, you'll probably want to get rid of the htmlspecialchars() depending on where you want the bullets.
__________________
Foundation Flash tutorials :
Please login or register to view this content. Registration is FREE


New Dreamed Up Web Design:
Please login or register to view this content. Registration is FREE
Foundationflash is offline
Reply With Quote
View Public Profile Visit Foundationflash's homepage!
 
Old 10-26-2007, 11:12 AM Re: Help with this php code for RSS feed
Skilled Talker

Posts: 79
Name: ian
Trades: 0
Is this the proper line to be using?

PHP Code:
('<ul><li>','</li><li>','</li></ul>'); 
This is where I placed it.

PHP Code:
<?php
$insideitem 
false;
$tag "";
$title "";
$description "";
$link "";
function 
startElement($parser$name$attrs) {
global 
$insideitem$tag$title$description$link;
if (
$insideitem) {
$tag $name;
} elseif (
$name == "ITEM") {
$insideitem true;
}
}
function 
endElement($parser$name) {
global 
$insideitem$tag$title$description$link;
if (
$name == "ITEM") {
printf("<dt><b><a href='%s'>%s</a></b></dt>",
trim($link),htmlspecialchars(trim($title)));
printf("<dt>%s</dt><br><br>",htmlspecialchars(trim($description))) ;
$title "";
$description "";
$link "";
$insideitem false;
}
}
function 
characterData($parser$data) {
global 
$insideitem$tag$title$description$link;
if (
$insideitem) {
switch (
$tag) {
case 
"TITLE":
$title .= $data;
(
'<ul><li>','</li><li>','</li></ul>');
break;
case 
"DESCRIPTION":
$description .= $data;
break;
case 
"LINK":
$link .= $data;
break;
}
}
}
$xml_parser xml_parser_create();
xml_set_element_handler($xml_parser"startElement""endElement");
xml_set_character_data_handler($xml_parser"characterData");
$fp fopen("http://michaelthompson.org/news/goo-world.xml","r")
or die(
"Error reading RSS data.");
while (
$data fread($fp4096))
xml_parse($xml_parser$datafeof($fp))
or die(
sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)), 
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);
?>
collegec is offline
Reply With Quote
View Public Profile Visit collegec's homepage!
 
Old 10-28-2007, 05:30 AM Re: Help with this php code for RSS feed
Foundationflash's Avatar
Ultra Talker

Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
Trades: 0
Do you want all the titles to be bulleted?
__________________
Foundation Flash tutorials :
Please login or register to view this content. Registration is FREE


New Dreamed Up Web Design:
Please login or register to view this content. Registration is FREE
Foundationflash is offline
Reply With Quote
View Public Profile Visit Foundationflash's homepage!
 
Old 10-29-2007, 02:39 AM Re: Help with this php code for RSS feed
Skilled Talker

Posts: 79
Name: ian
Trades: 0
Quote:
Originally Posted by Foundationflash View Post
Do you want all the titles to be bulleted?
Yes, just the titles.
collegec is offline
Reply With Quote
View Public Profile Visit collegec's homepage!
 
Old 10-29-2007, 08:53 AM Re: Help with this php code for RSS feed
Foundationflash's Avatar
Ultra Talker

Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
Trades: 0
Hmm... This might work - I'll try it out later...

PHP Code:
<?php
$currentlyinli 
false;
$currentlyindesc false
$insideitem 
false;
$tag "";
$title "";
$description "";
$link "";
function 
startElement($parser$name$attrs) {
global 
$insideitem$tag$title$description$link;
if (
$insideitem) {
$tag $name;
} elseif (
$name == "ITEM") {
$insideitem true;
}
}
function 
endElement($parser$name) {
global 
$insideitem$tag$title$description$link;
if (
$name == "ITEM") {
printf("<dt><b><a href='%s'>%s</a></b></dt>",
trim($link),htmlspecialchars(trim($title)));
printf("<dt>%s</dt><br><br>",htmlspecialchars(trim($description))) ;
$title "";
$description "";
$link "";
$insideitem false;
}
}
function 
characterData($parser$data) {
global 
$insideitem$tag$title$description$link;
if (
$insideitem) {
switch (
$tag) {
case 
"TITLE":
if(
$currentlyinli == false){
$currentlyinli true;
$title .= "<ul><li>";
}
$title .= $data;
break;
case 
"DESCRIPTION":
if(
$currentlyinli == true){
$description .= "</li></ul>";
$currentlyindesc true;
$currentlyinli false;
}
$description .= $data;
break;
case 
"LINK":
$currentlyindesc false;
$link .= $data;
break;
}
}
}
$xml_parser xml_parser_create();
xml_set_element_handler($xml_parser"startElement""endElement");
xml_set_character_data_handler($xml_parser"characterData");
$fp fopen("http://michaelthompson.org/news/goo-world.xml","r")
or die(
"Error reading RSS data.");
while (
$data fread($fp4096))
xml_parse($xml_parser$datafeof($fp))
or die(
sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)), 
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);
?>
Not going to be perfect I admit, but might work.
__________________
Foundation Flash tutorials :
Please login or register to view this content. Registration is FREE


New Dreamed Up Web Design:
Please login or register to view this content. Registration is FREE
Foundationflash is offline
Reply With Quote
View Public Profile Visit Foundationflash's homepage!
 
Old 10-29-2007, 12:40 PM Re: Help with this php code for RSS feed
Skilled Talker

Posts: 79
Name: ian
Trades: 0
FF - I gave that code a try, but recieved this error:

Parse error: syntax error, unexpected T_VARIABLE in /home/collegec/public_html/testmain2.php on line 127

line 127 contains this line:

$insideitem = false;
collegec is offline
Reply With Quote
View Public Profile Visit collegec's homepage!
 
Old 10-29-2007, 01:48 PM Re: Help with this php code for RSS feed
Foundationflash's Avatar
Ultra Talker

Posts: 410
Name: Harry Burt
Location: Colchester, Essex, England
Trades: 0
Sorry, I missed a ; After further study and a bit of thought
PHP Code:
<?php
$currentlyinli 
false;
$currentlyindesc false;
$insideitem false;
$tag "";
$title "";
$description "";
$link "";
function 
startElement($parser$name$attrs) {
global 
$insideitem$tag$title$description$link;
if (
$insideitem) {
$tag $name;
} elseif (
$name == "ITEM") {
$insideitem true;
}
}
function 
endElement($parser$name) {
global 
$insideitem$tag$title$description$link;
if (
$name == "ITEM") {
printf("<dt><b><a href='%s'>%s</a></b></dt>",
trim($link),trim($title));
printf("<dt>%s</dt><br><br>",trim($description)) ;
$title "";
$description "";
$link "";
$insideitem false;
}
}
function 
characterData($parser$data) {
global 
$insideitem$tag$title$description$link$currentlyinli$currentlyindesc;
if (
$insideitem) {
switch (
$tag) {
case 
"TITLE":
if(
$currentlyinli == false){
$currentlyinli true;
$title .= "<ul><li>";
}
$title .= $data;
break;
case 
"DESCRIPTION":
if(
$currentlyinli == true){
$description .= "</li></ul>";
$currentlyindesc true;
$currentlyinli false;
}
$description .= $data;
break;
case 
"LINK":
$currentlyindesc false;
$link .= $data;
break;
}
}
}
$xml_parser xml_parser_create();
xml_set_element_handler($xml_parser"startElement""endElement");
xml_set_character_data_handler($xml_parser"characterData");
$fp fopen("http://news.google.com/?ned=us&topic=s&output=rss","r")
or die(
"Error reading RSS data.");
while (
$data fread($fp4096))
xml_parse($xml_parser$datafeof($fp))
or die(
sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)), 
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);
?>
seems to work. I have change the XML file to one that works as an example.
__________________
Foundation Flash tutorials :
Please login or register to view this content. Registration is FREE


New Dreamed Up Web Design:
Please login or register to view this content. Registration is FREE
Foundationflash is offline
Reply With Quote
View Public Profile Visit Foundationflash's homepage!
 
Old 10-29-2007, 04:20 PM Re: Help with this php code for RSS feed
Skilled Talker

Posts: 79
Name: ian
Trades: 0
Thanks it worked!
collegec is offline
Reply With Quote
View Public Profile Visit collegec's homepage!
 
Reply     « Reply to Help with this php code for RSS feed

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.57790 seconds with 12 queries