recover localstorage array and turn into a sql query in php

Asked

Viewed 470 times

3

I am developing a feature to add and remove favorite properties with localstorage.

//Com esse código eu recupero os itens que foram armazenados em um array no localstorage
           $(function(){
               var favoritos = JSON.parse(localStorage.getItem("favoritos"));
               $(favoritos).each(function(i,v) {
                   $('#favoritos').append("<li>" + v + "</li>");
               });
           });

The return I get from the favorite variable when I run a document.write, are the references of the buildings separated by (,). Ex:3174,3304,3205. This is exactly what I need to mount an SQL query (SELECT * FROM imveis WHERE id_imovel IN ($favorites)).

How to turn recovered localstorage items into something recognized by php to store these items in a variable($favorites)?

1 answer

1

You must make the looping in a result requested by AJAX to a PHP controller and in this request you sent the contents of localStorage.getItem("favoritos") which will be processed by PHP.

In short:

  1. Interface requests controller by listing favorite Ids;
  2. Controller delivers list of items in a JSON from a comma-separated ID STRING
  3. Interface Loops in List

There will be two requests in total: Main document and JSON

  • @I can’t understand your answer.

  • @Igorsilva you understand the MVC structure?

  • @gpudo, yes, layered development.

  • So I’ll try to explain it another way: Layer V collects the favorites list and sends it to C by ajax. Receives from C a JSON that is processed and displayed in V

Browser other questions tagged

You are not signed in. Login or sign up in order to post.