0

I have a database table with some rows that I want to fetch using PHP and then encode them using JSON.

Currently, my database structure is the following:

idcomponente | quantidade

After fetching the values in PHP, I want to know how can I encode them using JSON (with multiple rows, using the same names) so I can read them using jQuery.post().

$.post('test.php',{id:id},function(data){
    //READ data HERE
});

Thanks in advance

Edit:

So far, I made this:

$.post('edit/producomponentes.php',{id:id},function(data){
    console.log(data);
});


Logs this:

[Object { componente="1", quantidade="2"}, Object { componente="3", quantidade="3"}]

Now how can I go through each row and fetch their properties? (data.componente, data.quantidade)

4 Answers 4

3

Here I try to give you some idea.

Make associative array in PHP using key => value pair like

$data = array('id' => 1, 'quat' => 10, 'id' => 2, 'quat' => 20)

and then send it as json_encode() like

header('Content-type: application/json');  // don't miss it
echo json_encode(array('data' => $data));

In jQuery

$.post('edit/producomponentes.php',{id:id},function(response){
    //READ data HERE.
    console.log(response.data); // you will get a json Object
}, 'json');
     ^-------- set dataType as json, then you don't any extra parse effort

According to edit

 $.post('edit/producomponentes.php',{id:id},function(data){
        $.each(data, function(index, value) {
           console.log(value.componente);
           console.log(value.quantidade);
        });
    }, 'json');
Sign up to request clarification or add additional context in comments.

2 Comments

Dont forget to set the correct headers in the php file to serve JSON :)
Thanks for this. I already have the headers, now my only question is how to loop through the rows (see edited post).
2

In test.php

$id = $_POST['id']; // if is 'id' passed to fetch one row
$db = new mysqli(HOST,USER,PASS,DB_NAME);
$q = "SELECT * FROM table WHERE id='$id'";
$result = $db->query($q);

EDIT proper headers to for json responses

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
echo json_encode($result->fetch_all(MYSQLI_ASSOC));
$result->close();
$db->close();

In JavaScript

$.post('test.php',{id:id},function(data){
    console.log(data); // data is already a js object
},'json'); // specify the data type you expect

3 Comments

PHP side is complete, now I need to loop through the rows in jQuery (see edited post). Thanks
is the header() Cache-Control and Expires really necessary?
not really but prevent browser from caching responses which is something you dont want with ajax responses
1

You could place each row in an array and run json_encode() on the final array to get one big JSON object.

Reference - http://php.net/manual/en/function.json-encode.php

Comments

1

use JSON.parse to get the data as an object like

var object1 = JSON.parse(data);
alert(object1.property1);

for encoding on php, use

json_encode($result); //assuming you're getting a result set.

to encode your result set and

echo json_encode($result);

to print it to document body, so that you can fetch using jquery on result data

documentation is here

you'll need to test it many times, be patient and chech your values with echo on the php side and alert() with the client side.

1 Comment

My question is (using PHP), how to encode multiple rows with the same name correctly!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.