Okay I'm trying to get this to work server side with what I have. help?
Okay I'm trying to get this to work server side with what I have. help?
I have a php page that already has a database connection.
this needs to be manual. Because I'm not calling a table.
I'm calling a stored procedure.
So what I did was, I echoed the results in a string that looks exactly like the JSON representation in the serverside example on here.
Here's what my loop and output look like.
When I call this page, Datatables says it isn't JSON.
How I can make this page Appear to be JSON?
I don't want to create another page to spit the results into. Because multiple people may be using this at the same time. If the results from someone and another person update that file, then person A may get person B's results.
--------(code)not sure why only some of it is showing up as "code"(there's some bellow it)----------
$sum = 0;
$records = "";
while ($row = mysqli_fetch_array($result)){
$sum += $row[0];
$records = $records. ' [
"'.$row[0].'",
"'.$row[1].'",
"'.( $row[2] == "" ? "Not Specified" : $row[2]).'",
"'.( $row[3] == "" ? "Not Specified" : $row[3]).'",
"'.$row[4].'",
"'.$row[5].'",
"<a target=\"_blank\" href=\"http://maps.google.com/?q='.$row[8].', '.$row[5].', '.$row[4].'\"><img width=\"22px\" src=\"http://i2techsolutions.com/greenbook/visistat/library/images/googlei22w.png\"></a>",
"'.$row[7].'"
],';
}
$records = substr($records, 0, -1);
$json = '{
"draw": 1,
"recordsTotal": '.$sum.',
"recordsFiltered": 57,
"data": [';
$json = $json.$records;
$json = $json.']
}';
}
echo JSON.parse($json);
Replies
i tried adding
header('Content-Type: application/json');
before the echo and changing it to
echo json_encode($json);
but it says same error
If you are going to json encode it, why are you trying to put it in json format before that? json_encode will turn your PHP arrays into a json object. Same thing for your records... it will go from a php array to a json object when you
echo json_encode();
it.Thank you. I tried this, but it didn't work. I even tried to add:
header('Content-Type: application/json');
but i still get this error. "DataTables warning: table id=example - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1"
this is what my code looks like now:
$sum = 0;
$records = "";
while ($row = mysqli_fetch_array($result)){
$sum += $row[0];
$records[] = array(
0 => $row[0],
1 => $row[1],
2 => ( $row[2] == "" ? "Not Specified" : $row[2]),
3 => ( $row[3] == "" ? "Not Specified" : $row[3]),
4 => $row[4],
5 => $row[5],
6 => "<a target='_blank' href='http://maps.google.com /?q=".$row[8].",".$row[5].",".$row[4]."><img width='22px' src='http://i2techsolutions.com/greenbook/visistat/library/images/googlei22w.png'></a>",
7 => $row[7]
);
}
$json = array(
"draw" => 1,
"recordsTotal" => $sum,
"recordsFiltered" => 57,
"data" => $records
);
header('Content-Type: application/json');
echo json_encode($json);
Paste your json response(use debugging tools to see) into jsonlint.com
actually. it might have worked. I guess it was a fluke the first reload.
I'm getting this error now:
"DataTables warning: table id=example - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3"
oh nvm. that error is because I'm calling it from somewhere else.
the json error is still standing when i just refresh it. Let me go through the debugger and check it.
After inspecting it. It may be getting stuck because my post data isn't being sent correctly.
Let me show you a piece of the set up; and you can tell me if I'm doing it wrong.
All this code works fine as it is. When POST data was actually sent to the php page.
I guess what I have set up is not actually passing POST variables to the php page.
I assumed that since adding "type": "POST" and no place to actually build the POST variables, that the datatables would automatically grab them from the input fields lieing around the page. Am I wrong?
---the debug error---
----jquery---
-----the input that isn't being recognized--- (on the same page as the jquery)
---the serverside that is trying to recieve the POST data---
without the dates passing into the php page, the queries will fail. If the queries fail, the loop will fail. if the loop fails, the array is not made. if the array is not made, the json is not made.
check
line: 57 on views/DetailsTableJSON.php
Call to undefined function parse()
the strange thing... there is no line 57 on that page.
nor is there a "parse()" on the whole page
the "parse" must be in the code that "echo json_encode($json);" spits out.
Which would make sense. because there is no results stored in the array. I have to fix the POST issue first.
Without using datatables. What is the output of your php file.
Is it valid json?
jsonlint.com will tell you if it is.
okay I manually set the POST data. (Still is a problem)
but now, it's working.
Only issue is, its saying "Showing 0-10 records, and its spitting all of them out. with unclickable page numbers.
I probably just need to learn the parameters in the beginning of the json.
Can you please tell us if you have valid JSON or not?
Invalid JSON will not work with datatables.
yes the JSON is working. I'm getting the record results now.
They just aren't being paged correctly.
The results are showing 26 pages in the pagination at the bottom.
and the text says "Showing 1 to 10 of 288 entries" at the bottom.
But... it shows all 288 records on the first page, despite this info.