Page 1 of 1

My script is not recognizing a boolean variable

Posted: Tue May 20, 2003 6:49 pm
by particle
Hello all,

Here's my problem: I have a webpage that accepts user input, and write that input into an XML file. It then redisplays the page until the user quits. The first time the user enters data, I want it to write some simple XML headers. So I set up a variable called $headerwritten, which is initially false. The XML headers should only be written if this variable is false. Once the headers are written, the value of the variable changes to true so the headers won't be written each time thru, capice?

This is the first script that gets called:

Code: Select all

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

$areadesc=HTMLSpecialChars($_POST['areadesc']);
$areaname=HTMLSpecialChars($_POST['areaname']);
$climate=HTMLSpecialChars($_POST['climate']);
$roomnumber=$_POST['roomnumber'];
$roomname=$_POST['roomname'];
$shortdesc=$_POST['shortdesc'];
$longdesc=$_POST['longdesc'];
$inout=$_POST['inout'];
$headerwritten=$_POST['headerwritten'];

$filename = sanatizeFilename($areaname);

echo "Headerwritten = $headerwritten";

if(!$headerwritten)
{
        #This never executes even though the variable is false! HELP! 
        echo "Preparing to write headers!";
        writeRoomHeader($areaname, $climate, $areadesc, $headerwritten);
        $headerwritten = true;
}

writeRoomDetails($areaname, $roomnumber, $roomname, $shortdesc, $longdesc, $inout);
drawPage();
?>
The relevant include() file includes this code:

Code: Select all

<?phpfunction drawPage()
{

$areaname = $_POST['areaname'];
$headerwritten = $_POST['headerwritten'];

#Extraneous code to draw a webpage removed for brevity

echo "<INPUT TYPE=HIDDEN NAME=areaname VALUE=$areaname>";
echo "<INPUT TYPE=HIDDEN NAME=headerwritten VALUE=$headerwritten>";
}

function writeRoomDetails($filename, $roomnumber, $roomname, $shortdesc, $longdesc, $inout)
{
        $safename = sanatizeFilename($filename);

        if (!($fp = fopen($safename, "a"))) die ("Cannot open $safename");

        $bodytext = "\n\t<room>";
        $bodytext .= "\n\t\t<roomnumber>$roomnumber</roomnumber>";
        $bodytext .= "\n\t\t<roomname>$roomname</roomname>";

        fwrite($fp, $bodytext);
}

function writeRoomHeader($filename, $climate, $areadesc, $headerwritten)
{
        echo "Inside writeRoomHeader creation block.";
        $safename = sanatizeFilename($filename);

        if (!($fp = fopen($safename, "a"))) die ("Cannot open $safename");
        $xmlheader = createXMLHeader($filename, $climate, $areadesc);
        fwrite($fp, $xmlheader);
}

function createXMLHeader($filename, $climate, $areadesc)
{
        $xmlheader = "<?xml version="1.0"?>";
        $xmlheader .= "\n<area name=$filename climate=$climate>";
        $xmlheader .="\n<areadesc>$areadesc</areadesc>";
        return $xmlheader;
}
If anyone can point out what I'm doing wrong, I'd appreciate it.

Re: My script is not recognizing a boolean variable

Posted: Tue May 20, 2003 7:19 pm
by Wayne Herbert
particle wrote:

Code: Select all

<?php
echo "Headerwritten = $headerwritten";
So, what does this statement display? A number or true/false?

Is $headerwritten ever declared explicity as a boolean? Is it set explicitly to true or false? I've seen (and performed) major confusion when assuming that a value in a non-boolean variable tests true or false.

Re: My script is not recognizing a boolean variable

Posted: Tue May 20, 2003 7:33 pm
by particle
Wayne Herbert wrote:
particle wrote:

Code: Select all

<?php
echo "Headerwritten = $headerwritten";
So, what does this statement display? A number or true/false?

Is $headerwritten ever declared explicity as a boolean? Is it set explicitly to true or false? I've seen (and performed) major confusion when assuming that a value in a non-boolean variable tests true or false.
Oh jeez! I knew I was forgetting to include something in my post.

My script begins with one page, foo.php that includes this line:

Code: Select all

<?php
<INPUT TYPE=HIDDEN NAME=headerwritten VALUE="false">
That then gets passed into the form that gets redisplayed as long as the user enters data.
?>