【问题标题】:php shopping cart not displaying items in cart?php购物车不显示购物车中的物品?
【发布时间】:2013-11-27 17:11:45
【问题描述】:

我正在尝试在我的网站上实现一个 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>

【问题讨论】:

  • 您是否在浏览器中执行了view source 以验证您是否有 html 出来?也许它有一个语法错误,导致产品信息被浏览器忽略。在代码中完成任何调试,例如var_dump($_SESSION['cart']) 看看里面到底有什么?也许您的数量字段设置不正确,因此您会自动跳过购物车中的所有条目。
  • @Marc B 该页面使用我的 HTML 正常显示。唯一没有加载的是表格标题下方的内容。渲染页面时会显示 var_dump 吗?
  • 我的意思是表格内容的 html。它是否显示在页面的源代码中?如果没有,那么您的代码没有输出任何内容,您需要找出原因。
  • 我在任何地方都看不到session_start();。但我敢打赌你有,对吧? 99% 的人会说“是的,我有,只是没有在我的问题中发布”。
  • @Fred-ii- 是的,我的代码顶部有一个会话开始。 MarcB 当我查看源代码时,源代码在 options 结束标记之后立即停止。

标签: javascript php sql cart


【解决方案1】:
   <?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>';
            }
        ?>

【讨论】:

  • @user3042765,你能解释一下你的代码是做什么的吗?
猜你喜欢
  • 2019-12-31
  • 1970-01-01
  • 2015-06-01
  • 2013-05-02
  • 2015-10-24
  • 2011-04-09
  • 1970-01-01
  • 2015-06-09
  • 1970-01-01
相关资源
最近更新 更多