基本上,我需要跟踪POS系统中的变量。单击顶部的项目时,它会将其添加到数组中的列表中(从数据库中提取),我需要设置" tally"在1处(即添加了1个项目),当您再次单击以添加项目时,它会检查列表中的项目数组,如果它在那里,则将其添加到"计数"。我无法做到这一点,它只是将一个项目添加到数组中,然后在此之后不做任何事情。任何帮助将不胜感激。
<!doctype html>
<html lang="en">
<?
// Connect database.
include("settings.php");
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
?>
<head>
<meta charset="UTF-8"/>
<title>POS</title>
<link rel="stylesheet" href="dist/css/bootstrap.css">
</head>
<body>
<h1>Company</h1>
<h4>User</h4>
<div class="row">
<div class="col-sm-12 col-md-8">
<div class="pagination btn-group" id="items">
<? $sql = "SELECT itemname FROM items ORDER BY itemname ASC";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
$datacost = $row['price'];
$id = $row['id'];
$itemname = $row['itemname'];
echo "<button class='btn btn-medium highlight-color-0' id='$id' item-name='$itemname' data-cost='$datacost'><div class='btn-image gears'></div><span class='btn-text'>$itemname</span>";
}
echo "</button>"; ?>
</div>
</div>
<div class="col-sm-6 col-md-4">
<h2>Totals</h2>
<ul id="items">
</ul>
<h3 id="price">£0</h3>
<button class="btn btn-large background-color-1">checkout</button>
<button class="btn btn-large background-color-2">add to tab</button>
</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
// Set up an object for the order
order =
{
ids: [],
itemNums: [],
total: 0,
// Initialise things
init: function()
{
order.itemNums[0] = 0;
// Do something when someone clicks a button
$('#items button').click(function()
{
id = $(this).attr('id');
name = $(this).attr('item-name');
price = $(this).attr('data-cost');
// Add item to order
if ((order.itemNums[id] < 1) || (order.itemNums[id] == null))
{
order.itemNums[id] = 1;
order.addItemToOrder(id, name, price);
}
else
{
order.itemNums[id]++;
}
});
},
addItemToOrder: function(id, name, price)
{
$("ul#items").append("<div class='tag-remove'>" + name + "</div>");
$("ul#items").append("<div class='tag tag-rounded tag-padded background-color-1 light-text'>" + order.itemNums[id] + "</div><br>");
order.updateTotal(price);
},
updateTotal: function(price)
{
order.total += price * order.itemNums[id];
$('h3#price').html("£" + parseFloat(order.total, 10).toFixed(2));
}
};
$(function()
{
order.init();
});
</script>
</body>
</html>