【问题标题】:Replacing/updating SESSION arrays替换/更新 SESSION 数组
【发布时间】:2015-10-03 21:00:08
【问题描述】:

这有点与我的第一篇文章有​​关,但是是新问题。我不确定协议,所以我创建了一个新主题。 (link to first post)

我在这里四处搜索,发现了我的问题的几个不同实例,但似乎无法理解任何足以推进我自己的程序的内容。

简要说明:购物车。我在带有 itemID 的页面上列出了一些项目。用户点击一个项目,它被添加到购物车。如果用户两次单击同一项目,则数量应增加一(当前是硬编码)。 $_SESSION['cart'] 数组应该从空开始并且是动态的。

当前问题:在测试中,它似乎只循环遍历数组一次,即使参数一是数组,array_push 也会出错,没有按应有的方式删除项目,也没有在 for 循环中读取数量。它在print_r($_SESSION['cart'] 中显示“1” 但是$_SESSION['cart']['itemID']['quantity']; 中的'0'

Warning: array_push() expects parameter 1 to be array, null given in C:\xampp\htdocs\A06_DictCart\controller\updateCart.php on line 25

我想有一些不同的问题,但解决一个问题将帮助我与其他问题一起前进,我想它们是半相关的。

viewCart.php

<?php
session_start();

/* Display the itemID and quantity of each item in the shopping cart.
This may be implemented by iterating though the items in the dictionary 
and displaying their keys and values. */
echo ' ** array/ '.print_r($_SESSION['cart']).' ** count/ '.count($_SESSION['cart']);
echo '<br>'.$_SESSION['cart']['itemID']['quantity'];
echo '<center><h2><u>SHOPPING CART</u></h2>';
echo '<br><TABLE border=1 cellPadding=3 cellSpacing=1>
        <TBODY>
        <TR>
          <TD>ItemID</TD>
          <TD>Quantity</TD>
          <TD>Remove from Cart</TD></TR>';

if (isset($_SESSION['cart'])) {
    $c = count($_SESSION['cart']);
    for ($x=0; $x <= $c; $x++){
        echo '<tr><td>'.$_SESSION['cart']['itemID'].'</td>';
        echo '<td>'.$_SESSION['cart']['itemID']['quantity'].'</td>';
        echo "<td><a href='updateCart.php?action=remove&amp;itemID=".$_SESSION['cart']['itemID']."&amp;quantity=".$qty."> Remove from Cart </a></td></tr>";
        $x++;
    }   
}
else {
    $msg = '<i> ** cart is empty ** </i>';
    echo '<tr> <td></td>';
    echo '<td> </td>';
    echo "<td> </td></tr>";
}

echo '</tbody></table>';
echo $msg;
echo '<br><br><a href="../controller/default.php">Back to Catalog</a>';
echo '<br><a href="../controller/updateCart.php?action=clear">Empty Cart</a></center>';
?>

updateCart.php

<?php
/* Read the values of action, itemID, and quantity from the querystring.  
Items will either be added or removed from the shopping cart depending 
on the values of the querystring. Once the quantity is 0 the item should 
be unset from the cart. */

session_start();

$action = $_GET['action'];
$itemID = $_GET['itemID'];
$qty = $_GET['quantity'];
$msg;

if (empty($_SESSION['cart'])) $_SESSION['cart'] = array();
//array(); //('123' => 0, '456' => 0, '789' => 0, '101' => 0);
//$cart = array($_SESSION['cart']);

// ADD TO CART
if ($action == 'add') { 
    if (!in_array($itemID, $_SESSION['cart'])) {    
        $_SESSION['cart']= array('itemID' => $itemID, 'quantity' => $qty);  // Insert new item
    } else {
        $temp = array($_SESSION['cart']);
        $q += $_SESSION['cart'][$itemID]['qty'];
        $temp = array('itemID' => $itemID, 'quantity' => $q);
        array_push($_SESSION['cart'][$itemID], $temp);// Update existing item's quantity
    }
    $msg = $qty.' of item # '.$itemID.' has been added to your cart.';
}
// REMOVE FROM CART
if ($action == 'remove'){ 
    if (($_SESSION['cart'][$itemID]['quantity'] - 1) <= 0)
        unset ($_SESSION['cart'][$itemID]); // If new value is zero, unset elements
    else
        $_SESSION['cart'][$itemID]['quantity']--;   // Else decrease quantity by one
    $msg = $qty.' of item # '.$itemID.' has been removed to your cart.';
}
// EMPTY CART
if ($action == 'clear') { 
    unset($_SESSION['cart']);   // Unset session
    session_destroy();  
    $msg = 'Your shopping cart has been emptied.';
}

?>


<HTML>
    <HEAD>
    </HEAD>
<BODY>
<center>
    <p> <?php echo $msg; ?><br><br>
    <p><a href="../controller/default.php">Back to Catalog</a></p>
    <p><a href="../controller/viewCart.php">View Cart</a></p>
</center>
</BODY>
</HTML>

由于这是一项正在进行中的工作,因此在代码语法方面可能存在一些我尚未发现的差异。我在另一篇文章中看到的“从购物车中删除”语句中的一些代码。

感谢任何帮助。

【问题讨论】:

    标签: php arrays session cart associative


    【解决方案1】:

    我没有测试这个,但是试试这个代码:

    // ADD TO CART
    if ($action == 'add') { 
        if (!isset($_SESSION['cart'][$itemID])) {    
            $_SESSION['cart'][$itemID] = array('itemID' => $itemID, 'quantity' => $qty);  // Insert new item
        } else {
            $_SESSION['cart'][$itemID]['quantity'] += $qty     
        }
        $msg = $qty.' of item # '.$itemID.' has been added to your cart.';
    }
    

    我就是这样写的。我想在你的代码中

    $_SESSION['cart']= array('itemID' => $itemID, 'quantity' => $qty);  // Insert new item
    

    应该是

    $_SESSION['cart'][$itemID] = array('itemID' => $itemID, 'quantity' => $qty);  // Insert new item
    

    而且我认为您在原始代码中将“数量”与“数量”键混淆了。在更新时,您阅读了密钥 qty,但您的初始化程序将 quantity 作为密钥:

    $q += $_SESSION['cart'][$itemID]['qty'];
    

    可能应该写成

    $q += $_SESSION['cart'][$itemID]['quantity'];
    

    编辑:注意

    【讨论】:

    • 谢谢 - 这绝对是其中的一部分!现在正在将变量添加到数组中,包括 ID 和数量。一路上,我的 viewCart 似乎受到了影响。我会继续玩的。
    • 不错 :) 玩得开心!并查看我为您的密钥建议的这种简短编码样式.. $array[key][subkey][subsubkey]。未来:如果您仅仅因为想删除一个数组而销毁会话,您可能还想在那里使用$_SESSION['cart'] = array();。否则您可能会意外删除未来的状态事物。如果您能投票赞成我的答案,我将有缺陷-我需要 50 分才能评论另一个线程:D
    • 我是否在 for 循环中调用了错误的数量?无论我做什么,它都会打印“0”,但我可以看到它在数组中是“1”。
    • 也许看到这张图片:link 我确实标记了我在其中看到的缺陷。
    • 顺便说一句:这一行:'$temp = array($_SESSION['cart']);' 对我来说没有任何意义。如果您仍然使用旧代码,我会删除它
    【解决方案2】:

    在开始编写任何代码之前,您可能需要考虑应用程序的逻辑以及您想要实现的目标。

    if ($action == 'add') { 
      if (!in_array($itemID, $_SESSION['cart'])) {    
        $_SESSION['cart']= array('itemID' => $itemID, 'quantity' => $qty);  // Insert new item
      } else {
        $temp = array($_SESSION['cart']);
        $q += $_SESSION['cart'][$itemID]['qty'];
        $temp = array('itemID' => $itemID, 'quantity' => $q);
        array_push($_SESSION['cart'][$itemID], $temp);// Update existing item's quantity
      }
    }
    

    应该是:

    if ($action == 'add' ) {
       $_SESSION['cart'][$itemID]+=$qty;
    }
    

    (注意数组结构的变化)

    【讨论】:

      猜你喜欢
      • 2022-01-17
      • 2011-06-04
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      • 2020-11-16
      • 2018-06-16
      相关资源
      最近更新 更多