Incremental variables to add

0

The thing is that I need the variable% co_of% which is where the total price of each product line is stored, something like $precioinicial , $precioinicial1 , for later where I put the total without VAT , make the sum of these variables.

I have the following code snippet:

<table class="table table-bordered">
<thead>
	<th>Cantidad</th>
	<th>Producto</th>
	<th>Precio Unitario</th>
	<th>Total</th>
	<th></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>
<th><?php echo $c["q"];?></th>
	<td><?php echo $r->name;?></td>
	<td><?php echo $r->price; ?> € </td>
	<td><?php $precioinicial = $c["q"]*$r->price;
	echo $precioinicial;?> € </td>
	
	
	
	<td style="width:260px;">
	<?php
	$found = false;
	foreach ($_SESSION["cart"] as $c) { if($c["product_id"]==$r->id){ $found=true; break; }}
	?>
		<a href="php/delfromcart.php?id=<?php echo $c["product_id"];?>" class="btn btn-danger">Eliminar</a>
	</td>
	</tr>

<?php endforeach; ?>
	<tr>
	<td>
	</td>
	<td>
	</td>
	<td>
	</td>
	<td>
	Total sin IVA<br>
	<?php echo $precioinicial;?> € 
	</td>
</tr>
</table>
    
asked by Joaquin 31.12.2017 в 17:20
source

1 answer

1

Transform $precioinicial in an array:

foreach($_SESSION["cart"] as $i => $c):
...
<td><?php $precioinicial[$i] = $c["q"]*$r->price;   echo $precioinicial[$i]; ?> € </td>

And to get the sum:

<?php echo array_sum($precioinicial); ?> €
    
answered by 31.12.2017 / 17:40
source