sum php mysql $ SESSION_START

0

Good morning, I'm new to the topic of sessions in PHP , and the inconvenience is that I do not know how to add data that is in the session and not inside the DB.

I already tried several combinations to make the array run and add the total data but I could not ( cant*precio unitario ), always presented individually and not the total of the sum ( SUMA of total prices).

I appreciate your help. Thanks.

<div class="fresh-table">
            <?php
                /*
                * Esta es la consula para obtener todos los productos de la base de datos.
                */
                $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">Cant</th>
                        <th data-field="prod" data-sortable="true">Producto</th>
                        <th data-field="total" data-sortable="true">Total</th>
                        <th data-field="actions"></th>
                    </thead>
                <?php 
                /*
                * Apartir de aqui hacemos el recorrido de los productos obtenidos y los reflejamos en una tabla.
                */
                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="Quitar" 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: 
<!-- Aquí debe ir la suma de los datos-->
                        
                    </bold>
                </h3>
            </span>
            </br></br>
                <a href="carrito.php" class="btn btn-danger"><i title="Ir al carrito" class="fa fa-cart"></i> Ir al carrito</a>
                
                <?php else:?>
                    <p class="alert alert-warning">El carrito esta vacio.</p>
                <?php endif;?>
                
            </div>
    
asked by Jhon117 02.05.2017 в 02:38
source

1 answer

0

You must accumulate the subtotals somewhere. Something like this inside the loop.

$total += $c["q"]*$r->price;

Then simply print the total

 echo $total;
    
answered by 02.05.2017 / 14:08
source