[英]How to display items using sessions
I am doing a opencourse project where I have to load a menu through a xml file and keep track of what the user buys and display it in a cart. 我正在做一个开放式项目,我必须通过xml文件加载菜单,并跟踪用户购买的商品并将其显示在购物车中。 Almost everything is done but I dont seem to access the items bought through the
$_SESSIONS
. 几乎所有内容都已完成,但我似乎无法访问通过
$_SESSIONS
购买的商品。 The idea of how the web is running is simple it loads a particular category from the menu using a get variable and when an item is bought I am saving it in the $_SESSION as an array but when I visit cart.php
I dont seem to see the record in $_SESSION
网络运行方式的想法很简单,它使用get变量从菜单中加载特定类别,并且在购买商品时将其保存为$ _SESSION作为数组,但是当我访问
cart.php
我似乎没有查看$_SESSION
的记录
here is my index.php 这是我的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");
}
?> ?>
the file header.php
just got the head
tag of the file so not pasting it here, table is produced by viewpage.php
so its code is as follows 文件
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>
the buying process is done by buy.php
, its code is as follows 购买过程由
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;
?> ?>
now when I use print_r($_SESSION)
in cart.php
I cant see any of the things code for it is 现在,当我在
cart.php
使用print_r($_SESSION)
,看不到它的任何事物代码
<?php
session_start();
foreach($_SESSION["cart"] as $cartitem)
{
print($cartitem["category"]."</br>");
print($cartitem["quantity"]."</br>");
print($cartitem["type"]."</br>");
}
//phpinfo();
?> ?>
the xml file is as follows 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>
The : :
$_SESSION["cart"][] = $cart;
in buy.php
is not inside the (correct) foreach
. 在
buy.php
中不在(正确) foreach
。
Put it at the end but inside of 放在最后但在里面
foreach($dom->xpath("/menu/category[@id = '{$categoryid}']") as $category)
loop. 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)
ok got the $_SESSION
working in cart.php
, by including error_reporting(E_ALL); ini_set("display_errors", 1)
好的,通过包含
error_reporting(E_ALL); ini_set("display_errors", 1)
在cart.php
使用$_SESSION
error_reporting(E_ALL); ini_set("display_errors", 1)
error_reporting(E_ALL); ini_set("display_errors", 1)
I got an error Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed' in [no active file] on line 0
did a bit of google search and found out that xml data is still a xml data and one needs to type cast
it into a string
. 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
收到一条Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed' in [no active file] on line 0
做了一点Google搜索,发现了xml数据仍然是xml数据,需要将其type cast
转换为string
。 Doing that allowed me to see the session variables in cart.php
这样做使我可以在
cart.php
查看会话变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.