Link to home
Start Free TrialLog in
Avatar of kevp75
kevp75Flag for United States of America

asked on

Casting Help?

I am trying to cast some variables I am getting from an XML file, however it is not working...

I have tried changing the value in Allow_Albums to 1, 0, -1 and it always shows the "Shows Albums".  I have tried settype, I have also tried to cast the variable, and the results are the same...

can someone help:
xml file:
<Gallery>
	<Allowed_File_Types>jpg, gif, png</Allowed_File_Types>
	<Max_Image_Size>1500000</Max_Image_Size>
	<Max_Images_At_Once>10</Max_Images_At_Once>
	<Rows_Per_Page>3</Rows_Per_Page>
	<Cols_Per_Row>8</Cols_Per_Row>
	<Allow_Albums>false</Allow_Albums>
	<Allow_Public_Upload>true</Allow_Public_Upload>
	<Show_Title>true</Show_Title>
	<Show_Description>true</Show_Description>
	<Show_Slideshow>true</Show_Slideshow>
	<Thumbnailer>true</Thumbnailer>
	<Thumbnail_Height>125</Thumbnail_Height>
	<Thumbnail_Width>200</Thumbnail_Width>
</Gallery>
 
 
 
 
	for ($i = 0;$i < $nodes->length;$i++) {
		$Allow_Albums = $nodes->item(3)->nodeValue; //boolean
		$Rows_Per_Page = $nodes->item(1)->nodeValue; //numeric
		$Cols_Per_Row = $nodes->item(2)->nodeValue; //numeric
		$Thumbnailer = $nodes->item(8)->nodeValue; //boolean
		$Thumbnail_Height = $nodes->item(9)->nodeValue; //numeric
		$Thumbnail_Width = $nodes->item(10)->nodeValue; //numeric
		$Show_Title = $nodes->item(5)->nodeValue; //boolean
		$Show_Description = $nodes->item(6)->nodeValue; //boolean
		$Show_Slideshow = $nodes->item(7)->nodeValue; //boolean
		$Allow_Public_Upload = $nodes->item(4)->nodeValue; //boolean
	}
	if ($Allow_Albums) {
		
		return "Show Albums";
	} elseif (!$Allow_Albums) {
		
		return "Do Not Show Albums";
	} else {
		return "The configuration file is not valid.  Please contact the site administrator.";
	}

Open in new window

Avatar of Arikael
Arikael
Flag of Switzerland image

What exactly is $nodes?

Does it hold all your nodes from <Gallery>?
if so, shouldn't it be?

$Allow_Albums = $nodes->item(5)->nodeValue;
Avatar of hielo
Try this:
for ($i = 0;$i < $nodes->length;$i++) {
            $Allow_Albums = (boolean)$nodes->item(3)->nodeValue; //boolean
            $Rows_Per_Page = (int)$nodes->item(1)->nodeValue; //numeric
            $Cols_Per_Row = (int)$nodes->item(2)->nodeValue; //numeric
            $Thumbnailer = (boolean)$nodes->item(8)->nodeValue; //boolean
            $Thumbnail_Height = (int)$nodes->item(9)->nodeValue; //numeric
            $Thumbnail_Width = (int)$nodes->item(10)->nodeValue; //numeric
            $Show_Title = (boolean)$nodes->item(5)->nodeValue; //boolean
            $Show_Description = (boolean)$nodes->item(6)->nodeValue; //boolean
            $Show_Slideshow = (boolean)$nodes->item(7)->nodeValue; //boolean
            $Allow_Public_Upload = (boolean))$nodes->item(4)->nodeValue; //boolean
      }
Avatar of kevp75

ASKER

I tried that as well hielo....

@Arikel...yes, typo, and it's been changed since....

still no dice...only sees the value as a string
Avatar of kevp75

ASKER

<Gallery>
      <Allowed_File_Types>jpg, gif, png</Allowed_File_Types>
      <Max_Image_Size>1500000</Max_Image_Size>
      <Max_Images_At_Once>10</Max_Images_At_Once>
      <Rows_Per_Page>3</Rows_Per_Page>
      <Cols_Per_Row>8</Cols_Per_Row>
      <Allow_Albums>false</Allow_Albums>
      <Allow_Public_Upload>true</Allow_Public_Upload>
      <Show_Title>true</Show_Title>
      <Show_Description>true</Show_Description>
      <Show_Slideshow>true</Show_Slideshow>
      <Thumbnailer>true</Thumbnailer>
      <Thumbnail_Height>125</Thumbnail_Height>
      <Thumbnail_Width>200</Thumbnail_Width>
</Gallery>


$ConfigFile is the full path to the XML file above
	$doc = new DOMDocument();
	$doc->load($ConfigFile);
	$parent = $doc->getElementsByTagName("Gallery");
	$nodes = $parent->item(0)->getElementsByTagName("*");
	//Get the configuration options
	for ($i = 0;$i < $nodes->length;$i++) {
		$Allow_Albums = (boolean)$nodes->item(5)->nodeValue; //boolean
		$Rows_Per_Page = (float)$nodes->item(3)->nodeValue; //numeric
		$Cols_Per_Row = (float)$nodes->item(4)->nodeValue; //numeric
		$Thumbnailer = (boolean)$nodes->item(10)->nodeValue; //boolean
		$Thumbnail_Height = (float)$nodes->item(11)->nodeValue; //numeric
		$Thumbnail_Width = (float)$nodes->item(12)->nodeValue; //numeric
		$Show_Title = (boolean)$nodes->item(7)->nodeValue; //boolean
		$Show_Description = (boolean)$nodes->item(8)->nodeValue; //boolean
		$Show_Slideshow = (boolean)$nodes->item(9)->nodeValue; //boolean
		$Allow_Public_Upload = (boolean)$nodes->item(6)->nodeValue; //boolean
	}
	if ($Allow_Albums) {
		
		return "Show Albums";
	} elseif (!$Allow_Albums) {
		
		return "Do Not Show Albums";
	} else {
		return "The configuration file is not valid.  Please contact the site administrator.";
	}

Open in new window

Have you tried to trim the value?

(boolean) "false" will return true.
Avatar of kevp75

ASKER

trim the value?  in PHP?
Avatar of kevp75

ASKER

that didn't change anything
Avatar of kevp75

ASKER

for the record:
<?php
echo (boolean)"false";
?>

and

<?php
echo (boolean) "false";
?>

both return true.  Does this mean that PHP boolean datatypes are bogus?
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kevp75

ASKER

what does that mean?   $Allow_Albums = "true"==$nodes

?  defaulting it to true?    So I take it 1 = true, 0 = false?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kevp75

ASKER

much thanks hielo!
much welcome kevp75!