Php 使用会话函数从数组中选择行
我正在尝试制作一个商店页面,但我一直在向购物车添加产品。我希望通过使用数组的会话来实现这一点。现在,该函数将为每行添加1个,而我只希望它为按钮所在的行添加1个。下面是我的代码Php 使用会话函数从数组中选择行,php,arrays,function,session,counter,Php,Arrays,Function,Session,Counter,我正在尝试制作一个商店页面,但我一直在向购物车添加产品。我希望通过使用数组的会话来实现这一点。现在,该函数将为每行添加1个,而我只希望它为按钮所在的行添加1个。下面是我的代码 <?php //Initialization session_start(); include ("store_functions.php"); //Variables $var = ""; if (isset($_GET['sort'])) { $var = "?sort=".$_GET['sort'];
<?php
//Initialization
session_start();
include ("store_functions.php");
//Variables
$var = "";
if (isset($_GET['sort'])) {
$var = "?sort=".$_GET['sort'];
}
//Table data
$products = array();
$products[] = array("Product" => "Payday2", "System" => "PC", "Price" => 29.99);
$products[] = array("Product" => "Metal Gear Solid 5", "System" => "PS4", "Price" => 49.99);
$products[] = array("Product" => "The Last of Us", "System" => "PS3", "Price" => 34.99);
$products[] = array("Product" => "Forza 5", "System" => "XBOX ONE", "Price" => 59.99);
$products[] = array("Product" => "Gran Turismo 6", "System" => "PS3", "Price" => 59.99);
//Sorting table function
if (isset($_GET['sort'])) {
switch ($_GET['sort']) {
case "prod":
$func = "sort_product";
break;
case "sys":
$func = "sort_system";
break;
case "prc":
$func = "sort_price";
break;
default:
$func = "sort_product";
break;
}
usort ($products, $func);
}
?>
如果数据库中有表产品,只需使用array\u push函数在作为数组的会话变量中插入产品ID即可。不要在会话中保存所有内容。我刚开始学习php,不知道push函数,但感谢您的快速回复。我将对此做一些研究。@ameer:array\u push增加了开销,因为它是一个函数调用。使用语言构造$anArray[]=“push this value”;,而不是函数;。
<!doctype html>
<html>
<head>
<title>
Gamestore Zero
</title>
</head>
<body>
<table border='1'>
<th>
<a href='?sort=prod'>Product</a>
</th>
<th>
<a href='?sort=sys'>Compatibility</a>
</th>
<th>
<a href='?sort=prc'>Price</a>
</th>
<th>
Add to cart
</th>
<tbody>
<?php echo listing_array($products, $var);?>
</tbody>
</table>
<div>
<p>
Shopping cart
</p>
<p>
<?php $func = add_product(); var_dump($func);?>
</p>
<p>
Grand Total: $
</p>
</div>
</body>
</html>
<?php
//Array listing (function)
function listing_array ($products, $var){
$html = "";
foreach ($products as $key => $value) {
$html.= "<tr><td>".$value["Product"]."</td><td>".$value["System"]." </td><td>".$value["Price"]."</td><td><form action='store_session.php".$var."' method='POST'>
<input type='submit' name='atc' value='Add'/><iput type='hidden' name='session_var' value='1'/></form></td></tr>";
}
return $html;
}
//Sorting functionality (per array $Product)
function sort_product ($a, $b) {
if ($a["Product"]==$b["Product"]) {
return 0;
}
if ($a["Product"]<$b["Product"]) {
return -1;
}
return 1;
}
//Sorting functionality (per array $System)
function sort_system ($a, $b) {
if ($a["System"]==$b["System"]) {
return 0;
}
if ($a["System"]<$b["System"]) {
return -1;
}
return 1;
}
//sort functionality (per array $Price)
function sort_price ($a, $b) {
if ($a["Price"]==$b["Price"]) {
return 0;
}
if ($a["Price"]<$b["Price"]) {
return -1;
}
return 1;
}
//Add new item to cart(function)
function add_product () {
if (isset($_SESSION['session_var'])) {
$_SESSION['session_var']++;
}
else {
$_SESSION['session_var'] = "0";
}
return $_SESSION['session_var'];
}
?>