在PHP会话中存储乘数变量并使用循环显示所有变量

时间:2013-02-18 12:55:20

标签: php session-variables

我想通过存储来自ORDER页面的订单值并在CART页面上显示来保存产品。这是两个页面编码。有人可以帮帮我吗?这是wordpress主题。

订购页面

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


$git_product_id = $_GET['git_product_id'];
$git_required = $_GET['git_required'];
$git_action = $_GET['git_action'];

if ($git_product_id != "" && $git_required != "" && $git_action != "" ){            
    ?>
        <form action="" method="POST" class="add-form">
            <label>
                <?php 

                    switch($git_required){

                        case "name" :
                            // showing Name
                            echo "Account Name:";
                        break; 

                        case "url" :
                            // showing url
                            echo "Web Url:";
                        break;

                    };
                ?>
            </label>
            <input type="text" name="git_required" value="" />
            <input type="hidden" name="git_product_id" value="<?php echo $git_product_id; ?>" />
            <input type="hidden" name="git_action" value="add" />
            <input type="submit" value="Add to cart" class="add-to-cart-button" />
        </form> 
    <?php

} else {

    echo "Sorry an error took place.";
}

&GT;

=============================================== ============

CART PAGE

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

&GT;

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

    if ($git_product_id != "" && $git_action != "" ){

        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
                        $_SESSION['cart'][$git_product_id] = array('product_id' => $git_product_id, 'git_required' => $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 => $qty) : ?>
        <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>             

1 个答案:

答案 0 :(得分:3)

你写了$_SESSION['cart']['$product']...但是你应该写$_SESSION['cart'][$product]...而不是'$ $产品......

说明:'$product'不是$ product的值,只是字符串 $ product

$product = 5;
var_dump($product);       // -> int(5)
var_dump("$product");     // -> string(1) "5"
var_dump('$product');     // -> string(8) "$product"