Okay, I've gotten further on my problem, but I'm still stumped....
Given this script to output a simple related links file:
<?php
$open_tags = array(
'REL_LINKS' => '<REL_LINKS>',
'LINK' => '<LINK>',
'TITLE' => '<TITLE>',
'URL' => '<URL>',
'DESCRIPTION' => '<DESCRIPTION>',
'SUBMITTER' => '<SUBMITTER>',
'EMAIL' => '<EMAIL>');
$close_tags = array(
'REL_LINKS' => '</REL_LINKS>',
'LINK' => '</LINK>',
'TITLE' => '</TITLE>',
'URL' => '</URL>',
'DESCRIPTION' => '</DESCRIPTION>',
'SUBMITTER' => '</SUBMITTER>',
'EMAIL' => '</EMAIL>');
function startElement($parser, $name, $attrs='') {
global $open_tags, $temp, $current_tag;
$current_tag = $name;
if ($format = $open_tags[$name]) {
switch ($name) {
case 'REL_LINKS':
echo "<dl>";
break;
case 'LINK':
echo "\n";
break;
default:
break;
}
}
}
function endElement($parser, $name, $attrs='') {
global $close_tags, $temp, $current_tag, $content;
if ($format = $close_tags[$name]) {
switch ($name) {
case 'REL_LINKS':
echo "</dl>";
break;
case 'LINK':
return_page($temp);
echo "\n";
//$temp = '';
return $temp;
break;
default:
break;
}
}
}
function characterData($parser, $data) {
global $current_tag, $temp;
switch ($current_tag) {
case 'TITLE':
$temp['title'] = $data;
$current_tag = '';
break;
case 'URL':
$temp['url'] = $data;
$current_tag = '';
break;
case 'DESCRIPTION':
$temp['description'] = $data;
$current_tag = '';
break;
case 'SUBMITTER':
$temp['submitter'] = $data;
$current_tag = '';
break;
case 'EMAIL':
$temp['email'] = $data;
$current_tag = '';
break;
default:
break;
}
}
function return_page() {
global $temp, $content;
$content = '<dt><a href="'.$temp['url'].'">'.$temp['title'].'</a> -';
$content .= '<em>submitted by <a href="mailto:'.$temp['email'].'">';
$content .= $temp['submitter'].'</a></em></dt><dd>'.$temp['description'].'</dd>';
echo $content;
}
$xml_file = '../test/links.xml';
$type = 'UTF-8';
$xml_parser = xml_parser_create($type);
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
xml_set_element_handler($xml_parser, 'startElement', 'endElement');
xml_set_character_data_handler($xml_parser, 'characterData');
if (!($fp = fopen($xml_file,'r'))) {
die("Could not open $xml_file for parsing!\n");
}
ob_start();
while ($data = fread($fp, 4096)) {
if (!($data = utf8_encode($data))) {
echo "ERROR \n";
}
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf( "XML error: %s at line %d\n\n",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
$mine = ob_get_contents();
$length = ob_get_length();
ob_end_clean();
return $mine;
xml_parser_free($xml_parser);
?>
I can call it and it outputs my xml file fine from this test script:
<?php
include ("index2.php");
echo $mine;
?>
However, where I need it to work is inside a switch/case statement, ie:
/* links page */
case 'links':
initBox('Links');
include('index2.php');
echo $mine;
readBox('', 'related links', CONTENT,$mine, 'boxleft');
break;
When I run my main script and go to my links section, my readBox() function reports that $mine is empty.
Is there something I'm missing here? This has stumped me for a while now, and if I can get this to work I can make major progress on my script. I admit I'm still relatively new to PHP, so please be gentle.
Pulling me hair out,
Ed