DataTables warning: table id=example - Invalid JSON response - php

I have googled for the answer of this question but noone helped.
I am getting error mentioned in title.
Here is my ajax code :
var table = $('#example').DataTable( {
"ajax": "<?php echo $root; ?>ajax/order.php",
"processing": true,
"serverSide": true,
"ordering": false,
"searching": true,
"columns": [
{ "data": "order_no" },
{ "data": "country" },
{ "data": "name" },
{ "data": "date","className": "align-center" },
{ "data": "subtotal","className": "align-right" },
{ "data": "dileveryAmt","className": "align-right" },
{ "data": "totalAmt","className": "align-right" },
{ "data": "paymentMode","className": "align-center" },
{ "data": "payment","className": "align-center" },
{ "data": null,"defaultContent": "<button id='view' class='btn btn-small btn-info'>View</button><button id='delete' class='btn btn-small btn-danger'>Delete</button>","className": "align-center" }
]
} )
I validated Response from server ( as seen in Developer tools) and its being shown as Valid JSON. But its not reflected in page.
My HTML Code is
<table id="example" class="display table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Order No</th>
<th>Country</th>
<th>Customer</th>
<th>Date</th>
<th>Sub Total</th>
<th>Delivery Charge</th>
<th>Total</th>
<th>Payment Mode</th>
<th>Payment</th>
<th>Action</th>
</tr>
</thead>
UPDATE
Response from Server(there are plenty of entries. I am showing one as example):
"data": [
{
"id": "183",
"customer_id": "183",
"subtotal": "0.00",
"totalAmt": "0.00",
"dileveryAmt": "0.00",
"date": "18/02/2015",
"midnightdelivery": "0",
"delivery_date": "2015-02-19",
"message_on_cake": "",
"special_instruction": "",
"payment": "<div class='label label-warning'>Pending</div>",
"delivery": "0",
"created": "2015-02-18 10:58:29",
"ip": "",
"payment_mode": "",
"first_name": "Ganesh",
"last_name": "Salunkhe",
"email": "g#s.com",
"address": "",
"flat_no": "k",
"building_name": "k",
"street": "k",
"area": "k",
"landmark": "k",
"city": "mumbai",
"country": "India",
"state": "maharashtra",
"contact_no": "7666902899",
"name": "Ganesh Salunkhe",
"order_no": "1181"
},
Any help will be appreciated.

I struggled with this. and after I RTM here https://www.datatables.net/examples/ajax/custom_data_flat.html I figured it out.
ajax: {
url: "data/objects_root_array.txt", <-- notice url and the link
dataSrc: "" <-- if you are just going to json_encode the result from the db. it will handle a flat string
}
ajax calls are done a certain way. not just ajax. but you need to include the url and dataSrc variables in there. After I did that with my code everything worked as it should.
See if this works for you.
var table = $('#example').DataTable( {
"ajax": {
url: "<?php echo $root; ?>ajax/order.php",
dataSrc : ""
},
"processing": true,
"serverSide": true,
"ordering": false,
"searching": true,
"columns": [
{ "data": "order_no" },
{ "data": "country" },
{ "data": "name" },
{ "data": "date","className": "align-center" },
{ "data": "subtotal","className": "align-right" },
{ "data": "dileveryAmt","className": "align-right" },
{ "data": "totalAmt","className": "align-right" },
{ "data": "paymentMode","className": "align-center" },
{ "data": "payment","className": "align-center" },
{ "data": null,"defaultContent": "<button id='view' class='btn btn-small btn-info'>View</button><button id='delete' class='btn btn-small btn-danger'>Delete</button>","className": "align-center" }
]
} )

Think the problem is paymentMode. It is written without underscore in the table definition and with underscore in your json ("payment_mode":)

The answer should look like this:
{
"data": [
{
"id": "183",
"customer_id": "183",
"subtotal": "0.00",
"totalAmt": "0.00",
"dileveryAmt": "0.00",
"date": "18/02/2015",
"midnightdelivery": "0",
"delivery_date": "2015-02-19",
"message_on_cake": "",
"special_instruction": "",
"payment": "<div class='label label-warning'>Pending</div>",
"delivery": "0",
"created": "2015-02-18 10:58:29",
"ip": "",
"payment_mode": "",
"first_name": "Ganesh",
"last_name": "Salunkhe",
"email": "g#s.com",
"address": "",
"flat_no": "k",
"building_name": "k",
"street": "k",
"area": "k",
"landmark": "k",
"city": "mumbai",
"country": "India",
"state": "maharashtra",
"contact_no": "7666902899",
"name": "Ganesh Salunkhe",
"order_no": "1181"
}
]
}
Look at here: http://json.parser.online.fr/

Related

How to read Specific value from curl Response

this is my response
i need to read first line id value & url of sandbox
{
"id": "chg_g5Y25220190539Ob590811651",
"object": "charge",
"live_mode": false,
"api_version": "V2",
"method": "CREATE",
"status": "INITIATED",
"amount": 1.000,
"currency": "KWD",
"threeDSecure": false,
"card_threeDSecure": false,
"save_card": false,
"statement_descriptor": "Sample",
"description": "Test DESC",
"transaction":
{
"timezone": "UTC+03:00",
"created": "1573191592651",
"url": "https://sandbox.payments.tap.company/test_gosell/v2/payment/response.aspx?tap_chg=PsV8hZdr78kEIAOZl9XL1B5u3dXe%2bBQ%2b3Hrd%2fempxYs%3d&sess=noWi606EI%2bM%3d&token=PsV8hZdr78kEIAOZl9XL1B5u3dXe%2bBQ%2bTyYdQy9iGpS6aAXwKOqAdw%3d%3d",
"expiry": { "period": 30, "type": "MINUTE" },
"asynchronous": false
},
"reference": { "transaction": "txn_0001", "order": "ord_0001" },
"response": { "code": "100", "message": "Initiated" },
"receipt": { "email": true, "sms": true },
"customer": { "first_name": "yxy", "last_name": "ttt", "email": "xyx#xyz.com" },
"source": { "object": "source", "id": "src_card" },
"redirect": { "status": "PENDING", "url": "http://localhost:81/School/" },
"post": { "status": "PENDING", "url": "http://localhost:81/School/" } }
assuming you have your response from curl in a variable called $resp
$resp_decoded = json_decode($resp,TRUE);
print($resp_decoded['id']);
print($resp_decoded['transaction']['url']);

Display JSON Array as a list in php

I'm trying to display a JSON Array in php
JSON:
"members":[
{
"username":"Abstract",
"status":"online",
"bot":true,
"nick":"ViralBot \ud83d\ude0b",
"avatar_url":"https://cdn.discordapp.com/avatars/133718676741292033/59b492151d2c352f3ac5e249e4f04a82.jpg",
"avatar":"59b492151d2c352f3ac5e249e4f04a82",
"discriminator":"0612",
"id":"133718676741292033"
},
{
"username":"Aethex",
"status":"online",
"bot":true,
"game":{
"name":"aethex.xyz | -help"
},
"avatar_url":"https://cdn.discordapp.com/avatars/150300454708838401/bca569aaf0e1093ec8103e7bd3bedfb7.jpg",
"avatar":"bca569aaf0e1093ec8103e7bd3bedfb7",
"discriminator":"0394",
"id":"150300454708838401"
},
{
"username":"AIRHORN SOLUTIONS",
"status":"online",
"bot":true,
"nick":"MLG AIRHORNZ!",
"game":{
"name":"airhornbot.com"
},
"avatar_url":"https://cdn.discordapp.com/avatars/159800228088774656/96be9927ca947a75021db568485612fc.jpg",
"avatar":"96be9927ca947a75021db568485612fc",
"discriminator":"6723",
"id":"159800228088774656"
},
{
"username":"BuddyGang",
"status":"online",
"game":{
"name":"Custom Widgets API"
},
"avatar_url":"https://cdn.discordapp.com/avatars/97172171259904000/089e84734ea5ca86d20292abcca0982f.jpg",
"avatar":"089e84734ea5ca86d20292abcca0982f",
"discriminator":"1704",
"id":"97172171259904000"
},
{
"username":"danclay",
"status":"dnd",
"nick":"danclay \ud83d\ude09",
"avatar_url":"https://cdn.discordapp.com/avatars/97147476531757056/fa460980203d7838c8481c93c0361f6d.jpg",
"avatar":"fa460980203d7838c8481c93c0361f6d",
"discriminator":"2051",
"id":"97147476531757056"
},
{
"username":"dragongod100",
"status":"idle",
"avatar_url":"https://cdn.discordapp.com/avatars/96716994136514560/5393bc18ab9d7ddf19b6fe54575d93a7.jpg",
"avatar":"5393bc18ab9d7ddf19b6fe54575d93a7",
"discriminator":"5291",
"id":"96716994136514560"
},
{
"username":"Hax0nWax0ff",
"status":"idle",
"avatar_url":"https://cdn.discordapp.com/avatars/125738028793593856/ccf68e818fa44e342f60030a89a0fd53.jpg",
"avatar":"ccf68e818fa44e342f60030a89a0fd53",
"discriminator":"7691",
"id":"125738028793593856"
},
{
"username":"iiPsionic",
"status":"idle",
"avatar_url":"https://cdn.discordapp.com/avatars/142014347038818304/ba4bba83436f227933004694b4c184f2.jpg",
"avatar":"ba4bba83436f227933004694b4c184f2",
"discriminator":"6034",
"id":"142014347038818304"
},
{
"username":"RH1-N0",
"status":"online",
"bot":true,
"nick":"RHINO ATTACK!",
"avatar_url":"https://cdn.discordapp.com/avatars/135288293548883969/fe0070998d6c9378edf8243e3267f950.jpg",
"avatar":"fe0070998d6c9378edf8243e3267f950",
"discriminator":"6993",
"id":"135288293548883969"
},
{
"username":"WildBot",
"status":"online",
"bot":true,
"nick":"Cra-Cra Bot \ud83d\ude31",
"avatar_url":"https://cdn.discordapp.com/avatars/110462073074388992/f8143e56615e2107d9cc1ef35c0dfa9e.jpg",
"avatar":"f8143e56615e2107d9cc1ef35c0dfa9e",
"discriminator":"3942",
"id":"110462073074388992"
}
]
I want to be able to display it in numeric order of the "position" variable, with the avatar positioned next to the username then the game text.
HTML layout:
<img src="AVATAR_URL" width="50%"> USERNAME - <small>GAME</small><br>
try this code bro, hope it helps
$arr=json_decode($data,true);
// to prevent error undefined index
if(isset($arr['members'])) {
foreach($arr['members'] as $val){
$gmtxt=isset($val['game']['name'])?$val['game']['name']:"";
echo '<img src="'.$val['avatar_url'].'" width="50%"> '.$val['username'].' - <small>'.$gmtxt.'</small><br>';
}
}
Here the code:-
$data='{
"members": [
{
"username": "Abstract",
"status": "online",
"bot": true,
"nick": "ViralBot 😋",
"avatar_url": "https://cdn.discordapp.com/avatars/133718676741292033/59b492151d2c352f3ac5e249e4f04a82.jpg",
"avatar": "59b492151d2c352f3ac5e249e4f04a82",
"discriminator": "0612",
"id": "133718676741292033"
},
{
"username": "Aethex",
"status": "online",
"bot": true,
"game": {
"name": "aethex.xyz | -help"
},
"avatar_url": "https://cdn.discordapp.com/avatars/150300454708838401/bca569aaf0e1093ec8103e7bd3bedfb7.jpg",
"avatar": "bca569aaf0e1093ec8103e7bd3bedfb7",
"discriminator": "0394",
"id": "150300454708838401"
},
{
"username": "AIRHORN SOLUTIONS",
"status": "online",
"bot": true,
"nick": "MLG AIRHORNZ!",
"game": {
"name": "airhornbot.com"
},
"avatar_url": "https://cdn.discordapp.com/avatars/159800228088774656/96be9927ca947a75021db568485612fc.jpg",
"avatar": "96be9927ca947a75021db568485612fc",
"discriminator": "6723",
"id": "159800228088774656"
},
{
"username": "BuddyGang",
"status": "online",
"game": {
"name": "Custom Widgets API"
},
"avatar_url": "https://cdn.discordapp.com/avatars/97172171259904000/089e84734ea5ca86d20292abcca0982f.jpg",
"avatar": "089e84734ea5ca86d20292abcca0982f",
"discriminator": "1704",
"id": "97172171259904000"
},
{
"username": "danclay",
"status": "dnd",
"nick": "danclay 😉",
"avatar_url": "https://cdn.discordapp.com/avatars/97147476531757056/fa460980203d7838c8481c93c0361f6d.jpg",
"avatar": "fa460980203d7838c8481c93c0361f6d",
"discriminator": "2051",
"id": "97147476531757056"
},
{
"username": "dragongod100",
"status": "idle",
"avatar_url": "https://cdn.discordapp.com/avatars/96716994136514560/5393bc18ab9d7ddf19b6fe54575d93a7.jpg",
"avatar": "5393bc18ab9d7ddf19b6fe54575d93a7",
"discriminator": "5291",
"id": "96716994136514560"
},
{
"username": "Hax0nWax0ff",
"status": "idle",
"avatar_url": "https://cdn.discordapp.com/avatars/125738028793593856/ccf68e818fa44e342f60030a89a0fd53.jpg",
"avatar": "ccf68e818fa44e342f60030a89a0fd53",
"discriminator": "7691",
"id": "125738028793593856"
},
{
"username": "iiPsionic",
"status": "idle",
"avatar_url": "https://cdn.discordapp.com/avatars/142014347038818304/ba4bba83436f227933004694b4c184f2.jpg",
"avatar": "ba4bba83436f227933004694b4c184f2",
"discriminator": "6034",
"id": "142014347038818304"
},
{
"username": "RH1-N0",
"status": "online",
"bot": true,
"nick": "RHINO ATTACK!",
"avatar_url": "https://cdn.discordapp.com/avatars/135288293548883969/fe0070998d6c9378edf8243e3267f950.jpg",
"avatar": "fe0070998d6c9378edf8243e3267f950",
"discriminator": "6993",
"id": "135288293548883969"
},
{
"username": "WildBot",
"status": "online",
"bot": true,
"nick": "Cra-Cra Bot 😱",
"avatar_url": "https://cdn.discordapp.com/avatars/110462073074388992/f8143e56615e2107d9cc1ef35c0dfa9e.jpg",
"avatar": "f8143e56615e2107d9cc1ef35c0dfa9e",
"discriminator": "3942",
"id": "110462073074388992"
}
]
}';
$arr=json_decode($data,true);
foreach($arr['members'] as $val){
$gmtxt=isset($val['game']['name'])?$val['game']['name']:"";
echo '<img src="'.$val['avatar_url'].'" width="50%"> '.$val['username'].' - <small>'.$gmtxt.'</small><br>';
}

How can i get json object results in php using php_decode and url

below is my json object result of google api now i want to get url of cover from it which is parameter 'thumbnail' in below json object,but i dont know how can i retrive it can anyone here help me out?
{
"kind": "books#volumes",
"totalItems": 1,
"items": [
{
"kind": "books#volume",
"id": "9VKCkifBlisC",
"etag": "hhhZjjV6arI",
"selfLink": "url",
"volumeInfo": {
"title": "Instructor's manual to accompany An introduction",
"authors": [
"Jean-Paul Tremblay",
"P. G. Sorenson"
],
"publishedDate": "1976",
"industryIdentifiers": [
{
"type": "ISBN_10",
"identifier": "0070651515"
},
{
"type": "ISBN_13",
"identifier": "9780070651517"
}
],
"pageCount": 278,
"printType": "BOOK",
"categories": [
"Computers"
],
"averageRating": 5.0,
"ratingsCount": 1,
"contentVersion": "0.0.1.0.preview.0",
"imageLinks": {
"smallThumbnail": "url",
/*this is what i need*/
"thumbnail": "url"
},
"language": "en",
"previewLink": "url",
"infoLink": "url",
"canonicalVolumeLink": "url"
},
"saleInfo": {
"country": "IN",
"saleability": "NOT_FOR_SALE",
"isEbook": false
},
"accessInfo": {
"country": "IN",
"viewability": "NO_PAGES",
"embeddable": false,
"publicDomain": false,
"textToSpeechPermission": "ALLOWED",
"epub": {
"isAvailable": false
},
"pdf": {
"isAvailable": false
},
"webReaderLink": "url",
"accessViewStatus": "NONE",
"quoteSharingAllowed": false
}
}
]
}
You could do like this..
$arr = json_decode($json,true);
echo $arr['items'][0]['volumeInfo']['imageLinks']['thumbnail'];
Demo

How to access the JSON's multi-level objects with jQuery [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
In CodeIgniter, in PHP to create a nested-like elements for dropdown <select> I'am using this function:
function list_categories(&$result, $cats, $sub = ''){ // <- THIS
foreach($cats as $cat){
//$cat['category']->id = $sub.$cat['category']->title;
$result[$cat['category']->id] = $sub.$cat['category']->title; // <- THIS
if( sizeof($cat['children']) > 0 ){
$sub2 = str_replace('—→ ', '–', $sub);
$sub2.= '–→ ';
list_categories($result, $cat['children'], $sub2); // <- THIS
}
}
}
// Make an array available outside the function
$categoryData = array(0 => '-- '.lang('FORM_SELECT_CATTOP').' --');
// Launch the function to fill out the array with nested sets
list_categories($categoryData, $categories);
Which generates the HTML <select> list:
I want to do the same from retrieved JSON in jQuery but can't find the answer to go deeper than first level of JSON objects with this jQuery function:
$.getJSON('../raw_get_categories/'+com_id_selected+'/0', function(data){
var items = [];
$.each(data, function(key, value, sub){
items.push('<option id="'+ key[0] + '">' + value[0] + '</option>');
});
console.log(items);
});
And this is the JSON I want to manage and retrieve the same values from it like in PHP code:
{
"28": {
"category": {
"id": "28", // <<< I need to retrieve THIS
"pid": "0",
"com_id": "2",
"route_id": "59",
"excerpt": "",
"ordering": "1",
"title": "Oblecenie a vybava",
"slug": "oblecenie-a-vybava1",
"description": "",
"image": "clothes.png",
"seo_title": "",
"meta": ""
},
"compname": {
"id": "2",
"name": "E-commerce"
},
"children": {
"61": {
"category": {
"id": "61", // <<< I need to retrieve THIS
"pid": "28",
"com_id": "2",
"route_id": "52",
"excerpt": "",
"ordering": "1",
"title": "Ubor na hlavu",
"slug": "ubor-na-hlavu1",
"description": "<p>Capice, Klobuky, Panamy, Baretky, Prilby, Kukly<br></p>",
"image": "clothes-head.png",
"seo_title": "",
"meta": ""
},
"compname": {
"id": "2",
"name": "E-commerce"
},
"children": [ ]
},
"30": {
"category": {
"id": "30",
"pid": "28",
"com_id": "2",
"route_id": "53",
"excerpt": "",
"ordering": "2",
"title": "Bundy a vetrovky",
"slug": "bundy-a-vetrovky",
"description": "",
"image": "",
"seo_title": "",
"meta": ""
},
"compname": {
"id": "2",
"name": "E-commerce"
},
"children": {
"34": {
"category": {
"id": "34",
"pid": "30",
"com_id": "2",
"route_id": "0",
"excerpt": "",
"ordering": "0",
"title": "Letne",
"slug": "letne",
"description": "",
"image": "",
"seo_title": "",
"meta": ""
},
"compname": {
"id": "2",
"name": "E-commerce"
},
"children": [ ]
},
"35": {
"category": {
"id": "35",
"pid": "30",
"com_id": "2",
"route_id": "0",
"excerpt": "",
"ordering": "0",
"title": "Vsesezonne",
"slug": "vsesezonne",
"description": "",
"image": "",
"seo_title": "",
"meta": ""
},
"compname": {
"id": "2",
"name": "E-commerce"
},
"children": [ ]
},
"33": {
"category": {
"id": "33",
"pid": "30",
"com_id": "2",
"route_id": "0",
"excerpt": "",
"ordering": "0",
"title": "Zimne",
"slug": "zimne",
"description": "",
"image": "",
"seo_title": "",
"meta": ""
},
"compname": {
"id": "2",
"name": "E-commerce"
},
"children": [ ]
}
}
},
"31": {
"category": {
"id": "31",
"pid": "28",
"com_id": "2",
"route_id": "54",
"excerpt": "",
"ordering": "3",
"title": "Nohavice a teplaky",
"slug": "nohavice-a-teplaky",
"description": "",
"image": "",
"seo_title": "",
"meta": ""
},
"compname": {
"id": "2",
"name": "E-commerce"
},
"children": [ ]
},
"32": {
"category": {
"id": "32",
"pid": "28",
"com_id": "2",
"route_id": "55",
"excerpt": "",
"ordering": "4",
"title": "Obuv",
"slug": "obuv",
"description": "",
"image": "",
"seo_title": "",
"meta": ""
},
"compname": {
"id": "2",
"name": "E-commerce"
},
"children": [ ]
}
}
},
"38": {
...
}
Could someone suggest me the appropriate way to achieve the same foreach statement in jQuery pls?
Juste use point to get more childs
$.getJSON('filename.php', function(data) {
$('#List li').remove();
$.each(data, function(index, item) {
$('#list').append('<li>'+item.category.id+'</li>');
});
});
You can do something like this:
$.each(data.actions, function(entryIndex, entry) {
var html = '<li class="top-level">' + this.action + '</li>';
});
Source

JSON decoding array

I have this JSON code:
{
"phrases": [
{
"phrases": [
{
"id": "33",
"text": "sasdsad",
"date": "2012-03-14 20:28:45",
"views": "0",
"ip": "64.191.90.5",
"reported": "0",
"strange": "0",
"lang": "en"
},
{
"id": "32",
"text": "que ondaa\r\n",
"date": "2012-03-14 20:27:45",
"views": "0",
"ip": "64.191.90.5",
"reported": "0",
"strange": "0",
"lang": "en"
},
{
"id": "31",
"text": "dsadssadsad",
"date": "2012-03-14 20:27:35",
"views": "0",
"ip": "64.191.90.5",
"reported": "0",
"strange": "0",
"lang": "en"
}
],
"details": {
"success": "true",
"phrase_id": "",
"phrase_text": "",
"phrase_date": ""
}
}
I don't really know what to do. I get some phrases vĂ­a MySQL, and pushes them to an array. This array is json_encoded() and gets printed.
$sth = $sql;
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
$sth = $sql;
$data = array(
"success" => "true",
"phrase_id" => "",
"phrase_text" => "",
"phrase_date" => "",
);
print json_encode($rows).json_encode($data);
and with jQuery I was trying to parse it, but I can't. This is the main problem.
function getPhrases(order,limit,last){
var req_url = ...;
$.getJSON(req_url, function(data) {
$.each(data.phrases, function(i, data) {
appendPhrase(data.text);
lastid = data.id;
});
$.each(data.details, function(i, data) {
$("#phrases-count").html(data.totalcount);
});
});
}
PS: I was doing this with "echo" but got some problems.
{
"phrases": [
{
"id": "33",
"text": "sasdsad",
"date": "2012-03-14 20:28:45",
"views": "0",
"ip": "64.191.90.5",
"lang": "en"
},
{
"id": "32",
"text": "que ondaa<br />",
"date": "2012-03-14 20:27:45",
"views": "0",
"ip": "64.191.90.5",
"lang": "en"
},
{
"id": "31",
"text": "dsadssadsad",
"date": "2012-03-14 20:27:35",
"views": "0",
"ip": "64.191.90.5",
"lang": "en"
}
],
"details": [
{
"totalcount": "3",
"logged_in": "false"
}
]
}
You can't simply combine these JSON arrays:
print json_encode($rows).json_encode($data);
Try this (attempt 2):
print json_encode( array('phrases' => $rows, 'details' => $data) );

Categories