gmerriment
June 23rd, 2006, 01:48 PM
I don't understand. I tell my PHP code to include another piece of code and when the new code comes up, it exits out of PHP for reasons beyond my knowledge. Can somebody please tell me if there's something wrong with my code that I'm missing???
<?php
/*
* XML FILE POSTING SYSTEM
* This code's primary purpose is retrieving data from a
* form and posting it to an XML document. The form in
* question will analyze the XML to determine what pages
* it can modify and what data those pages have. Therefore
* this code must be completely dynamic in its scope.
* By analyzing the XML file and determining its contents,
* The number of pages and information on each can be
* easily determined and the form fields determined on the
* spot.
*/
//--((( Begin XML Parsing Goodness (STEP ONE) )))--\\
$xml_file = "xmlSource.xml";
$xml_pagename_key = "*SITEINFO*PAGE*PAGENAME";
$xml_maintable_key = "*SITEINFO*PAGE*MAINTABLE";
$xml_subtable_key = "*SITEINFO*PAGE*SUBTABLE";
$xml_infotable_key = "*SITEINFO*PAGE*INFOTABLE";
$xml_pastortable_key = "*SITEINFO*PAGE*PASTORTABLE";
$pageArray = array();
$counter = 0;
class PageDef {
var $pageName, $mainTable, $subTable, $infoTable, $pastorTable;
}
function startTag($parser, $data) {
global $current_tag;
$current_tag .= "*$data";
}
function endTag($parser, $data) {
global $current_tag;
$tag_key = strrpos($current_tag, '*');
$current_tag = substr($current_tag, 0, $tag_key);
}
function contents($parser, $data) {
global $current_tag, $xml_pagename_key, $xml_maintable_key, $xml_subtable_key, $xml_infotable_key, $xml_pastortable_key, $counter, $pageArray;
switch($current_tag) {
case $xml_pagename_key:
$pageArray[$counter] = new PageDef();
// The code stops here, at the ">" symbol
$pageArray[$counter]->pageName = $data;
break;
case $xml_maintable_key:
$pageArray[$counter]->mainTable = $data;
break;
case $xml_subtable_key:
$pageArray[$counter]->subTable = $data;
break;
case $xml_infotable_key:
$pageArray[$counter]->infoTable = $data;
break;
case $xml_pastortable_key:
$pageArray[$counter]->pastorTable = $data;
$counter++;
break;
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen($xml_file, "r") or die("Could not open file");
$data = fread($fp, filesize($xml_file)) or die("Could not read file");
if(!(xml_parse($xml_parser, $data, feof($fp)))){
echo "$counter<br>Error on line " . xml_get_current_line_number($xml_parser);
}
# We don't close/free the xml file because we're going to write it in a second.
//--((( End XML Parsing Goodness )))--\\
//--((( Begin textual data acquisition and editing (STEP TWO) )))--\\
$selectedPage = $_POST['selectedpage'];
$postData = array(
$_POST['maintabledata'],
$_POST['subtabledata'],
$_POST['infotabledata'],
$_POST['pastortabledata']
);
$searchData = array();
for($i=0;$i<count($pageArray);$i++) {
if ($pageArray[$i]->pageName == $selectedPage) {
$searchData[1] = $pageArray[$i]->mainTable;
$searchData[2] = $pageArray[$i]->subTable;
$searchData[3] = $pageArray[$i]->infoTable;
$searchData[4] = $pageArray[$i]->pastorTable;
preg_replace($searchData, $postData, $fp);
break;
}
}
//--((( End textual data acquisition and editing (STEP TWO) )))--\\
//--((( Begin final closeout )))--\\
fclose($fp);
$fp = fopen($xml_file, "w") or die("Could not open file");
if (fwrite($xml_file, $fp) === FALSE) {
echo "Cannot write to file ($xml_file)";
exit;
}
else {
echo "Successfully updated Website!<br><a href=index.php?page=MainPage>Click here to return to the index.</a>";
}
xml_parser_free($xml_parser);
fclose($fp);
?>
Ahem. Waaaaaaaaah.
<?php
/*
* XML FILE POSTING SYSTEM
* This code's primary purpose is retrieving data from a
* form and posting it to an XML document. The form in
* question will analyze the XML to determine what pages
* it can modify and what data those pages have. Therefore
* this code must be completely dynamic in its scope.
* By analyzing the XML file and determining its contents,
* The number of pages and information on each can be
* easily determined and the form fields determined on the
* spot.
*/
//--((( Begin XML Parsing Goodness (STEP ONE) )))--\\
$xml_file = "xmlSource.xml";
$xml_pagename_key = "*SITEINFO*PAGE*PAGENAME";
$xml_maintable_key = "*SITEINFO*PAGE*MAINTABLE";
$xml_subtable_key = "*SITEINFO*PAGE*SUBTABLE";
$xml_infotable_key = "*SITEINFO*PAGE*INFOTABLE";
$xml_pastortable_key = "*SITEINFO*PAGE*PASTORTABLE";
$pageArray = array();
$counter = 0;
class PageDef {
var $pageName, $mainTable, $subTable, $infoTable, $pastorTable;
}
function startTag($parser, $data) {
global $current_tag;
$current_tag .= "*$data";
}
function endTag($parser, $data) {
global $current_tag;
$tag_key = strrpos($current_tag, '*');
$current_tag = substr($current_tag, 0, $tag_key);
}
function contents($parser, $data) {
global $current_tag, $xml_pagename_key, $xml_maintable_key, $xml_subtable_key, $xml_infotable_key, $xml_pastortable_key, $counter, $pageArray;
switch($current_tag) {
case $xml_pagename_key:
$pageArray[$counter] = new PageDef();
// The code stops here, at the ">" symbol
$pageArray[$counter]->pageName = $data;
break;
case $xml_maintable_key:
$pageArray[$counter]->mainTable = $data;
break;
case $xml_subtable_key:
$pageArray[$counter]->subTable = $data;
break;
case $xml_infotable_key:
$pageArray[$counter]->infoTable = $data;
break;
case $xml_pastortable_key:
$pageArray[$counter]->pastorTable = $data;
$counter++;
break;
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen($xml_file, "r") or die("Could not open file");
$data = fread($fp, filesize($xml_file)) or die("Could not read file");
if(!(xml_parse($xml_parser, $data, feof($fp)))){
echo "$counter<br>Error on line " . xml_get_current_line_number($xml_parser);
}
# We don't close/free the xml file because we're going to write it in a second.
//--((( End XML Parsing Goodness )))--\\
//--((( Begin textual data acquisition and editing (STEP TWO) )))--\\
$selectedPage = $_POST['selectedpage'];
$postData = array(
$_POST['maintabledata'],
$_POST['subtabledata'],
$_POST['infotabledata'],
$_POST['pastortabledata']
);
$searchData = array();
for($i=0;$i<count($pageArray);$i++) {
if ($pageArray[$i]->pageName == $selectedPage) {
$searchData[1] = $pageArray[$i]->mainTable;
$searchData[2] = $pageArray[$i]->subTable;
$searchData[3] = $pageArray[$i]->infoTable;
$searchData[4] = $pageArray[$i]->pastorTable;
preg_replace($searchData, $postData, $fp);
break;
}
}
//--((( End textual data acquisition and editing (STEP TWO) )))--\\
//--((( Begin final closeout )))--\\
fclose($fp);
$fp = fopen($xml_file, "w") or die("Could not open file");
if (fwrite($xml_file, $fp) === FALSE) {
echo "Cannot write to file ($xml_file)";
exit;
}
else {
echo "Successfully updated Website!<br><a href=index.php?page=MainPage>Click here to return to the index.</a>";
}
xml_parser_free($xml_parser);
fclose($fp);
?>
Ahem. Waaaaaaaaah.