Ver Mensaje Individual
  #4 (permalink)  
Antiguo 22/08/2011, 09:28
marx-pola
 
Fecha de Ingreso: mayo-2002
Ubicación: Capital Federal
Mensajes: 630
Antigüedad: 22 años
Puntos: 1
Respuesta: Cómo se suma el total de los valores de una columna?

Hola y gracias por responder pero no me funcionó, quizás porque no lo estoy utilizando bien. Les paso el código para que vean como está armado:

Código PHP:
<?php session_start();
error_reporting(E_ALL);
@
ini_set('display_errors''1');
if(isset(
$_SESSION['carro']))
$carro=$_SESSION['carro'];else $carro=false;

if(
$carro){
?>

<table width="880" border="0" cellspacing="0" cellpadding="0" align="center">
  <tr bgcolor="#333333" class="tit"> 
    <td width="64" height="20">Producto</td>
    <td width="82" height="20">Precio</td>
    <td height="20" colspan="2" align="center">Cantidad de Unidades</td>
    <td width="70" height="20" align="center">Borrar</td>
    <td width="100" height="20" align="center">Actualizar</td>
  </tr>
  <?php
  $color
=array("#ffffff","#F0F0F0");
  
$contador=0;
  
$suma=0;
   foreach(
$carro as $k => $v){
   
$subto=$v['cantidad']*$v['precio'];
   
$suma=$suma+$subto;
   
$contador++;
    
?>
  <form name="a<?php echo $v['identificador'?>" method="post" action="carro_actualiza.php?<?php echo SID ?>" id="a<?php echo $v['identificador'?>">
    <tr bgcolor="<?php echo $color[$contador%2]; ?>" class='prod'> 
      <td height="20"><?php echo $v['id_articulo'?></td>
      <td height="20"><?php echo $v['precio'?></td>
      <td width="45" height="20" align="center"><?php echo $v['cantidad'?></td>
      <td width="79" height="20" align="center"> 
        <input name="cantidad" type="text" class="prod" id="cantidad" value="<?php echo $v['cantidad'?>" size="6">
        <input name="id_articulo" type="hidden" id="id_articulo" value="<?php echo $v['id_articulo'?>"> </td>
      <td height="20" align="center"><a href="carro_borrar.php?<?php echo SID ?>&id_articulo=<?php echo $v['id_articulo'?>"><img src="trash.gif" width="12" height="14" border="0"></a></td>
      <td height="20" align="center"> 
        <input name="imageField" type="image" src="actualizar.gif" width="20" height="20" border="0"></td>
  </tr></form>
  <?php }?>
</table>
</td>
  </tr>
  <tr>
    <td height="30" align="center" valign="bottom">&nbsp;</td>
  </tr>
  <tr>
    <td height="53" align="center" valign="bottom"><div align="center"><span class="prod">Total de Artículos: <?php echo count($carro); ?></span> 
</div><br>
<div align="center"><span class="prod">Total: $<?php echo number_format($suma,2); ?></span> 
</div><br>
<div align="center"><span class="prod">Continuar la selección de productos</span> 
  <a href="../index.php?<?php echo SID;?>"><img src="continuar.gif" width="13" height="13" border="0"></a></div></td>
  </tr>
  <tr>
    <td align="center"><?php }else{ ?>
      <p align="center"> <span class="prod">No hay productos seleccionados</span> <a href="../index.php?<?php echo SID;?>"><img src="continuar.gif" width="13" height="13" border="0"></a>
        <?php }?>
    </p></td>
  </tr>
  <tr>
    <td align="center">
     </td>
  </tr>
</table>
La línea "Total de Artículos: <?php echo count($carro); ?>" me cuenta solo las filas/artículos ingresados pero no la cantidad ingresada en cada uno de ellos. Por eso puse este ejemplo:
Producto----- Cantidad
lamparitas--- 1
faroles------ 3
lamparas---- 5
...
Productos: 3 - Total/Cantidad: ?

Además encontré otro error. No me está sumando los decimales y la suma me devuelve números enteros. Lo que noté es que en la bdd los números están cargados con comas (51,10) y esta línea "Total: $<?php echo number_format($suma,2); ?>" me devuelve el número con punto (51.00).

Si pueden darme una mano se los agradecería. Solo me falta solucionar esto para tener el carrito completo.

Muchas gracias. Marx.
__________________
"Todo lo que somos es el resultado de lo que hemos pensado". Budda.