0

I met some trouble in my ajax request.

In fact I do not know how to ou many records.

I've tried this :
    $rqt = "SELECT a,b,c from table";
    $res = mysql_query($rqt);
    while ($data = mysql_fetch_assoc($res)):
    $objet = $data;
    endwhile;
//requettage
    return $objet;

but it out to php only one rows even if I have many records in database.

Second trouble is for display that records.

I've tried this in jquery

$.ajax({
                    type: "POST",
                    url: "requetes_ajax/fetch_debours_detail.json.php",
                    data: "groupe="+groupe_debours,
                    success: function(data){
                    $('.remove').remove()
                    console.log(data);
                    $.each(data, function (key, value) {
                        $('#fetchdebours tr:last').after('<tr class="remove"><td>'+data.libelle_debours+'</td><td>'+data.date+'</td><td>'+data.debours_montant_ht_tva+'</td><td>'+data.debours_montant_ht_no_tva+'</td><td>'+data.debours_montant_ttc+'</td></tr>')
                        //alert(key + ': ' + value);
                    });
                    }
                });

for that things the trouble I have is that instead of displaying all records in one row, it fill all items horizontally and also vertically so I have as many rows as I have column.

Anykind of help will be much appeciated

2 Answers 2

1

In the while loop you are forever replacing the $objet variable with the latest row, so in the end the latest row is all what you get.

To build an array, use this code:

$objet = array();
while ($data = mysql_fetch_assoc($res)):
    $objet[] = $data;
endwhile;

To pass the data as JSON you should first declare dataType: "json" in your $.ajax call. The second step is to actually output the PHP array as JSON. Try the following code:

$json = json_encode($objet);
print($json);

Since now you are passing an entire array instead of just one object, your display code needs a small adjustment:

$.each(data, function (k, obj) {
    $('#fetchdebours tr:last').after('<tr class="remove"><td>' + obj.libelle_debours + '</td> ... </tr>')
});

I haven't tested the code, use with caution.

Sign up to request clarification or add additional context in comments.

1 Comment

I have encoded the json to output I just had some trouble with the php loop to populate the array and with the each of jquery now it works fine because of you Thank you so much for your help
0

From the syntax of you ajax request, you want json to comeback as a response, try this:

$rqt = "SELECT a,b,c from table";
$res = mysql_query($rqt);
$data = mysql_fetch_assoc($res);
return json_encode($data);

see json_encode

requires PHP >= 5.2.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.