Page 1 of 1

variable scope problem

Posted: Thu Nov 02, 2006 1:13 am
by itsmani1
I am parsing an xml file and after the parsing is done i am saving it in a variable called $tt but at the end when i print it don't work. I think problem is with scope of variable. when i try echo it works but if is put it in var and then print at the end it simple don't , is there any way?

Code: Select all

<table width="60%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="40%" bgcolor="#09276D"><span class="style1">Event</span></td>
    <td width="20%" bgcolor="#09276D" class="style1">Date</td>
    <td width="20%" bgcolor="#09276D" class="style1">Venue</td>
    <td width="20%" bgcolor="#09276D">&nbsp;</td>
  </tr>

<?PHP
$file = "tst1.xml";
function startElement($parser, $name, $attrs)
{
   if($name == "ROW")
	{
//		echo "<tr><td bgcolor=\"$bg\">".$attrs['THISNAME']."</td><td bgcolor=\"$bg\">".$attrs['THISID']."</td><td bgcolor=\"$bg\">".$attrs['ST']."</td><td bgcolor=\"$bg\" align=\"center\"><img src=\"images/ViewTicket.gif\" width=\"93\" height=\"16\" /></td><tr>";
		$tt = "<tr><td bgcolor=\"$bg\">".$attrs['THISNAME']."</td><td bgcolor=\"$bg\">".$attrs['THISID']."</td><td bgcolor=\"$bg\">".$attrs['ST']."</td><td bgcolor=\"$bg\" align=\"center\"><img src=\"images/ViewTicket.gif\" width=\"93\" height=\"16\" /></td><tr>";
	}
}
function endElement($parser, $name)
{
}
function characterData($parser, $name)
{
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen($file,"r");
$data = fread($fp,4096);
xml_parse($xml_parser, $data);
xml_parser_free($xml_parser);

?>
<?PHP echo $tt; ?>
</table>
<style type="text/css">
<!--
.style1 {
	color: #FFFFFF;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 12px;
	font-weight: bold;
}
-->
</style>

Posted: Thu Nov 02, 2006 2:18 am
by aaronhall
You're exactly right, $tt only exists in the context of the function. Can't you simply print it from the function like you were saying? If not, check this out.

Posted: Thu Nov 02, 2006 3:05 am
by itsmani1
yes tried it with global and working now