I am making a shopping cart, I have the product table formed, with the values that I am adding in a session variable .. the problem is that every time I add a product, it sends me the beginning of the page, and I would like that either send me to each div, or just refresh the cart table (which is below the product at this time)
This is the home page, where the two tables are:
<?php
session_start();
include_once("conectar.php");
$link = Conectarse();
if(isset($_SESSION['carrito']))
{
$carrito=$_SESSION['carrito'];
}
else
{
$carrito=false;
}
?>
<div class="col-md-8 col-md-offset-3">
<table class='Table table-striped' style="float:left">
<thead>
<tr>;
<th scope="col">Codigo</th>
<th scope="col">Producto</th>
<th scope="col">Comentario</th>
<th scope="col">Precio</th>
<th scope="col">Agregar al carro</th>
</tr>
</thead>
<tbody>
<?php
include ("carrito.php");
foreach ($link->query("SELECT * FROM productos ORDER BY codigo") as $row)
{
echo "<tr>";
echo "<div id='".$row["codigo"]."'>";
echo "<th scope='row'>".$row["codigo"]."</th>";
$cod=$row["codigo"];
echo "<td>".$row["producto"]."</td>";
echo "<td>".$row["descripcion"]."</td>";
echo "<td>$".$row["precio"]."</td>";
echo "<td><a href='agregarproducto.php?codigo=$cod'><i class='fa fa-cart-plus fa-3x' aria-hidden='true'></i> </td>";
echo "</div>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
<table class='Table table-striped'>
<thread>
<tr>
<th scope="col">Producto</th>
<th scope="col">Cantidad</th>
<th scope="col">Precio Unitario</th>
<th scope="col">Precio total</th>
<th scope="col">Eliminar del carro</th>
</tr>
</thread>
<tbody>
<?php
$carrito=$_SESSION["carrito"];
$total=0;
foreach ($carrito as $car)
{
echo "<tr>";
echo "<th scope='row'>".$car["producto"]."</th>";
echo "<td>".$car["cantidad"]."</td>";
echo "<td>$".$car["precio"]."</td>";
echo "<td>$".$car["precio"]*$car["cantidad"]."</td>";
$total=$total+($car["precio"]*$car["cantidad"]);
echo "<td><i class='fa fa-cart-arrow-down fa-3x' aria-hidden='true'></i> </td>";
echo "</tr>";
}
echo "<tr>";
echo "<th scope='col'>Precio total: $".$total."</th>";
echo "</tr>";
?>
</tbody>
</table>
And this is the cart class, to add each one (through a page that only instantiates that class):
<?php
class carrito
{
public function agregarProducto()
{
session_start();
include_once ("conectar.php");
$link = Conectarse();
$id=$_GET["codigo"];
if(isset($_SESSION['carrito']))
{
$carrito=$_SESSION['carrito'];
}
$carrito=$_SESSION['carrito'];
$cantidad=$carrito[$id]['cantidad']+1;
$consulta=("select * from productos where codigo=$id");
$pro=$link->query($consulta);
$fila=mysqli_fetch_array($pro);
$carrito[$id]=array($id,'cantidad'=>$cantidad,'producto'=>$fila['producto'],'precio'=>$fila['precio'],'id'=>$id);
$_SESSION['carrito']=$carrito;
header("location:inicio.php");
}
}
?>