php购物车没有在购物车中显示商品?

时间:2013-11-27 17:11:45

标签: javascript php sql cart

我正在尝试在我的网站上实现一个php购物车。我目前正在从我的数据库中正确读取产品页面。当购物车为空时,显示“购物车中没有商品”文字,但是当购物车中有商品时,会显示此表格的标题,但不显示产品信息。

<form name="cartform" method="post" action="">
        <input type="hidden" name="productid" />
        <input type="hidden" name="command" />
        <span id="cont_shop">
        <input type="button" value="Continue Shopping" onclick="window.location.href='../Project/products.php'" />
        </span>
        <div id="formerror"><?php echo $message ?></div>

        <table id="cart_table">
        <?php
            if(count($_SESSION['cart']))
            {
                echo '<tr>
                <th>Name</th>
                <th>Price</th>
                <th>Quantity</th>
                <th>Amount</th>
                <th>Options</th>
                </tr>';

                $max = count($_SESSION['cart']);

                for($i = 0; $i < $max; $i++)
                {
                    $product_id = $_SESSION['cart'][$i]['productid'];
                    $quantity = $_SESSION['cart'][$i]['quantity'];
                    $product_name = get_product_name($dbc,$product_id);
                    $product_price = get_price($dbc,$product_id);

                    if($quantity == 0)
                    {
                        continue;
                    }

                    echo '<tr>
                    <td>' . $product_name . '</td>
                    <td>&#36; ' . $product_price . '</td>
                    <td><input type="text" name="product' . $product_id . '" value="' . $quantity . '" maxlength="4" size="2" /></td>
                    <td>&#36; ' . $product_price*$quantity . '</td>
                    <td><a href="javascript:del(' . $product_id . ')">Remove Item</a></td>
                    </tr>';
                }

                echo '<tr>
                <td colspan="2"><strong>Order Total: &#36;' . get_order_total($dbc) . '</strong></td>
                <td></td>
                <td colspan="3" id="cart_buttons">
                <input type="submit" value="Clear Cart" onclick="clear_cart()">
                <input type="submit" value="Update Cart" onclick="update_cart()">
                <input type="submit" value="Complete Order" onclick="complete_order()">
                </td>
                </tr>';
            }

            else
            {
                echo '<tr><td>There are no items in your shopping cart.</td></tr>';
            }
        ?>
        </table>
        </form>

当我查看页面源代码时,我看到的只是HTML渲染到这里:                                                           

    <table id="cart_table">
    <tr>
            <th>Name</th>
            <th>Price</th>
            <th>Quantity</th>
            <th>Amount</th>
            <th>Options</th>
            </tr>

1 个答案:

答案 0 :(得分:0)

   <?php
     if(count($_SESSION['cart']))
            { 
   ?>
      <form name="cartform" method="post" action="">
        <input type="hidden" name="productid" />
        <input type="hidden" name="command" />
        <span id="cont_shop">
          <input type="button" value="Continue Shopping" onclick="window.location.href='../Project/products.php'" />
        </span>
        <div id="formerror"><?php echo $message ?></div>

        <table id="cart_table">
        <?php

                echo '<tr>
                <th>Name</th>
                <th>Price</th>
                <th>Quantity</th>
                <th>Amount</th>
                <th>Options</th>
                </tr>';

                $max = count($_SESSION['cart']);

                for($i = 0; $i < $max; $i++)
                {
                    $product_id = $_SESSION['cart'][$i]['productid'];
                    $quantity = $_SESSION['cart'][$i]['quantity'];
                    $product_name = get_product_name($dbc,$product_id);
                    $product_price = get_price($dbc,$product_id);

                    if($quantity == 0)
                    {
                        continue;
                    }

                    echo '<tr>
                    <td>' . $product_name . '</td>
                    <td>&#36; ' . $product_price . '</td>
                    <td><input type="text" name="product' . $product_id . '" value="' . $quantity . '" maxlength="4" size="2" /></td>
                    <td>&#36; ' . $product_price*$quantity . '</td>
                    <td><a href="javascript:del(' . $product_id . ')">Remove Item</a></td>
                    </tr>';
                }

                echo '<tr>
                <td colspan="2"><strong>Order Total: &#36;' . get_order_total($dbc) . '</strong></td>
                <td></td>
                <td colspan="3" id="cart_buttons">
                <input type="submit" value="Clear Cart" onclick="clear_cart()">
                <input type="submit" value="Update Cart" onclick="update_cart()">
                <input type="submit" value="Complete Order" onclick="complete_order()">
                </td>
                </tr>';
             ?>
         </table>
         </form>
      <?php
         }

            else
            {
                echo '<tr><td>There are no items in your shopping cart.</td></tr>';
            }
        ?>