Jump to content

PHP and XML price selection


dasick

Recommended Posts

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:

<?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:

<?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;
}
?>

Link to comment
Share on other sites

First your XML isn't arranged terribly well. If I were designing it I would do something more like





Basic guidelines are to not mix text and XML as siblings and to have it drill down into data. First you pick the size paper, then a quantity, number of sides to use, and finally you get the price. Since the number of sides is fixed you can collapse some tiers together in a reasonable way.

 

Second, if you have DOMDocument then you have SimpleXMLElement too.

$xml = new SimpleXMLElement($path, 0, true);

// price for 500 2.5x2.5 one-sided cards
$pricenode = current($xml->xpath("//size[@width=2.5][@height=2.5]/price[@qty=500]"));
if ($pricenode) {
    $price = (float)$pricenode["oneside"];
} else {
    // no price set for that size/number of sides/quantity
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.