我正在进行一个开放课程项目,我必须通过xml文件加载菜单并跟踪用户购买的内容并将其显示在购物车中。几乎所有事情都已完成,但我似乎无法访问通过$_SESSIONS
购买的物品。关于网络如何运行的想法很简单,它使用get变量从菜单中加载特定类别,当一个项目被购买时,我将它作为数组保存在$ _SESSION中但是当我访问cart.php
时我不这样做似乎在$_SESSION
这是我的index.php
<?php
session_start();
//loading the xml file
$dom = simplexml_load_file("../menu.xml");
// variable to store category id
$categoryid;
//to store all the data of a particular category id
$names = [];
//to store the name of the category
$categoryname;
//to count how many categories were in there in the xml file
$categorycount = count($dom->xpath("/menu/category"));
//Checking if the user visited via GET and if GET variable is set
if($_SERVER["REQUEST_METHOD"] == "GET")
{
if(!isset($_GET["cat"]))
{
$categoryid = 1;
}
// checking if the variable is set within the limits of number of categories loaded
else if (isset($_GET["cat"]) && ($_GET["cat"] > 0 && $_GET["cat"] <= $categorycount))
{
$categoryid = $_GET["cat"];
}
else
{
print("invalid category id");
}
//now loading the appropriate category from xml
foreach($dom->xpath("/menu/category[@id = '{$categoryid}']") as $category)
{
$categoryname = $category->option;
//saving the data
foreach($category->name as $name)
{
$names[] = [
"optionname" => $name->type,
"small" => $name->small,
"large" => $name->large,
"general" => $name->general
];
}
}
//loading the displaying files
include("../views/header.php");
include("../views/viewpage.php");
}
&GT;
文件header.php
刚刚获得文件的head
标记,因此不在此处粘贴,表格由viewpage.php
生成,因此其代码如下
<body>
<div class = "cart">
<a href= "cart.php">View Cart</a>
</div>
<?php if(isset($error)): ?>
<div class = "warning">
<p>No Item was selected</p>
</div>
<?php endif; ?>
<table>
<thead>
<tr>
<th colspan = "4"><?= $categoryname ?></th>
</tr>
<tr>
<th>Options</th>
<th>Small</th>
<th>Large</th>
<th>General Price</th>
</tr>
</thead>
<tbody>
<form action = "buy.php" method = "POST">
<?php foreach($names as $item): ?>
<tr class = "even">
<td><?= $item["optionname"] ?></td>
<td>
<!-- checking every key value pair of $item if its empty and dynamically giving the names to inputs combining name and price-->
<?php if($item["small"] != ""): ?>
<?= $item["small"] ?>
<input type = "text" name = "<?= $item["optionname"]?>#<?=$item["small"]?>">
</input>
<?php endif; ?>
</td>
<td>
<?php if($item["large"] != ""): ?>
<?= $item["large"] ?>
<input type = "text" name = "<?= $item["optionname"]?>#<?= $item["large"] ?>">
</input>
<?php endif; ?>
</td>
<td>
<?php if($item["general"] != ""): ?>
<?= $item["general"] ?>
<input type = "text" name = "<?= $item["optionname"]?>#<?= $item["general"] ?>">
</input>
<?php endif; ?>
</td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<input type = "submit" value = "Buy!">
</form>
<!-- printing the list to display categories using the categorycount variable-->
<div class= "list">
<h4>category</h4>
<?php
for($i = 1; $i <= $categorycount; $i++)
{
print("<ul>
<li><a href = \"?cat={$i}\">{$i}</a></li>
</ul>");
}
?>
</div>
</body>
购买过程由buy.php
完成,其代码如下
<?php
session_start();
//print_r($_POST);
//print("</br>");
$quantity;
$typename;
$price;
foreach($_POST as $key => $value)
{
//array to save category name, its size, price and quantity of each item
$cart = [];
if(!empty($value))
{
$quantity = $value;
//print($key ." val ". $value);
//print("</br>");
//breaking the input name to get the type thats in xml from input name, the section before '#'
$typename = strstr($key, "#", true);
//getting the part that comes after '#'
$price = substr($key, strpos($key, "#") + 1);
//now converting the '_' to a space and '.'
$typename = str_replace("_", " ", $typename);
$price = str_replace("_", ".", $price);
//checking if '&' in name is present then convert if to &
/*if(strpos($typename, "&"))
{
$typename = str_replace("&", "&", $typename);
}*/
//print($typename);
//print("</br>");
//print($price);
//print("</br>");
//opening the xml file to search
$dom = simplexml_load_file("../menu.xml");
//searching
foreach($dom->xpath("/menu/category") as $category)
{
//iterating over every name tag in xml
foreach($category->name as $name)
{
//print("</br>checking on run{$name->type} </br></br>");
//checking every type tag
if($name->type == $typename)
{
//storing what category name the type belongs to
$cartcategoryname = $category->option;
//print("the category is {$cartcategoryname}</br>");
//test to see what size the above matched type is
if($name->small == $price)
{
$size = "small";
}
else if($name->large == $price)
{
$size = "large";
}
else
{
$size = "general";
}
}
}
/*adding the name,size,type quantity in an array
* which would be added to $_SESSION and the array
* would be wiped clean.*/
$cart = [
"category" => $cartcategoryname,
"type" => $typename,
"size" => $size,
"price" => $price,
"quantity" => $quantity
];
}
$_SESSION["cart"][] = $cart;
}
/* name of category, its type, size, price and quantity in
* arry cart */
/*$cart[] = [
"category" => $cartcategoryname,
"type" => $typename,
"size" => $size,
"price" => $price,
"quantity" => $quantity
];*/
//print_r($cart);
//print("</br></br></br>");
//pushing the above created arror into session global
//$_SESSION["cart"][] = $cart;
}
//print_r($_SESSION);
//print("</br>");
/*foreach($_SESSION["cart"] as $cartitem)
{
print($cartitem["category"]."</br>");
print($cartitem["quantity"]."</br>");
print($cartitem["type"]."</br>");
}*/
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'index.php?cat=1';
header("Location: http://$host$uri/$extra");
exit;
//header("Location: index.php?cat=1");
//exit;
&GT;
现在当我在print_r($_SESSION)
中使用cart.php
时,我无法看到任何事情
代码是
<?php
session_start();
foreach($_SESSION["cart"] as $cartitem)
{
print($cartitem["category"]."</br>");
print($cartitem["quantity"]."</br>");
print($cartitem["type"]."</br>");
}
//phpinfo();
&GT;
xml文件如下
<menu>
<category id = '1'>
<option>Pizzas</option>
<name>
<type>Tomato & Cheese</type>
<small>5.50</small>
<large>9.75</large>
<general/>
</name>
<name>
<type>Onions</type>
<small>6.85</small>
<large>10.85</large>
<general/>
</name>
</category>
<category id = '2'>
<option>Side Orders</option>
<name>
<type>Onion Rings</type>
<small>2.60</small>
<large>2.95</large>
<general/>
</name>
<name>
<type>French Fries</type>
<small>2.85</small>
<large>3.85</large>
<general/>
</name>
</category>
答案 0 :(得分:0)
:
$_SESSION["cart"][] = $cart;
中的 buy.php
不在(正确的)foreach
内。
把它放在最后但是在
之内 foreach($dom->xpath("/menu/category[@id = '{$categoryid}']") as $category)
循环。
$cart = [
"category" => $cartcategoryname,
"type" => $typename,
"size" => $size,
"price" => $price,
"quantity" => $quantity
];
$_SESSION["cart"][] = $cart;
} // End of foreach($dom->xpath("/menu/category[@id = '{$categoryid}']") as $category)
答案 1 :(得分:0)
通过加入$_SESSION
,cart.php
工作了error_reporting(E_ALL);
ini_set("display_errors", 1)
我收到了错误Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed' in [no active file] on line 0
做了一些谷歌搜索,发现xml数据仍然是一个xml数据,需要type cast
进入string
。这样做让我可以在cart.php