0

我想使用 foreach 循环在我的会话中显示所有存储的数据。我无法展示它。谁能给我一个关于如何显示储值的想法。我需要显示每个添加产品的产品 ID 和信息。这是代码 - `

<?php
session_start();
/*
TEMPLATE NAME: Cart Page
*/

?>

<?php
    $git_product_id = $_POST['git_product_id'];
    $git_required = $_POST['git_required'];
    $git_action = $_POST['git_action'];

    if ( isset($git_product_id)){

        switch($git_action){

            case "add" :
                // adding product
                    // checking first if the product is already added
                    if (isset($_SESSION['cart'][$git_product_id])){
                        echo "You have already added this product";                     
                    } else {
                        // I AM NOT SURE IF THIS CODING IS OKAY. PLEASE CHECK THIS
                        if (!isset($_SESSION['cart'])) {
                          $_SESSION['cart'] = array();
                        }
                        $_SESSION['cart'][$git_product_id] = array('product_id' => $git_product_id, 'info' => $git_required);

                    }
            break;

            case "remove":
                // removing product
                unset($_SESSION['cart'][$git_product_id]);
            break;

            case "empty" :
                // empty cart
                unset($_SESSION['cart']); 
            break;              

        } 
    }
?>

<?php 

    if ($_SESSION['cart'] != ""):
        foreach($_SESSION['cart'] as $product => $key ) : ?>
        <tr>
            <td>
                <?php // I WANT TO SHOW HERE EACH PRODUCT ID and RESPECTIVE REQUIRED INFO ?>
                <?php // BUT I DON'T KNOW HOW TO DO IT ?>
            </td>

            <td>
                <form action="" method="post">
                    <input type="hidden" name="git_product_id" value="<?php echo $product; ?>" />
                    <input type="hidden" name="git_required" value="<?php echo $qty; ?>" />
                    <input type="hidden" name="git_action" value="remove" />
                    <input type="submit" value="Remove" />
                </form>
            </td>

        <?php endforeach; ?> 
    <?php endif; ?>

    <form action="" method="POST">
        <input type="hidden" name="git_product_id" value="<?php echo $product; ?>" />
        <input type="hidden" name="git_required" value="<?php echo $qty; ?>" />
        <input type="hidden" name="git_action" value="empty" />
        <input type="submit" value="empty" />
    </form>             
4

1 回答 1

0

这段代码总是对我有用,它并不完全符合您的要求……但它包含一个 for 循环。

在下面的示例中:
- $sys[session_allowed] 是一个白名单数组,如果没有,您可以跳过/删除它。
- 会话数据可用于$sess[foo1]$sess[foo2]等。

// create session
session_cache_limiter('private, must-revalidate, post-check=0, pre-check=0');
session_start();

// load values, and encrypt
$enc_data = explode(";", session_encode());
$sess_name = array(); $sess_data = array();
for ($i=0;$i<count($enc_data)-1;$i++)
  {
  $sess_get = explode("|", $enc_data[$i]);
  if (substr($sess_get[1], 0, 2)=="s:")
    {
    $tmp = str_replace("\"", "", strstr($sess_get[1], "\""));
    if (strlen($tmp)!=0 && in_array($sess_get[0], $sys[session_allowed]))
      { $sess_data[$i] = $tmp; $sess_name[$i] = $sess_get[0]; }
    }
  else
    {
    $tmp = substr($sess_get[1], 2);
    if (strlen($tmp)!=0 && in_array($sess_get[0], $sys[session_allowed]))
      { $sess_data[$i] = $tmp; $sess_name[$i] = $sess_get[0]; }
    }
  }
if (count($sess_name)>=1) { $sess = array_combine($sess_name, $sess_data); }
else { $sess = array(); }

// now you can access SESSION data by $sess[blabla1], $sess[blabla2]

// to add to session, use function (max 3 add actions):
function session_add($act1="", $act2="", $act3="")
  {
  global $sess, $_SESSION;
  if (strlen($act1)!=0) { $loc = strpos($act1, "="); $act_a = substr($act1, 0, $loc); $act_b = substr($act1, $loc+1); $_SESSION[$act_a] = $act_b; $sess[$act_a] = $act_b; }
  if (strlen($act2)!=0) { $loc = strpos($act2, "="); $act_a = substr($act2, 0, $loc); $act_b = substr($act2, $loc+1); $_SESSION[$act_a] = $act_b; $sess[$act_a] = $act_b; }
  if (strlen($act3)!=0) { $loc = strpos($act3, "="); $act_a = substr($act3, 0, $loc); $act_b = substr($act3, $loc+1); $_SESSION[$act_a] = $act_b; $sess[$act_a] = $act_b; }
  }

// to remove from session, use function (max 3 delete actions):
function session_rem($act1="", $act2="", $act3="")
  {
  global $sess, $_SESSION;
  if (strlen($act1)!=0) { $_SESSION[$act1] = ""; unset($sess[$act1]); }
  if (strlen($act2)!=0) { $_SESSION[$act2] = ""; unset($sess[$act2]); }
  if (strlen($act3)!=0) { $_SESSION[$act3] = ""; unset($sess[$act3]); }
  }
于 2013-02-18T18:20:08.903 回答