freejellyfish Posted July 22, 2008 Share Posted July 22, 2008 Hi, I have got a RSS-Parser stored in the file rssparser.php and i have got a page that takes rss-links from a database and [tries to] display the rss-"content". My problem now is this: i have included the rssparser.php fiel at the top of the page and it does display the first (of two ) rss-blocks correctly but the second looks like this: Fatal error: Cannot redeclare startelement() (previously declared in /data/apache/users/kilu.de/heinbld/www/project/rssparser.php:11) in /data/apache/users/kilu.de/heinbld/www/project/rssparser.php on line 11 Do I have to create a new rssparser.php file each time i want to display a rss file?? Thanks a lot for your help! greeez -jellyfish Quote Link to comment https://forums.phpfreaks.com/topic/116044-solved-cannot-redeclare-function-xy/ Share on other sites More sharing options...
jonsjava Posted July 22, 2008 Share Posted July 22, 2008 gotta see the code. it's all dependent on how you wrote it. sorry. Quote Link to comment https://forums.phpfreaks.com/topic/116044-solved-cannot-redeclare-function-xy/#findComment-596811 Share on other sites More sharing options...
freejellyfish Posted July 22, 2008 Author Share Posted July 22, 2008 ok, here comes the code ############# <?php include ("rssparser.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ........<body> <?php $host = "localhost"; $user = "*****"; $password = "****"; $db_name = "*******"; $mysql_id = mysql_connect($localhost, $user, $password) or die("Could NOT establish a connection to the database'".$host."'! Reason: ".mysql_error()); mysql_select_db($db_name, $mysql_id) or die("Could NOT select the database ".$db_name."! Reason: ".mysql_error()); $selected = mysql_query('SELECT * from plugins', $mysql_id) or die(mysql_error()); while($result = mysql_fetch_array($selected, MYSQL_ASSOC)) { rss_div_create($result['link']); echo "<br />"; } ?> </body> </html> ##################### and this is whats inside the rssparser.php http://www.sitepoint.com/examples/phpxml/sitepointcover.php.txt i have just modified the output and made a function out of it... Quote Link to comment https://forums.phpfreaks.com/topic/116044-solved-cannot-redeclare-function-xy/#findComment-596842 Share on other sites More sharing options...
.josh Posted July 22, 2008 Share Posted July 22, 2008 The problem is that when you include rssparser.php the startelement function is declared but the error is saying that it's already been declared but I'm not seeing that in the code you provided so...is that code you provided being included somewhere else that perhaps also includes rssparser.php? My only clue to that is that you have another custom function rss_div_create that's not in your rssparser.php... Quote Link to comment https://forums.phpfreaks.com/topic/116044-solved-cannot-redeclare-function-xy/#findComment-596852 Share on other sites More sharing options...
freejellyfish Posted July 22, 2008 Author Share Posted July 22, 2008 actually this code while($result = mysql_fetch_array($selected, MYSQL_ASSOC)) { rss_div_create($result['link']); echo " "; } will call the function rss_div_create 2 times (2 links in the database) it cant be because of that? rssparser.php : <?php function rss_parser ($rss_file) { $insideitem = false; $tag = ""; $title = ""; $description = ""; $link = ""; function startElement($parser, $name, $attrs) { [color=red]<======= line11[/color] 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") { $crap = array("ä" => "ä", "ü" => "ü", "ö" => "ö", "Ä" => "Ä", "Ü" => "Ü", "Ö" => "Ö", "…" => "...", "ß" => "ß", "„" => "„", "“" => "”" ); echo '<a href="'.$link.'">'; echo strtr($title, $crap); echo '</a>'; //<br />'; echo '<br />'; $title = ""; $description = ""; $link = ""; $insideitem = false; } } function characterData($parser, $data) { global $insideitem, $tag, $title, $description, $link; global $sTitle, $sLink, $sDescription; 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($rss_file,"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); } function rss_div_create ($rsssrc) { echo '<div id="box" class="box">'; echo rss_parser ($rsssrc); <==== [color=red]does this line cause the problem?!?![/color] echo '</div>'; } Quote Link to comment https://forums.phpfreaks.com/topic/116044-solved-cannot-redeclare-function-xy/#findComment-596875 Share on other sites More sharing options...
wildteen88 Posted July 22, 2008 Share Posted July 22, 2008 Hold on read the error message carefully. That error message is coming form rssparser.php on line 11 not from the file you include rssparser.php Fatal error: Cannot redeclare startelement() (previously declared in /data/apache/users/kilu.de/heinbld/www/project/rssparser.php:11) in /data/apache/users/kilu.de/heinbld/www/project/rssparser.php on line 11 Quote Link to comment https://forums.phpfreaks.com/topic/116044-solved-cannot-redeclare-function-xy/#findComment-596878 Share on other sites More sharing options...
trq Posted July 22, 2008 Share Posted July 22, 2008 actually this code while($result = mysql_fetch_array($selected, MYSQL_ASSOC)) { rss_div_create($result['link']); echo " "; } will call the function rss_div_create 2 times (2 links in the database) it cant be because of that? No, you can call a function as many times as you want, you just can't (re)define a function that has already been defined. Quote Link to comment https://forums.phpfreaks.com/topic/116044-solved-cannot-redeclare-function-xy/#findComment-596881 Share on other sites More sharing options...
wildteen88 Posted July 22, 2008 Share Posted July 22, 2008 actually this code while($result = mysql_fetch_array($selected, MYSQL_ASSOC)) { rss_div_create($result['link']); echo " "; } will call the function rss_div_create 2 times (2 links in the database) it cant be because of that? rssparser.php : <?php function rss_parser ($rss_file) { $insideitem = false; $tag = ""; $title = ""; $description = ""; $link = ""; function startElement($parser, $name, $attrs) { [color=red]<======= line11[/color] 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") { $crap = array("ä" => "ä", "ü" => "ü", "ö" => "ö", "Ä" => "Ä", "Ü" => "Ü", "Ö" => "Ö", "…" => "...", "ß" => "ß", "„" => "„", "“" => "”" ); echo '<a href="'.$link.'">'; echo strtr($title, $crap); echo '</a>'; //<br />'; echo '<br />'; $title = ""; $description = ""; $link = ""; $insideitem = false; } } function characterData($parser, $data) { global $insideitem, $tag, $title, $description, $link; global $sTitle, $sLink, $sDescription; 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($rss_file,"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); } function rss_div_create ($rsssrc) { echo '<div id="box" class="box">'; echo rss_parser ($rsssrc); <==== [color=red]does this line cause the problem?!?![/color] echo '</div>'; } I have found the problem, you have defined the startElement function within the rss_parser function. Everytime you call the rss_parser function you are redefining the startElement function. Move the statElement function outside of the rss_parser function. Quote Link to comment https://forums.phpfreaks.com/topic/116044-solved-cannot-redeclare-function-xy/#findComment-596890 Share on other sites More sharing options...
freejellyfish Posted July 22, 2008 Author Share Posted July 22, 2008 G.R.E.A.T T.H.A.N.K.S thanks a lot everybody i would have never thought about thant! Quote Link to comment https://forums.phpfreaks.com/topic/116044-solved-cannot-redeclare-function-xy/#findComment-596898 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.