Show results mysql indifferent to shopping cart

1

I am creating a shopping cart system, which works very well, now the problem comes where it shows the results of the car ( cesta.php ), I want to show results indifferent to what the user has inserted into the car, for so to facilitate if you want to buy more products.

Let's say if we add 3 products to buy, with id1 , id2 , id3 .

And in the Database ( productos ) there are 6 records.

Well I should show id4 , id5 , id6 , because as I mentioned before, the user has already inserted to buy the id1 , id2 , id3 .

The shopping cart I keep in a session as follows:

The shopping cart was created on the page card.php

 //Carro de la compra

//Si esta definida la ID obtenido por URL
if (isset($_GET['articulo'])) {

    $id_tutorial = $_GET['articulo'];//Obtenemos el ID del tutorial añadido, para poder acer comprobaciones a mostrar otros resultados.

    //Si esta definido la sesion carro -> es decir si ay algun articulo comprado
    if (isset($_SESSION['carrito'])) {

        $arreglo = $_SESSION['carrito'];
        $encontro = false;      

        for ($i=0; $i<count($arreglo); $i++) { 

            if ($arreglo[$i]['Id'] == $_GET['articulo']) {
                $encontro = true;               
            }
        }

        if ($encontro == false) {


            $titulo = "";
            $precio = 0;
            $precioUSD = 0;
            $icon = "";

            $stmt = $c->prepare("SELECT titulo,precio,icon,id_autor FROM tutoriales WHERE page=? and status=1");
            $stmt->bind_param("i",$_GET['articulo']);
            $stmt->execute();
            $stmt->store_result();
            if ($stmt->num_rows > 0) {
                $stmt->bind_result($titulo,$precio,$icon,$id_autor);
                while ($stmt->fetch()) {

                    //Sentencia prepare -> autor proyecto
                    $stmtN = $c->prepare("SELECT autor FROM autor WHERE id_autor=?");           
                    $stmtN->bind_param("i", $id_autor);         
                    $stmtN->execute();          
                    $stmtN->bind_result($autor);            
                    $stmtN->fetch();            
                    $stmtN->close();

                    $datosnuevos = array('Id' => $_GET['articulo'], 'Titulo' => $titulo, 'Precio' => $precio, 'Icon' => $icon, 'Cantidad' => 1 );

                    /*
                        #Si se utiliza array_push() para añadir un solo elemento al array, es mejor utilizar $array[] = ya que de esta forma no existe la sobrecarga de llamar a una función. 
                    */

                    //array_push($arreglo, $datosnuevos); 
                    $arreglo[] = $datosnuevos;
                    $_SESSION['carrito'] = $arreglo;

                    $data = $_SESSION['carrito'];
                    $value_carrito = count($data);
                    $_SESSION['compras'] = $value_carrito;

                } $stmt->close();


            } else {
                $stmt->close();
            }
        }   

    } else { //Caso falso añadimos primer articulo al carro

        $titulo = "";
        $precio = 0;
        $precioUSD = 0;
        $icon = "";

        $stmt = $c->prepare("SELECT titulo,precio,icon,id_autor FROM tutoriales WHERE page=? and status=1");
        $stmt->bind_param("i",$_GET['articulo']);
        $stmt->execute();
        $stmt->store_result();
        if ($stmt->num_rows > 0) {
            $stmt->bind_result($titulo,$precio,$icon,$id_autor);
            while ($stmt->fetch()) {

                //Sentencia prepare -> autor proyecto
                $stmtN = $c->prepare("SELECT autor FROM autor WHERE id_autor=?");           
                $stmtN->bind_param("i", $id_autor);         
                $stmtN->execute();          
                $stmtN->bind_result($autor);            
                $stmtN->fetch();            
                $stmtN->close();

            } $stmt->close();

        } else {
            $stmt->close();
        }       

            $arreglo[] = array('Id' => $_GET['articulo'], 'Titulo' => $titulo, 'Precio' => $precio, 'Icon' => $icon, 'Cantidad' => 1 );

            $_SESSION['carrito'] = $arreglo;

            $data = $_SESSION['carrito'];
            $value_carrito = count($data);
            $_SESSION['compras'] = $value_carrito;

            //echo "<script>window.location.reload();</script>";


    }

} 
    
asked by D.Bulten 07.10.2016 в 12:50
source

1 answer

0

I will autocontestarme my prejunta, since they helped me in Stack Overflow .

This example will give an initial advantage on how to build the query SELECT :

Suppose $_SESSION['carrito'] contains the products that have already been purchased by the customer and $_SESSION[ 'compras'] keeps the count of the total number of products purchased, the query SELECT to recover the remaining products would be like this:

SELECT id_tutorial,page,titulo,info,icon,bg,vdo,url,precio,duracion,status,id_nivel,id_estado 
FROM tutoriales 
WHERE status = 1 AND id_tutorial NOT IN (
    SELECT id_tutorial 
    FROM tutoriales 
    WHERE id_tutorial IN (" . rtrim(str_repeat("?,", $_SESSION['compras']), ",") . "
    )
)

And the code should be like this:

$param = array(); 
$paramType = '';
for($i = 0; $i < $_SESSION['compras']; ++$i){
    switch(gettype($_SESSION['carrito'][$i]['Id'])){
        case 'boolean':
        case 'NULL':
        case 'integer':
            $paramType .= 'i';
            break;
        case 'double':
            $paramType .= 'd';
            break;
        case 'string':
            $paramType .= 's';
            break;
        default:
            $paramType .= 'b';
    }
    $param[] = &$_SESSION['carrito'][$i]['Id'];
}
array_unshift($param, $paramType);
call_user_func_array(array($stmt, 'bind_param'), $param);

$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
    $result->bind_result($id_tutorial,$page,$titulo,$info,$icon,$bg,$vdo,$url,$precio,$duracion,$status,$id_nivel,$id_estado);
    while ($result->fetch()){

        // your code          

    }
} else {
    $stmt->close();
}    

Implement the example on my server and it works correctly. I also leave the entry of Stack Overflow , where I answered:

answered by 11.10.2016 / 01:44
source