【问题标题】:Combining Session Data with Database Data将会话数据与数据库数据相结合
【发布时间】:2017-05-02 04:11:57
【问题描述】:

我是这个 php 主题的新手,问题是我不知道如何添加会话中而不是数据库中的数据。

我已经尝试了几种组合来创建数组路径并添加总数据,但我无法(不能乘以单价),它们总是单独呈现,而不是总和(总价的总和) .

<div class="fresh-table">
    <?php
    /*
    * This is the console to get all the products in the database.
    */
    $products = $con->query("select * from product");
    if(isset($_SESSION["cart"]) && !empty($_SESSION["cart"])):
    ?>
    <table id="fresh-table" class="table table-responsive">
        <thead>
            <th data-field="cant" data-sortable="true">Quantity</th>
            <th data-field="prod" data-sortable="true">Product</th>
            <th data-field="total" data-sortable="true">Total</th>
            <th data-field="actions"></th>
        </thead>
        <?php 
        /*
        * From here we take the route of the products obtained and reflect them in a table.
        */
        foreach($_SESSION["cart"] as $c):
            $products = $con->query("select * from product where id=$c[product_id]");
            $r = $products->fetch_object();
        ?>
        <tr>
            <td><?php echo $c["q"];?></td>
            <td><?php echo $r->name;?></td>
            <td>$ <?php echo $c["q"]*$r->price; ?></td>
            <td style="width:auto;">
                <?php
                $found = false;
                foreach ($_SESSION["cart"] as $c) {
                    if($c["product_id"]==$r->id){
                        $found = true;
                        break;
                    }
                }
                ?>
                <a rel="tooltip" title="Remove" class="table-action remove" href="cart/delfromfloat.php?id=<?php echo $c["product_id"];?> ">
                    <i class="fa fa-trash-o fa-lg"></i>
                </a>
            </td>
        </tr>
        <?php endforeach; ?>
    </table>
</div>
</br>
<span>
    <h3>
        <bold>Total: $
            <?php
            foreach($_SESSION["cart"] as $pr):
                $products = $con->query("select * from product where id=$pr[product_id]");
                $r = $products->fetch_object();
                $subtotal = $pr["q"]*$r->price;
                $sumArr[] = $subtotal;
                echo array_sum($sumArr);
            ?>
            <?php endforeach; ?>
        </bold>
    </h3>
</span>
</br></br>
<a href="carrito.php" class="btn btn-danger"><i title="Go to cart" class="fa fa-cart"></i> Go to cart</a>
<?php else:?>
<p class="alert alert-warning">The cart is empty.</p>
<?php endif;?>
</div>

【问题讨论】:

  • 大概这是数量乘以价格$c["q"]*$r-&gt;price,因此您将其存储在echo $subtotal = $c["q"]*$r-&gt;price; $sumArr[] = $subtotal; 之类的数组中,然后您想要总计,您只需echo array_sum($sumArr);...至少这是我从你的问题中理解。除了检查购物车是否存在并遍历项目之外,我不知道会话部分的来源。
  • 您好@Jhon117,在使用$_SESSION 之前,请确保您已在顶部启动会话,即session_start()
  • 测试了,但是屏幕上的结果: Total: $ 15000 30000 其中15000是第一个产品,第二个产品没有显示,显示的是两个产品的总和。

标签: php mysql session


【解决方案1】:

正如我所说,您需要存储小计并在总计中使用array_sum()

<?php
function getCart($con)
    {
        if(empty($_SESSION["cart"]))
            return array();
        # Store the ids for the sql query
        # Store the qty values for later
        foreach($_SESSION["cart"] as $c) {
            $ids[]  =   $c['product_id'];
            $qty[$c['product_id']]  =   $c['q'];
        }
        # Default array
        $row    =   array();
        # Creating one query is more efficient then a whole bunch
        $sql    =   "select * from product where id IN (".implode(', ',$ids).")";
        $con->query($sql);
        # Fetch assoc so all values are arrays, not a mix of
        # arrays and objects (for consistency and ease)
        while($result = $con->fetch_assoc()) {
            # Create the quantity value from stored
            $result['qty']      =   $qty[$result['id']];
            # Create the subtotal value from stored and db
            $result['subtotal'] =   $result['price'] * $qty[$result['id']];
            # Assign the row
            $row[]              =   $result;
        }
        # Return cart array
        return $row;
    }
?>
<div class="fresh-table">
    <?php
    if(!empty($_SESSION["cart"])){
    ?>
    <table id="fresh-table" class="table table-responsive">
        <thead>
            <th data-field="cant" data-sortable="true">Quantity</th>
            <th data-field="prod" data-sortable="true">Product</th>
            <th data-field="total" data-sortable="true">Total</th>
            <th data-field="actions"></th>
        </thead>
        <?php 
        $grandtotal =   array();
        /*
        * From here we take the route of the products obtained and reflect them in a table.
        */
        foreach(getCart($con) as $item) {
            $grandtotal[]   =   $c["subtotal"];
        ?>
    <tr>
        <td><?php echo $item["qty"] ?></td>
        <td><?php echo $item['name'] ?></td>
        <td>$ <?php echo $c["subtotal"] ?></td>
        <td style="width:auto;">
            <?php
            $found = false;
            foreach ($_SESSION["cart"] as $c) {
                if($c["product_id"]==$item['id']){
                    $found = true;
                    break;
                }
            }
            ?>
            <a rel="tooltip" title="Remove" class="table-action remove" href="cart/delfromfloat.php?id=<?php echo $item["product_id"] ?> ">
                <i class="fa fa-trash-o fa-lg"></i>
            </a>
        </td>
    </tr>
<?php   } ?>
</table>
</div>
</br>
<!-- Just echo the stored subtotals here, don't loop again -->
<span><h3><bold>Total: $<?php echo array_sum($grandtotal) ?></bold></h3></span>
</br></br>
    <a href="carrito.php" class="btn btn-danger"><i title="Go to cart" class="fa fa-cart"></i> Go to cart</a>

<?php
}
else { ?>
        <p class="alert alert-warning">The cart is empty.</p>
<?php 
} ?>

</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 2023-04-10
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多