Php 根据用户登录状态调整购物车中的数组值
根据用户是否登录,如何调整会话数组中的各个值。因此,基本上,数组值等于购物车中的单个商品价格,如果用户登录或注销Php 根据用户登录状态调整购物车中的数组值,php,mysql,arrays,session,Php,Mysql,Arrays,Session,根据用户是否登录,如何调整会话数组中的各个值。因此,基本上,数组值等于购物车中的单个商品价格,如果用户登录或注销会话,数组值将进行调整 以下是我当前的代码: PHP if(!empty($_SESSION)){ foreach($_SESSION['basket'] as $b){ if ($session->logged_in) { $b['itemprice'] = ""; } $sub_total = $b['itemprice'] *
会话
,数组值将进行调整
以下是我当前的代码:
PHP
if(!empty($_SESSION)){
foreach($_SESSION['basket'] as $b){
if ($session->logged_in) { $b['itemprice'] = ""; }
$sub_total = $b['itemprice'] * $b['itemqty'];
$total = $total + $sub_total;
// display contents of shopping cart
}
}
elseif (empty($_SESSION)) { ?>
// display message informing the user that their cart is empty
MYSQL
$itemids = array();
foreach ($_SESSION['basket'] as $item) {
$itemids[] = "'" . $item['itemid'] . "'";
}
$itemids_str = implode(',', $itemids);
$query = mysql_query("SELECT product_id, price, price_100 FROM product WHERE product_id IN ($itemids_str)");
while ($result=mysql_fetch_array($query)) { }
我最接近于实现这一点的是,购物车中的每个产品都有相同的价格,如果用户登录或注销,价格会发生变化。但是,我需要将每个项目更改为特定于该项目产品ID的新价格
我们的一些会员在某些项目上会获得折扣,因此,一旦用户从客户
登录到注册的用户
,则需要在页面刷新时更改价格
例如:
**Logged in == FALSE**
Item 1 Price: 100.00
Item 2 Price: 100.00
**User logs in (Logged in == TRUE)**
Item 1 Price: 85.00
Item 2 Price: 94.00
我希望我已经说清楚了——任何建议都将不胜感激。
谢谢选项A:
function updateCartPrices()
{
$itemids = array();
foreach ($_SESSION['basket'] as $item) {
$itemids[] = "'" . $item['itemid'] . "'";
}
$itemids_str = implode(',', $itemids);
$query = mysql_query("SELECT product_id, price, price_100 FROM product WHERE product_id IN ($itemids_str)");
while ($result=mysql_fetch_array($query)) {
foreach ($_SESSION['basket'] as $key => $item)
{
if($result['product_id'] == $item['itemid'])
{
$_SESSION['basket'][$key]['itemprice'] = ($_SESSION['logged_in']) ? $result['price'] : $result['price_100'];
}
}
}
}
if(!empty($_SESSION)){
updateCartPrices();
foreach($_SESSION['basket'] as $b){
$sub_total = $b['itemprice'] * $b['itemqty'];
$total = $total + $sub_total;
// display contents of shopping cart
}
}
elseif (empty($_SESSION)) { ?>
// display message informing the user that their cart is empty
备选方案B(性能更好):
谢谢你的回答-这正是我需要的,然而,会员价格并不是基于这样的公式。它来自SQL数据库,因此'if($session->logged_in)$price将等于数据库中的'price_100'列,其中product_id与篮子中项目的id匹配。很抱歉,我更新了我的答案。你的意思是这样的吗?再次感谢-我已经尝试了两种选择。选项A显示来宾用户和登录用户的登录价格。选项B不会改变物品的价格。知道为什么吗?
$\u会话['logged\u in']
是否在登录时变量true
?getLoggedInPrices()
需要返回登录价格的数组。您可以通过在$loggedInPrices=getLoggedInPrices()之后放置print\r($loggedInPrices)
来检查它是否已填充如果$\u SESSION['logged\u in']
为true
则在之后添加echo'loggedinprice filled for product.$b['itemid']
在$b['itemprice']=$loggedInPrices[$b['itemid']之后
这样您就可以检查它是否改变了价格。
function getLoggedInPrices()
{
$itemids = array();
foreach ($_SESSION['basket'] as $item) {
$itemids[] = "'" . $item['itemid'] . "'";
}
$itemids_str = implode(',', $itemids);
$query = mysql_query("SELECT product_id, price, price_100 FROM product WHERE product_id IN ($itemids_str)");
$prices = array();
while ($result=mysql_fetch_array($query)) {
$prices[$result['product_id']] = $result['price'];
}
return $prices;
}
if(!empty($_SESSION)){
$loggedInPrices = getLoggedInPrices();
foreach($_SESSION['basket'] as $b){
if($_SESSION['logged_in'])
{
$b['itemprice'] = $loggedInPrices[$b['itemid']];
}
$sub_total = $b['itemprice'] * $b['itemqty'];
$total = $total + $sub_total;
// display contents of shopping cart
}
}
elseif (empty($_SESSION)) {