Reply of the full query of line breaks

Asked

Viewed 49 times

0

Hi, I have a problem with the query answer I made to db is full of \n and \ . Does anyone know what the problem might be? Thank you.

model function, query a db in search of products added to cart by a certain user

function _get_cart_by_user($id)
{
    $this->db->where('user_id',$id);
    $query = $this->db->get('cart');
    return $query;

}

controller function, only receives the model result and does re-turn from it

    public function get_cart()
    {
        $id=$this->input->post('user_id', TRUE);

        $this->load->model('cart_model');
        $query=$this->cart_model->_get_cart_by_user($id);   

        $result= $query->result();


        var_dump($result);


    }

AJAX request, which should receive the return of the controller and show in the browser console

    function addItemToCart(user_id) {

                $.ajax({
                    url: BASE_URL + 'cart/get_cart',
                    type: 'POST',
                    data: {
                        'user_id': user_id
                    },
                    success: function (data) {
                        console.log(JSON.stringify(data))
                    },
                    error: function () {
                        alert("error");
                    }
                });

this is the result of var_dump of the controller.

"array(2) { n [0]=> n >Object(stdClass)#24 (6) { n >[ "id "]=> n string(2) > "16 " n [ "user_id "]=> n >string(1) "5 " n
[\"item_id "]=> n >string(1) "2 " n
[\"title "]=> n string(4) > "Buddha " n [ "price "]=> n >string(4) "9.99 " n
[\"image "]=> n string(0) > " n } n [1]=> n >Object(stdClass)#25 (6) { n >[ "id "]=> n string(2) > "17 " n [ "user_id "]=> n >string(1) "5 " n
[\"item_id "]=> n >string(1) "3 " n
[\"title "]=> n string(12) > "budaoriginal " n
[\"price "]=> n string(4) > "9.99 " n [ "image "]=> n >string(0) " " n } n} n"

Can anyone tell me what these are \ and \n, I also tried to make json_enconde (did echo of json_encode and appears the \ ) and move to AJAX but when I do the Response console it just appears "".

  • can use a Regex to remove the escape characters \n

1 answer

0

According to the ASCII table \n means new line (new line), and how the browser interprets the html to mount the DOM \n does not have any effect directly, but fortunately there is an html tag <pre></pre> that has this function of formatting these lines breaks and blank spaces.

In your case just use as follows.

echo "<pre>";
var_dump($result)
echo "</pre>";

Finally the \" actually has the function of indicating that that quote is part of the content of the code or information and is not a quotation mark closing.

To be clearer follow the example:

"Abrindo aspas nessa linha
vou ignorar essas aspas \"faço parte do conteúdo\"
Agora nessa linha vou fechar aspas"

I hope I’ve helped you and made it clear.

  • Then make json_encode of the result and return it to Ajax, then make a console.log(JSON.stringify(data)). This should show a string with all information, but only shows empty quotes.

  • I don’t think it’s a good idea to generate an html tag in php for an ajax return, this should be formatted in client-side javascript

  • What I tried was result() and result_array() then returned json_encode from the result. In Ajax Success I made the log console of JSON.stringify, but only appeared empty quotes. And when I did in the controller the result var_dump and result_array appeared such n (new line) that I think n be normal.

  • I realized what I was doing wrong I don’t need to do a Return to provide date to Ajax. Just echo the result

Browser other questions tagged

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