Hello
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:
<store>
<name>Pizza Shop</name>
<address>
<street>1212 Main St</street>
<city>New York</city>
<state>NY</state>
<zip>10046</zip>
</address>
<menu>
<category cat_name="Pizza">
<item name="Tomato and Cheese">
<desc>Pizza topped with tomatoes and cheese</desc>
<size id="Small">
<price>5.50</price>
</size>
<size id="Large">
<price>9.75</price>
</size>
</item>
-- PHP --
<?php
session_start();
require_once("classes.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>
<div id="sidebar">
<?php
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 "";
}
else
{
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>";
}
}
?>