Page 1 of 1

PHP and XML price selection

Posted: Fri Oct 28, 2011 3:42 pm
by dasick
My website, http://www.LawnBagSigns.com, has a lot of price tables, which I think, are getting bulky and hard to read. Like

this price page here, I think the table might be scaring people away.

So here is what I want to try - have a series of dropdown boxes where the user can choose the details a product one by one

(for example, first the size, then the quantity). I want to do this in PHP and XML, and I want to avoid JavaScript since,

believe it or not, not all of my website's visitors have JavaScript enabled.

My problem at the moment is that the XML document is full of white scape that is screwing over my system, but I don't want

to sacrifice readability by blobbing the whole xml file into one line with no spaces (because if I want to change or add

something... you get the idea).

So my question is, how do I get rid of the white scape?

Here is the prototype I've been working on:
http://www.lawnbagsigns.com/flyers-test.php
The XML:

Code: Select all

<?xml version="1.0" encoding="ISO-8859-1"?>

<?xml-stylesheet type="text/xsl" href="whitespace_ripper.xsl"?>

<price_table>
	<side>1
		<qty>25
			<size>3.5 x 8.5
				<price>
				37
				</price>
			</size>
			<size>8.5 x 5.5
				<price>
				37
				</price>
			</size>
			<size>4.25 x 11
				<price>
				37
				</price>
			</size>
			<size>8.5 x 7
				<price>
				38
				</price>
			</size>
		</qty>
	</side>
	<side>2
		<qty>25
			<size>3.5 x 8.5
				<price>
				37
				</price>
			</size>
			<size>8.5 x 5.5
				<price>
				37
				</price>
			</size>
			<size>4.25 x 11
				<price>
				37
				</price>
			</size>
			<size>8.5 x 7
				<price>
				38
				</price>
			</size>
		</qty>
	</side>
	
</price_table>
and the PHP code:

Code: Select all

<?php

//PRE: Session

function priceSelect ($path)
{

	if (!empty($_REQUEST['Previous']))
	{
	
		$_SESSION['xmlCursor'] = $_SESSION['xmlCursor']->parentNode;
		echo 'test1<br />';
	}
	elseif (!empty($_REQUEST['Next']))
	{
	
		$exitLoop = false;
		$childCursor = $_SESSION['xmlCursor']->firstChild;
		
		while ($exitLoop == false)
		{
			if ($childCursor == $_SESSION['choice'])
			{
				$_SESSION['xmlCursor'] = $childCursor;
				$exitLoop = true;
			}
			else
			{
				$childCursor = $childCursor->nextSibling;
			}
		}
		echo 'test2<br />';
	}
	else
	{
	
		$_SESSION['userMadeChoices'] = array(array());
		$_SESSION['xmlCursor'] = loadXML($path);
		echo $_SESSION['xmlCursor']->firstChild->nodeValue;
		echo $_SESSION['xmlCursor']->parentNode->nodeValue;
		echo 'test3<br />';
	
	}

	$reload = $_SERVER['PHP_SELF'];
	
	echo 
	'<form action="'.$reload.'" method="post">';
	
		displayChoices($_SESSION['userMadeChoices']);
		echo 'test4<br />';
		displayMenu($_SESSION['xmlCursor'], $_SESSION['userMadeChoices'] );
		echo 'test5<br />';
		displayButtons($_SESSION['xmlCursor']);
		echo 'test6<br />';
	
	echo '</form>';
}	

function loadXML($path)
{
	$xmlDoc = new DOMDocument();
	$xmlDoc->load($path);
	$xmlDoc = $xmlDoc->documentElement;
	return $xmlDoc;
}

function displayChoices($data)
{
	for ($i = 0; $i < count($data); $i ++)
	{
		echo $data[$i][0];
		echo '</br>';
	}
}

function displayMenu($xml, $data)
{
	echo '<select name="choice">';
	//$xml = $xml->documentElement;
		//$data[$xml] = 
	
	foreach($xml->childNodes AS $child)
	{
		echo '<option value="' . $child->firstChild->nodeValue . '">' . $child->firstChild->nodeValue . 

'</option>';
	}		

    echo '</select>';
}

function displayButtons($xml)
{	
	if ($xml->childNodes != null)
	{
		echo '<input type="submit" name="Previous" value="Previous Step" />';
	}
	
	if ($xml->parentNode != null)
	{
		echo '<input type="submit" name="Next" value="Next Step" />';
	}
}

function stripWhitespace ($string)
{
	
	return $string;
}
?>

Re: PHP and XML price selection

Posted: Sun Oct 30, 2011 2:54 am
by social_experiment
Controlling Whitespace
Hth
dasick wrote:...not all of my website's visitors have JavaScript enabled.
Yeah that's been known to happen from time to time :)