Ver Mensaje Individual
  #14 (permalink)  
Antiguo 07/03/2015, 03:04
darioPHP
 
Fecha de Ingreso: marzo-2015
Mensajes: 15
Antigüedad: 9 años, 2 meses
Puntos: 3
Respuesta: Carrito compras Consultar 2 tablas igual id

el truco al programar es buscar siempre la solucion mas simple. Las tablas duplicadas te van a dar un dolor de cabeza al pedo cuando quieras buscar, quitar o agregar datos a traves del "Id".
¿Porque no agregar una nueva columna para categorias en la misma tabla?

aca limpie un toque el codigo

Código PHP:
session_start();
include 
'./conexion.php';

$_GET['id'] = isset($_GET['id'])? $_GET['id'] : '';
$_SESSION['carrito'] = isset($_SESSION['carrito'])? $_SESSION['carrito'] : '';

if(
$_SESSION['carrito'] && $_GET['id']):

    if(!
agregarCantidad())
        
array_push($_SESSION['carrito'], datosNuevos());

elseif(
$_GET['id']):

    
$_SESSION['carrito'] = datosNuevos();

endif;


function 
datosNuevos(){
    
    
$respuesta mysql_query("select * from TABLA1 where id=".$_GET['id']);
    
$row mysql_fetch_array($respuesta);

    
$datosNuevos = [];
    
$datosNuevos["Id"] = $_GET['id'],
    
$datosNuevos["Nombre"] = $row['nombre'],
    
$datosNuevos["Precio"] = $row['precio'],
    
$datosNuevos["Imagen"] = $row['imagen'],
    
$datosNuevos["Cantidad"] = 1

    
return $datosNuevos;
}

function 
agregarCantidad(){

    foreach(
$_SESSION['carrito'] as $datos){
        if(
$datos['Id'] == $_GET['id']){

            
$datos['Cantidad']++;
            return 
true;

        }
    }
    return 
false;