I'm creating a simple Pizza website with online ordering capabilities. Since it's a simple site it doesn't require an extensive database which is why I'm using XML to store all my menu items. I'm new to PHP and XML so this site is part of a learning experience
So far I've been successful at pulling the XML values of my items and listing them. I've also gotten all but my price value to pass through a $_POST variable.
Can someone help me get the price of my items to pass through a $_POST?
Right now it shows up as Array and not the value corresponding to the selected "Small " or "Large" value. I suspect that I'm not identifying the node correctly using Xpath.

Disregard some of the commented // variables. I was trying various ways to get at the price value with no luck.

Please take a look and let me know what I'm doing incorrectly. Thank you.

Below is a snippet of the XML and PHP:

	<name>Pizza Shop</name>
		<street>1212 Main St</street>
		<city>New York</city>
		<category cat_name="Pizza">
			<item name="Tomato and Cheese">
				<desc>Pizza topped with tomatoes and cheese</desc>
				<size id="Small">
				<size id="Large">

-- PHP --

$xml = new SimpleXMLElement(file_get_contents("menu2.xml"));

// Identify the variables from XML using xpath
$categories = $xml->xpath("/store/menu/category");
	echo "<ul>";
		foreach ($categories as $category)
			/* Setting up a dynamic URL link for each menu item using $_GET without a form */
			echo "<li><a href='index.php?select=".$category['cat_name']."'>";
			echo $category['cat_name']."</a></li>";
	echo "</ul>";
<div id="sidebar">
if (isset($_GET["select"]))
/* Sanitizing for the URL so we know we have a valid XML menu item as the $_GET['select'] */
	$menuItem = $_GET["select"];
	$menuResults = $xml->xpath("/store/menu/category[@cat_name='$menuItem']");
	if (!empty($menuResults)) $categoryName = $_GET["select"];
foreach ($categories as $category)
			/* Below if statement compares variable in XML database the variable passed through $_GET */
			if ($category['cat_name'] == $categoryName)
			echo "<form action='add_cart.php' method='post'>";
			echo "<h1>".$category['cat_name']."</h1><br/>";
			$items = $category->item;
				echo "<select id='cat' name='item'>";
					foreach ($items as $item)
						echo "<option value='{$item['name']}'>";
						echo "{$item['name']}";
						echo "</option>\n";
				echo "</select>\n";
				echo "<br/><br/>";
			$sizes = $item->size;
					foreach ($sizes as $size)
						$varSize = $size['id'];
						if ($size[id] == "")
							echo "";
							echo "<b>".$varSize."</b>\n"; 
							echo "<input type='radio' name='size' value='{$size['id']}' onmousedown='this.__chk = this.checked' onclick='if (this.__chk) this.checked = false' />\n"; 
							echo "<br/>";
//						$varPrice = $item->size[@id='$varSize']->price);
//						$varPrice = $xml->xpath("/size[@id='$varSize']/price");
//						$varPrice = $xml->xpath("/store/menu/category/item/size[@id='varSize']/price/text()");
						$varPrice = $varSize->price;
						echo "<input type='hidden' name='cost[]' value='{$varCost}' />";
					echo "<br/>";
					echo "<label><b>Quantity: </b></label><input type='text' name='qty' size='1' maxlength='2' value='0'/>\n";					
			echo "<br/><br/><input type='submit' name='order' value='Order' />";
			echo "</form>";

I was able to solve the problem but not without having to restructure the functionality of my site. Originally I wanted drop down menus for each food item which could be selected first and then each subsequent selection (size, price, etc...) could be made. My problem was getting the correct values to pass through the $_POST variable. I suspect that using a client side script such as Javascript might allow me to achieve this effect however since I'm new to programming, I solved the problem the way I knew how.
I switched from using a drop down selection to simply listing the all the items. The foreach loop used for the drop down menu in combination with xpath made it difficult to identify the correct values for $_POST, especially the price. If someone knows of a way to keep the functionality of a drop down menu, without using Javascript, I'd be curious to know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.