Pregunta:
Fecha: 10-04-2016 21:07:07
(En Español)
en php y mysql?
esta es la columna que quiero sumar
https://www.dropbox.com/s/g8kqyggg4vojbkt/Sumar.PNG?dl=0
y este es mi codigo de php
¿Cómo sumar una columna de total en PHP y MySQL?[Resuelta]
Hola buenas noches a todos, disculpen me podrian ayudar, cómo puedo sumar una columna totalen php y mysql?
esta es la columna que quiero sumar
https://www.dropbox.com/s/g8kqyggg4vojbkt/Sumar.PNG?dl=0
y este es mi codigo de php
<section class="content-header">
<h1><i class='fa fa-shopping-cart'></i> Ventas Realizadas</h1>
</section>
<section class="content">
<div class="row">
<div class="col-md-12">
<div class="clearfix"></div>
<br>
<div class="btn-group">
<a class="btn btn-default" href="index.php?view=sells&t=0"> <?php if(isset($_GET["t"]) && $_GET["t"]=="0" ):?><i class="glyphicon glyphicon-ok-sign"></i> <?php endif; ?> Pendientes</a>
<a class="btn btn-default" href="index.php?view=sells&t=1"> <?php if(isset($_GET["t"]) && $_GET["t"]=="1" ):?><i class="glyphicon glyphicon-ok-sign"></i> <?php endif; ?> Finalizados</a>
<a class="btn btn-default" href="index.php?view=sells"> <?php if(!isset($_GET["t"])):?><i class="glyphicon glyphicon-ok-sign"></i> <?php endif; ?>Todos</a>
</div>
<?php foreach(Productdata::getAllActive() as $product):?> <?php endforeach ; ?>
<?php
$page = 1;
if(isset($_GET["page"])){
$page=$_GET["page"];
}
$limit=10;
if(isset($_GET["limit"]) && $_GET["limit"]!="" && $_GET["limit"]!=$limit){
$limit=$_GET["limit"];
}
$products=null;
if(!isset($_GET["t"])){
$products = SellData::getAll();
}else if(isset($_GET["t"]) && $_GET["t"]=="0" ){
$products = SellData::getAllUnApplied();
}
else if(isset($_GET["t"]) && $_GET["t"]=="1" ){
$products = SellData::getAllApplied();
}
if(count($products)>0){
?>
<div class="box box-solid box-primary">
<div class="box-header">
<h3 class="box-title"></h3>
</div>
<div class="box-body table-responsive">
<table class="table table-bordered table-hover datatable">
<thead>
<th class="success" class="success">Ver</th>
<!--<th class="success">Id</th>-->
<th class="success">N° Mesa</th>
<th class="success">Mesero</th>
<th class="success">Menús</th>
<th class="success">Cantidad</th>
<th class="success">Total</th>
<th class="success">Status</th>
<th class="success">Fecha</th>
</thead>
<?php foreach($products as $sell):?>
<tr>
<td style="width:30px;"><a href="index.php?view=onesell&id=<?php echo $sell->id; ?>" class="btn btn-xs btn-success"><i class="glyphicon glyphicon-eye-open"></i></a></td>
<!--<td style="width:60px;">N°<?php echo $sell->id;?></td>-->
<td style="width:90px;"><?php echo $sell->item_id;?></td>
<td>
<?php
$mesero = UserData::getById($sell->mesero_id);
echo $mesero->name." ".$mesero->lastname;
?>
</td>
<td><?php echo $product->name ;?></td>
<td>
<?php
$operations = OperationData::getAllProductsBySellId($sell->id);
$rx = 0;
foreach($operations as $operation){
$rx += $operation->q;
}
echo $rx;
?></td>
<td>
<?php
$total=0;
foreach($operations as $operation){
$product = $operation->getProduct();
$total += $operation->q*$product->price_out;
}
echo "<b>S/. ".number_format($total,2,",",".")."</b>";
?>
</td>
<td style="width:100px;"><center><?php
if($sell->is_applied) { echo "<p class='label label-primary'><i class='glyphicon glyphicon-ok'></i> Finalizado</p>"; }
else { echo "<p class='label label-warning'><i class='glyphicon glyphicon-time'></i> Pendiente</p>"; }
?>
</center></td>
<td style="width:180px;"><?php echo $sell->created_at; ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
<br><br><h1>Total: S/. <?php echo number_format($total,2,",","."); ?></h1>
</div>
<div class="btn-group pull-right">
</div>
<div class="clearfix"></div>
<?php
}else{
?>
<div class="jumbotron">
<h2>No hay ventas</h2>
<p>No se han agregado ventas.</p>
</div>
<?php
}
?>
<br><br><br><br><br><br><br><br><br><br>
</div>
</div>
</section>
Votos: 2 -
Respuestas: 6 -
Vistas: 16
Compartir en: Google
Facebook
Twitter
LinkedIn
Link
Respuestas:
-
Fecha: 12-04-2016 11:50:51 Hola amigo,
Lo que veo es que te imprime el último registro lo que yo haría aunque es algo largo, es crear un array y dentro de While ir agregando en valor a sumar y con un contador al final vas llamando uno a uno.
Y al final los sumas.
Esa sería una forma, pero para hacerlo más dinámico té recomiendo que uses JQuery.
Pones un script que recorra todos los totales y te lo valla sumando, así sería una forma para evitar el recargo de la web si usas llamas la tabla por Ajax.
Nota: si lo haces con JQuery sería bueno que al contador le pusieras un elector de counter.
Saludos.
Equipo Arduino-PA Votos: 0 - Link respuesta -
Fecha: 12-04-2016 16:06:19 Lo que tienes q hacer es lo siguiente ante de hacer esto
echo "<b>S/. ".number_format($total,2,",",".")."</b>";
Vas agrear un acumulador quedando asi
$totalgeneral+=$total; echo "<b>S/. ".number_format($total,2,",",".")."</b>";
Ahora faltaria inicializar la variable totalgeneral y eso lo harias ante del foreach quedando asi
<?php $totalgeneral=0; foreach($products as $sell):?>
Por ultimo solo faltaria reemplazar esto
<br><br><h1>Total: S/. <?php echo number_format($total,2,",","."); ?></h1>
Por esto
[code=php] <br><br><h1>Total: S/. <?php echo number_format($totalgeneral,2,",","."); ?></h1>
Saludo prueba y me avisas Votos: 2 - Link respuesta -
Fecha: 12-04-2016 19:53:49 Hola buenas noches disculpe no se si lo lo habre hecho bien, aceptare tu correccion, pero se puede decir que ingrese los cogidos de esta manera
<!-- Para sumar el totla--> <?php $totalgeneral = 0; foreach ($products as $sell) { $totalgeneral+=$total; echo "<b>S/. " . number_format($total, 2, ",", ".") . "</b>"; } ?> <br><br><h1>Total: S/. <?php echo number_format($totalgeneral, 2, ",", "."); ?></h1> <!-- Fin -->:
Por de otra manera me salia error.
ahora cuando lo modifique de esta manera el resultado es asi:
Link del imagen:
https://www.dropbox.com/s/xm9c51c4uq6mo1b/suma_total.PNG?dl=0
Código completo
<section class="content-header"> <h1><i class='fa fa-shopping-cart'></i> Ventas Realizadas</h1> </section> <section class="content"> <div class="row"> <div class="col-md-12"> <div class="clearfix"></div> <br> <div class="btn-group"> <a class="btn btn-default" href="index.php?view=sells&t=0"> <?php if(isset($_GET["t"]) && $_GET["t"]=="0" ):?><i class="glyphicon glyphicon-ok-sign"></i> <?php endif; ?> Pendientes</a> <a class="btn btn-default" href="index.php?view=sells&t=1"> <?php if(isset($_GET["t"]) && $_GET["t"]=="1" ):?><i class="glyphicon glyphicon-ok-sign"></i> <?php endif; ?> Finalizados</a> <a class="btn btn-default" href="index.php?view=sells"> <?php if(!isset($_GET["t"])):?><i class="glyphicon glyphicon-ok-sign"></i> <?php endif; ?>Todos</a> </div> <?php foreach(Productdata::getAllActive() as $product):?> <?php endforeach ; ?> <?php $page = 1; if(isset($_GET["page"])){ $page=$_GET["page"]; } $limit=10; if(isset($_GET["limit"]) && $_GET["limit"]!="" && $_GET["limit"]!=$limit){ $limit=$_GET["limit"]; } $products=null; if(!isset($_GET["t"])){ $products = SellData::getAll(); }else if(isset($_GET["t"]) && $_GET["t"]=="0" ){ $products = SellData::getAllUnApplied(); } else if(isset($_GET["t"]) && $_GET["t"]=="1" ){ $products = SellData::getAllApplied(); } if(count($products)>0){ ?> <div class="box box-solid box-primary"> <div class="box-header"> <h3 class="box-title"></h3> </div> <div class="box-body table-responsive"> <table class="table table-bordered table-hover datatable"> <thead> <th class="success" class="success">Ver</th> <!--<th class="success">Id</th>--> <th class="success">N° Mesa</th> <th class="success">Mesero</th> <th class="success">Menús</th> <th class="success">Cantidad</th> <th class="success">Total</th> <th class="success">Status</th> <th class="success">Fecha</th> </thead> <?php foreach($products as $sell):?> <tr> <td style="width:30px;"><a href="index.php?view=onesell&id=<?php echo $sell->id; ?>" class="btn btn-xs btn-success"><i class="glyphicon glyphicon-eye-open"></i></a></td> <!--<td style="width:60px;">N°<?php echo $sell->id;?></td>--> <td style="width:90px;"><?php echo $sell->item_id;?></td> <td> <?php $mesero = UserData::getById($sell->mesero_id); echo $mesero->name." ".$mesero->lastname; ?> </td> <td><?php echo $product->name ;?></td> <td> <?php $operations = OperationData::getAllProductsBySellId($sell->id); $rx = 0; foreach($operations as $operation){ $rx += $operation->q; } echo $rx; ?></td> <td> <?php $total=0; foreach($operations as $operation){ $product = $operation->getProduct(); $total += $operation->q*$product->price_out; } echo "<b>S/. ".number_format($total,2,",",".")."</b>"; ?> </td> <td style="width:100px;"><center><?php if($sell->is_applied) { echo "<p class='label label-primary'><i class='glyphicon glyphicon-ok'></i> Finalizado</p>"; } else { echo "<p class='label label-warning'><i class='glyphicon glyphicon-time'></i> Pendiente</p>"; } ?> </center></td> <td style="width:180px;"><?php echo $sell->created_at; ?></td> </tr> <?php endforeach; ?> </table> </div> <!-- Para sumar el totla--> <?php $totalgeneral=0; foreach($products as $sell){ $totalgeneral+=$total; echo "<b>S/. ".number_format($total,2,",",".")."</b>"; }?> <br><br><h1>Total: S/. <?php echo number_format($totalgeneral,2,",","."); ?></h1> <!-- Fin --> </div> <div class="btn-group pull-right"> </div> <div class="clearfix"></div> <?php }else{ ?> <div class="jumbotron"> <h2>No hay ventas</h2> <p>No se han agregado ventas.</p> </div> <?php } ?> <br><br><br><br><br><br><br><br><br><br> </div> </div> </section>Votos: 1 - Link respuesta -
Fecha: 13-04-2016 17:09:31 Haz hecho mal de lo q te he dicho haz ubicado mal los código q te dije q agregues primero el inicializador de totalgeneral lo haz hecho en otro lado para ser más específico hazlo afuera del foreach que tienes en la línea 53 haz ese cambio y lo sabes para ver si esta bien y así seguir Votos: 1 - Link respuesta
-
Fecha: 14-04-2016 05:13:30 reempla toda tu linea por esta , en el estara el codigo para la suma total
este es el codigo a reemplazar
<section class="content-header"> <h1><i class='fa fa-shopping-cart'></i> Ventas Realizadas</h1> </section> <section class="content"> <div class="row"> <div class="col-md-12"> <div class="clearfix"></div> <br> <div class="btn-group"> <a class="btn btn-default" href="index.php?view=sells&t=0"> <?php if(isset($_GET["t"]) && $_GET["t"]=="0" ):?><i class="glyphicon glyphicon-ok-sign"></i> <?php endif; ?> Pendientes</a> <a class="btn btn-default" href="index.php?view=sells&t=1"> <?php if(isset($_GET["t"]) && $_GET["t"]=="1" ):?><i class="glyphicon glyphicon-ok-sign"></i> <?php endif; ?> Finalizados</a> <a class="btn btn-default" href="index.php?view=sells"> <?php if(!isset($_GET["t"])):?><i class="glyphicon glyphicon-ok-sign"></i> <?php endif; ?>Todos</a> </div> <?php foreach(Productdata::getAllActive() as $product):?> <?php endforeach ; ?> <?php $page = 1; if(isset($_GET["page"])){ $page=$_GET["page"]; } $limit=10; if(isset($_GET["limit"]) && $_GET["limit"]!="" && $_GET["limit"]!=$limit){ $limit=$_GET["limit"]; } $products=null; if(!isset($_GET["t"])){ $products = SellData::getAll(); }else if(isset($_GET["t"]) && $_GET["t"]=="0" ){ $products = SellData::getAllUnApplied(); } else if(isset($_GET["t"]) && $_GET["t"]=="1" ){ $products = SellData::getAllApplied(); } if(count($products)>0){ ?> <div class="box box-solid box-primary"> <div class="box-header"> <h3 class="box-title"></h3> </div> <div class="box-body table-responsive"> <table class="table table-bordered table-hover datatable"> <thead> <th class="success" class="success">Ver</th> <!--<th class="success">Id</th>--> <th class="success">N° Mesa</th> <th class="success">Mesero</th> <th class="success">Menús</th> <th class="success">Cantidad</th> <th class="success">Total</th> <th class="success">Status</th> <th class="success">Fecha</th> </thead> <?php $totalgeneral= 0; foreach($products as $sell):?> <tr> <td style="width:30px;"><a href="index.php?view=onesell&id=<?php echo $sell->id; ?>" class="btn btn-xs btn-success"><i class="glyphicon glyphicon-eye-open"></i></a></td> <!--<td style="width:60px;">N°<?php echo $sell->id;?></td>--> <td style="width:90px;"><?php echo $sell->item_id;?></td> <td> <?php $mesero = UserData::getById($sell->mesero_id); echo $mesero->name." ".$mesero->lastname; ?> </td> <td><?php echo $product->name ;?></td> <td> <?php $operations = OperationData::getAllProductsBySellId($sell->id); $rx = 0; foreach($operations as $operation){ $rx += $operation->q; } echo $rx; ?></td> <td> <?php $total=0; foreach($operations as $operation){ $product = $operation->getProduct(); $total += $operation->q*$product->price_out; } $totalgeneral+=$total; echo "<b>S/. ".number_format($total,2,",",".")."</b>"; ?> </td> <td style="width:100px;"><center><?php if($sell->is_applied) { echo "<p class='label label-primary'><i class='glyphicon glyphicon-ok'></i> Finalizado</p>"; } else { echo "<p class='label label-warning'><i class='glyphicon glyphicon-time'></i> Pendiente</p>"; } ?> </center></td> <td style="width:180px;"><?php echo $sell->created_at; ?></td> </tr> <?php endforeach; ?> </table> </div> <br><br><h1>Total: S/. <?php echo number_format($totalgeneral,2,",","."); ?></h1> </div> <div class="btn-group pull-right"> </div> <div class="clearfix"></div> <?php }else{ ?> <div class="jumbotron"> <h2>No hay ventas</h2> <p>No se han agregado ventas.</p> </div> <?php } ?> <br><br><br><br><br><br><br><br><br><br> </div> </div> </section>
saludos , pruebalo y me comentas y no olvides de dar por resuelta la pregunta Votos: 1 - Link respuesta -
Fecha: 14-04-2016 13:53:55 Oki gracias por tu apoyo si funciona bien gracias . Votos: 3 - Link respuesta
Para participar activamente de la comunidad primero debes autenticarte, ingresa al sistema.Iniciar Sesión
