我有一个示例图书目录,目前在添加到购物车时会显示图书的数量和数量。
但是我想在添加到购物车时显示图书的标题,isbn编号,数量和总价。
会话变量是一个关联数组:title =>数量和我假设我必须将其更改为新的关联数组:title => [ISBN,Qty,Total]的数组)但我无法绕过它。
以下是代码示例。
html页面
<head>
<script type="text/javascript" src="CartPHP.js"></script>
</head>
<body>
<br/>
<img id="cover" src="test.jpg" />
<br />
<br />
<b>Book:</b><span id="book">name of book</span><br />
<b>Authors: </b><span id="authors"> Hart, Brandon, Jake</span>
<br /><b>ISBN: </b><span id="ISBN">0724583501</span>
<br /><b>Price: </b><span id="price">$39.99</span>
<br /><br />
<a href="#" onclick="AddRemoveItem('Add');" >Add to Shopping Cart</a>
<br /><br />
<span id="cart" ></span>
</body>
javascript
function getData()
{
if ((xHRObject.readyState == 4) &&(xHRObject.status == 200))
{
var serverResponse = xHRObject.responseXML;
var header = serverResponse.getElementsByTagName("book");
var spantag = document.getElementById("cart");
spantag.innerHTML = "";
for (i=0; i<header.length; i++)
{
if (window.ActiveXObject)
{
spantag.innerHTML += " " +header[0].firstChild.text;
spantag.innerHTML += " " + header[0].lastChild.text + " " + "<a href='#' onclick='AddRemoveItem(\"Remove\");'>Remove Item</a>";
}
else
{
spantag.innerHTML += " " +header[0].firstChild.textContent;
spantag.innerHTML += " " + header[0].lastChild.textContent + " " + "<a href='#' onclick='AddRemoveItem(\"Remove\");'>Remove Item</a>";
}
}
}
}
function AddRemoveItem(action)
{
var book = document.getElementById("book").innerHTML;
if(action=="Add")
{
xHRObject.open("GET", "ManageCart.php?action=" + action + "&book=" + encodeURIComponent(book) + "&value=" + Number(new Date), true);
}
else
{
xHRObject.open("GET", "ManageCart.php?action=" + action + "&book=" + encodeURIComponent(book) + "&value=" + Number(new Date), true);
}
xHRObject.onreadystatechange = getData;
xHRObject.send(null);
}
Php
$newitem = $_GET["book"];
$action = $_GET["action"];
if ($_SESSION["Cart"] != "")
{
$MDA = $_SESSION["Cart"];
if ($action == "Add")
{
if ($MDA[$newitem] != "")
{
$value = $MDA[$newitem] + 1;
$MDA[$newitem] = $value;
}
else
{
$MDA[$newitem] = "1";
}
}
else
{
$MDA= "";
}
}
else
{
$MDA[$newitem] = "1";
}
$_SESSION["Cart"] = $MDA;
ECHO (toXml($MDA));
function toXml($MDA)
{
$doc = new DomDocument('1.0');
$cart = $doc->createElement('cart');
$cart = $doc->appendChild($cart);
foreach ($MDA as $a => $b)
{
$book = $doc->createElement('book');
$book = $cart->appendChild($book);
$title = $doc->createElement('title');
$title = $book->appendChild($title);
$value = $doc->createTextNode($a);
$value = $title->appendChild($value);
$quantity = $doc->createElement('quantity');
$quantity = $book->appendChild($quantity);
$value2 = $doc->createTextNode($b);
$value2 = $quantity->appendChild($value2);
}
$strXml = $doc->saveXML();
return $strXml;
}
答案 0 :(得分:0)
如果变量$ MDA是您尝试转换为数组以将其分配给$ _SESSION [&#39; cart&#39;],您可以使用类似字符串附加$ MDA的所有值&#34; |&#34;,然后在将所有值附加到$ MDA后将其展开。 例如:
$append = "|";
$MDA .= "1".$append;
$MDA .= "3".$append;
$MDA .= "5";
//set to session
$_SESSION['cart'] = $MDA;
//then, when you want to get session values as an array
$sessionArrayValues = explode('|',$_SESSION['cart']);
希望有帮助...