Session php for book cart

1

Good morning. I am programming a book cart from a database of books using php sessions. There is very little left for it to work but I'm stuck because the cart does not pick up the added books. I do not put the complete forms only the shipments so they know the data I send.

The scheme is as follows

session not started -> search for books - > add book (the_id) - > register email - > continue search

session started -> search for books - > add book (the_id) - > stay in search results

The user does a search and clicks add book, if it is not registered it asks to add email to log in, and sends book id (works)

$ the_id is the identifier of the book

<input type="text" placeholder="Tu email" name="usuario" required>
<input type="hidden" name="libro" value="<?php echo the_ID() ?>">  
<input type="Submit" value="Registrarse"

add book with session started, send book id

$ the_id is the identifier of the book

<input type="hidden" name="libro" value="<?php echo the_ID() ?>"> 
<input type="submit"  value="A&ntilde;adir a Mis Registros"

I open session

session_start();

I save user email as identifier (it works)

if(isset($_POST['usuario'])){ 
$usuario = $_POST['usuario']; 
$_SESSION['usuario'] = $usuario; 
}

If there is a user in session, he / she welcomes you (works)

if($_SESSION['usuario'])  { echo "Bienvenido: " . $_SESSION['usuario'] . "";}

I set my session name

if(!isset($_SESSION["random"])) {
$_SESSION["random"] = rand(1,10); 
}
$randomNumber = $_SESSION["random"];

I retrieve and save the id of the book. (name book in the form)

if (isset($_POST['libro'])) { 
$id = $_POST['libro']; 
$_SESSION['libro'] = $id; 
}

See My records. If there is a session with added books, show them

if($_SESSION["random"]){ 
echo $id;
] else {
echo 'no hay registros';
}

The problem is that the cart is empty and therefore shows that there are no records, which makes me suppose that if the session is recorded but the id of the books is not collected. Since I have the books I will send the records by email but I still can not move towards it because the register cart is empty !!

Someone who can guide me ...? thanks!

    
asked by Texas 29.06.2018 в 06:16
source

1 answer

0

First of all, you do not have to do this:

if(!isset($_SESSION["random"])) {
    $_SESSION["random"] = rand(1,10); 
}
$randomNumber = $_SESSION["random"];

I understand that you do this to put a reference name to the session, but the session is unique for each user, it is not necessary to assign it an id.

And in this piece of code, what you do is ... that if there is a "random" value in the session (if it exists), show $ id, which you have defined above TO SAVE THE BOOK , with which that $ id is empty, you should use " $ _ SESSION ['book'] " instead, not $ id.

if($_SESSION["random"]){ 
    echo $id;
] else {
    echo 'no hay registros';
}

On the other hand, apart from not recovering well the saved book data (id), you are also keeping the book wrong.

if (isset($_POST['libro'])) { 
    $id = $_POST['libro']; 
    $_SESSION['libro'] = $id; 
 }

This sentence is correct, it works but only to save a book, if you realize, when there is a book "1", for example and you want to save the book "5", it will repeat that sentence, with which $ _SESSION ['book'] will stop being 1 and will be 5, with which you have just programmed a "cart" of books where you can only store a book.

What you should do is to save an array of ids, so make a cart that keeps several books, check if the book is already in the array and if not, save it. I'll give you an example cmabiando your code:

//Antes de nada, debemos inicializar un array vacio, al loguearse un usuario
$_SESSION['libro'] = array();

//Y ya cada vez que enviemos un libro hacemos lo siguiente
if (isset($_POST['libro'])) {
    $id = $_POST['libro'];//Recojo la id

    //Comprobamos si esa id existe en nuestro array, si existe devuelve la 
    //posicion en la que esta y si no, un null, asi que mientras sea 
    //null, guardamos libro
    if(array_search($id, $_SESSION['libro']) == null){
        $_SESSION['libro'][] = $id;//Esta sentencia añade al final del array la nueva id
    }else{
        echo 'el libro ya esta en el carrito';
    }
 }

This way, we already have a real cart, that keeps several books, not just one as you had before, now we just need to pick it up.

//Cuando quieras recoger los datos del carrito, hacemos un bucle for
//Y nos recogera cada id que haya en el array y la pintamos (o lo que sea)
for($i = 0; $i < sizeof($_SESSION['libro']); $i++){
    echo "Libro: " . $_SESSION['libro'][$i];
}

I hope you understand and help you:)

    
answered by 29.06.2018 / 07:43
source