Reputation: 85
I'll be brief... I'm about to jump
not all results are working
in the document header I am including jquery-1.7.2.min.js
-
my query:
"SELECT tableA.columns_xyz..., tableB.columns_xyz..., tableC.columns_xyz
FROM tableB
JOIN tableC ON tableB.x = tableC.x
JOIN tableA ON tableB.x = tableA.x
WHERE tableA.x = '$query_value'
GROUP BY tableA.y
ORDER BY tableA.y DESC
LIMIT 1";
PROBLEM 1:
change LIMIT 1
to LIMIT 5
or anything >1 and the query executes fine in phpmyadmin but not via ajax/json/php
the php:
$sql = above...
$result = $mysqli->query($sql);
while($row = mysqli_fetch_array($result)) {
echo json_encode(array($row['x'],$row['y'],....$row['z']));
$mysqli->close();
the javascript:
$.ajax ({
url: 'script.php',
type: "post",
dataType: "json",
data: {value : $("#searchbox").val()},
success: function(data) {
var a = parseFloat(data[0]);
var b = parseFloat(data[1]);
var c = data[2];
document.getElementById("blah1").innerHTML = a;
document.getElementById("blah2").innerHTML = b;
document.getElementById("blah3").innerHTML = c;
}
});
the html:
<td><span id="blah1"></span></td>
<td><span id="blah2"></span></td>
<td><span id="blah3"></span></td>
PROBLEM 2:
some elements are printing to page, some aren't. what am I doing wrong ?
PROBLEM 3:
I know I should be able to express document.getElementById("blah1").innerHTML;
as $('#blah1');
but $('#blah1');
is just not working... I don't know why...
Upvotes: 1
Views: 1054
Reputation: 23297
Store each row in an array then use json_encode once:
$store = array();
while($row = mysqli_fetch_array($result)) {
$store[] = array('x'=>$row['x'], 'y'=>$row['y'], 'z'=>$row['z']);
}
echo json_encode($store);
Then you can use JSON.parse
on the client-side:
var store = JSON.parse(data)
And then just loop through that:
for(var x in store){
$('#blah1').html(store[x]['x']);
}
You're already using jquery so why don't you make full use of it by converting this:
document.getElementById("blah1");
To its jquery version:
$('#blah1');
Just make sure that those elements are unique since you're using ID's
Upvotes: 1
Reputation: 7905
Should you not be creating one object then json encoding the whole lot, rather than json encoding each row seperatley?
Im not great with sql but having a LIMIT greater than 1 means ur expecting more than one row back (potentially), correct?
If so then you are essentially returning multiple json responses in one response which I dont think you should be doing.
Upvotes: 1