<?php
session_start();
include './conexion.php';
if(isset($_SESSION['carrito'])){
if(isset($_GET['id'])){
$arreglo=$_SESSION['carrito'];
$encontro=false;
$numero=0;
for($i=0;$i<count($arreglo);$i++){
if($arreglo[$i]['Id']==$_GET['id']){
$encontro=true;
$numero=$i;
}
}
if($encontro==true){
$arreglo[$numero]['Cantidad']=$arreglo[$numero]['Cantidad']+1;
$_SESSION['carrito']=$arreglo;
}else{
$nombre="";
$precio=0;
$imagen="";
$link = mysqli_connect("localhost","root", "","carrito");
mysqli_select_db($link, "carrito");
$tildes = $link->query("SET NAMES 'utf8'"); //Para que se muestren las tildes correctamente
$re = mysqli_query($link, "select * from productos where id=".$_GET['id'])or die(mysqli_error()
);
while ($f=mysqli_fetch_array($re)) {
$nombre=$f['nombre'];
$precio=$f['precio'];
$imagen=$f['imagen'];
}
$datosNuevos=array('Id'=>$_GET['id'],
'Nombre'=>$nombre,
'Precio'=>$precio,
'Imagen'=>$imagen,
'Cantidad'=>1);
array_push($arreglo, $datosNuevos);
$_SESSION['carrito']=$arreglo;
}
}
}else{
if(isset($_GET['id'])){
$nombre="";
$precio=0;
$imagen="";
$link = mysqli_connect("localhost","root", "","carrito");
mysqli_select_db($link, "carrito");
$tildes = $link->query("SET NAMES 'utf8'"); //Para que se muestren las tildes correctamente
$re = mysqli_query($link, "select * from productos where id=".$_GET['id'])or die(mysqli_error());
while ($f=mysqli_fetch_array($re)) {
$nombre=$f['nombre'];
$precio=$f['precio'];
$imagen=$f['imagen'];
}
$arreglo[]=array('Id'=>$_GET['id'],
'Nombre'=>$nombre,
'Precio'=>$precio,
'Imagen'=>$imagen,
'Cantidad'=>1);
$_SESSION['carrito']=$arreglo;
}
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8"/>
<title>Panel de Administración</title>
<link rel="stylesheet" type="text/css" href="./css/estilos.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript" href="./js/scripts.js"></script>
</head>
<body>
<header>
<img src="./imagenes/logo.png" id="logo">
<a href="./carritodecompras.php" title="ver carrito de compras">
<img src="./imagenes/carrito.png">
</a>
</header>
<section>
<nav class="menu2">
<menu>
<li><a href="./">Inicio</a></li>
<li><a href="#" class="selected">Admin</a></li>
<li><a href="./admin/agregarproducto.php" >Agregar</a></li>
<li><a href="./admin/modificar.php" >Modificar</a></li>
<li><a href="./login/cerrar.php">Salir</a></li>
</menu>
</nav>
<section>
<?php
$total=0;
if(isset($_SESSION['carrito'])){
$datos=$_SESSION['carrito'];
$total=0;
for($i=0;$i<count($datos);$i++){
?>
<div class="producto">
<center>
<img src="./productos/<?php echo $datos[$i]['Imagen'];?>"><br>
<span ><?php echo $datos[$i]['Nombre'];?></span><br>
<span>Precio: <?php echo $datos[$i]['Precio'];?></span><br>
<span>Cantidad:
<input type="text" value="<?php echo $datos[$i]['Cantidad'];?>"
data-precio="<?php echo $datos[$i]['Precio'];?>"
data-id="<?php echo $datos[$i]['Id'];?>"
class="cantidad">
</span><br>
<span class="subtotal">Subtotal:<?php echo $datos[$i]['Cantidad']*$datos[$i]['Precio'];?></span><br>
<a href="#" class="eliminar" data-id="<?php echo $datos[$i]['Id']?>">Eliminar</a>
</center>
</div>
<?php
$total=($datos[$i]['Cantidad']*$datos[$i]['Precio'])+$total;
}
}else{
echo '<center><h2>No has añadido ningun producto</h2></center>';
}
echo '<center><h2 id="total">Total: '.$total.'</h2></center>';
if($total!=0){
echo '<center><a href="./compras/compras.php" class="aceptar">Comprar</a></center>;';
}
?>
<center><a href="./">Ver catalogo</a></center>
</section>
</body>
</html>