Reputation: 2694
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
Upvotes: 0
Views: 295
Reputation: 5967
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.
Upvotes: 1
Reputation: 4211
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
Upvotes: 0