PDA

View Full Version : Why does it exit out of PHP??



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.

gmerriment
June 23rd, 2006, 03:24 PM
Yeah, and when the first thing I do on a new forum is make a total arse of myself, I'd appreciate nobody slapping me.

Perhaps it doesn't work because it wasn't actually supposed to be included in the original file at all and I'm looking at entirely the wrong file. It's okay! I'm not crazy! Ha ha!

So the moral of the story is: DON'T DRINK AT WORK

gmerriment
June 23rd, 2006, 03:35 PM
Continuing in my efforts to surpass myself in incompetence, I find whenever I include any php file at all, it exits out of php mode as soon as it encounters a ">" symbol. Um... why?

bwh2
June 23rd, 2006, 03:41 PM
$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;
}^this looks to be the culprit. if you're getting "Could not open file", it means your xml file can't be open. if you are getting the other, it means it can't be written to. you may have to change the permissions using chmod().

//edit: didn't see your responses in time. what do you mean it exits out of php? and where is it encountering the ">"?

gmerriment
June 23rd, 2006, 04:20 PM
This is what it outputs (in HTML!):

<?php
/*
* PHP FORM GENERATOR
* This code generates and outputs a form based on
* the contents of a given XML file. Doing this
* allows the form to be generated dynamically.
* At the moment its functionality is limited but
* in a site with more dynamic structures it is
* very helpful.
*/

function makeForm()
{
//--((( Begin XML Parsing Goodness (STEP ONE) )))--\\

$xml_file = "xmlSource.xml";

$xml_pagename_key = "*SITEDATA*SITEINFO*PAGE*PAGENAME";
$xml_maintable_key = "*SITEDATA*SITEINFO*PAGE*MAINTABLE";
$xml_subtable_key = "*SITEDATA*SITEINFO*PAGE*SUBTABLE";
$xml_infotable_key = "*SITEDATA*SITEINFO*PAGE*INFOTABLE";
$xml_pastortable_key = "*SITEDATA*SITEINFO*PAGE*PASTORTABLE";

$pageArray = array();
$counter = 0;
$htmlForm = '<form METHOD="POST" ACTION="xmlSystem.php">';
$htmlPageSelector = '<b>Select a page to edit: </b><select NAME="selectedpage">';

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();
$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);
}

xml_parser_free($xml_parser);

fclose($fp);

//--((( End XML Parsing Goodness )))--\\

//--((( Begin XML Interpretation )))--\\

for($i=0;$i<count($pageArray);$i++) {
$htmlPageSelector .= '<option VALUE="' . $pageArray[$i]->pageName . '">' . $pageArray[$i]->pageName;
}

$htmlPageSelector .= '</select>'
$htmlForm .= $htmlPageSelector
. '<br><b>Modify the data on the selected page:</b>'
. '<br>Main Table: <textarea name="maintabledata"></textarea>'
. '<br>Sub Table: <textarea name="subtabledata"></textarea>'
. '<br>"Our Pastor" Table (top left): <textarea name="pastortabledata"></textarea>'
. '<br>Information Table (bottom left): <textarea name="infotabledata"></textarea>'
. '<br><input TYPE="submit" VALUE="Submit Information">'
. '</form>';

//--((( End XML Interpretation )))--\\

return $htmlForm;
}

After this code, the output is Japanese/Chinese letters. (What the...?) You'll notice on the syntax how it gets to $pageArray[$counter]->pageName = $data; and then quits... however, on Firefox it dies at the very first occurence of a ">" symbol (in this case $htmlForm = '<form METHOD="POST" ACTION="xmlSystem.php">'; )

Arr.

bwh2
June 23rd, 2006, 04:31 PM
uhhh... really sounds like you don't have php installed. try making a new page with this code...
<?php
phpinfo();
?>that explanation would make sense b/c your output is getting hosed at >.

gmerriment
June 26th, 2006, 11:09 AM
It appears to be working, as it outputs the correct php info page. Notably the page works if I comment out the php code using HTML comments:


<!--
<?php
// php stuff to include
?>
-->

...It's wierd.

So that's what I'm doing for the moment. If somebody has another suggestion lemme know.

JoshuaJonah
June 26th, 2006, 11:17 AM
You don't have PHP installed

gmerriment
June 26th, 2006, 12:00 PM
If I don't have PHP installed, then how come the php works?

The only time the PHP doesn't work is when I include a seperate file in my PHP code as follows:


<?php
include("otherFile.php");

echo "This is some freaky shtuff.";
?>

If "otherFile.php" consists of the following:


<?php
echo '<table width="100%" border="1"><tr><td>This is some REALLY freaky shtuff...</td></tr></table><br><br>';
?>

The output SHOULD be:


This is some REALLY freaky shtuff...

This is some freaky shtuff.

But instead it outputs:


><tr><td>This is some REALLY freaky shtuff...</td></tr></table><br><br>';
?> {And here there are a bunch of Japanese characters}

But if I comment out the included PHP file, meaning that the contents of "otherFile.php" will be modified to:


<!--
<?php
echo '<table width="100%" border="1"><tr><td>This is some REALLY freaky shtuff...</td></tr></table><br><br>';
?>
-->

Then it works perfectly. I was told that when including a php file, you MUST use the <?php ?> tags because it exits out of PHP. But I DO that... it just still thinks it's HTML. As long as I comment out, it works.

It's wierd.

bwh2
June 26th, 2006, 12:56 PM
when you run this code:
<?php phpinfo(); ?>does the browser output a big page listing all of the php info looking something like this: http://images.google.com/images?q=php%20info ? if it does not, you either do not have php installed correctly or you are not accessing the files correctly. what is the URL you are entering to access the page?

gmerriment
June 26th, 2006, 12:59 PM
BWH: Exactly like that.

bwh2
June 26th, 2006, 01:29 PM
ok, so you have php installed and you are accessing the local site correctly. maybe it's a character encoding problem. hmm.. i wonder about this: http://us3.php.net/manual/en/function.include.php#65837

what program are you using to write the php files.