Jump to content

Recommended Posts

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 :P ) 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

Link to comment
https://forums.phpfreaks.com/topic/116044-solved-cannot-redeclare-function-xy/
Share on other sites

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...

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...

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>';

}

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

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.