I am trying to create a json using php
while ($info = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$id = stripslashes($info['id']);
$pricedol = stripslashes($info['pricedol']);
$final_result = array('id' => $id,'pricedol' => $pricedol );
}
$json = json_encode($final_result);
echo $json;
This is giving the following output
{
"id": 14567,
"pricedol": 15.57
}
But i have couple of records in the db...i want the following output
{
"id": 14567,
"pricedol": 15.57
},
{
"id": 4567,
"pricedol": 55.25
},
One more thing...do i need to serialize before parsing in jquery
You could create a multi-dimensional array and use json_encode on that array:
$final_result = array();
while ($info = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$id = stripslashes($info['id']);
$pricedol = stripslashes($info['pricedol']);
$final_result[] = array('id' => $id,'pricedol' => $pricedol);
}
$json = json_encode($final_result);
echo $json;
Related
I am trying to sent response in that form
{"files":[{"webViewLink":""},{"webViewLink":""}]}
But I'm getting response like that
{"files":[{"webViewLink":"""}]}{"files":[{"webViewLink":"""}]}
Here is my PHP code:
<?php
$idtemp = extractfiles($fol_id, $email);
foreach ($idtemp['items'] as $val) {
$id = $val['id'];
$val = array(
"webViewLink" => 'https://drive.google.com/file/d/'.$val['id'].'/view?usp=drivesdk"'
);
$enc = json_encode($val);
$val = '{"files":['.$enc.']}';
echo($val);
Please help me to fix code i need response in that way
{"files":[{"webViewLink":""},{"webViewLink":""}]}
You should no do $val = '{"files":['.$enc.']}';
Use json_encode to make json, don't do it manual
Create an object with desired keys outside the loop
Push anything to the desired array
Convert to json
<?php
$idtemp = extractfiles($fol_id, $email);
$json = (object) [
"files" => []
];
foreach ($idtemp['items'] as $val) {
$json->files[] = (object) [
"webViewLink" => 'https://drive.google.com/file/d/'.$val['id'].'/view?usp=drivesdk"'
];
}
$json = json_encode($json);
echo($json);
If I use some dummy values to create an example, the output is:
{
"files": [
{
"webViewLink": "https://drive.google.com/file/d/1/view?usp=drivesdk\""
},
{
"webViewLink": "https://drive.google.com/file/d/2/view?usp=drivesdk\""
},
{
"webViewLink": "https://drive.google.com/file/d/3/view?usp=drivesdk\""
}
]
}
As you can test in this online demo
I am new to PHP so far I managed to get the following output from database in JSON format.I created an array result which returns workorderName, but I want to get workOrderId also in the same array so that I can use it in android string request.
{
"result": [
{
"$workOrderName": "electrician"
},
{
"$workOrderName": "plumber"
},
{
"$workOrderName": "carpenter"
}
]
}
my php code is
<?PHP
require_once('connection.php');
$workName = "SELECT work_order_id,workorder_name FROM workorder_category";
$con=mysqli_connect($server_name,$user_name,$password,$db);
$r = mysqli_query($con,$workName);
$result = array();
$resultArr = array('success' => true);
while($row = mysqli_fetch_array($r)){
array_push($result,array('$workOrderName'=>$row['workorder_name']));
}
echo json_encode(array('result'=>$result));
mysqli_close($con);
?>
I want the Output like
{
"result": [
{
"$workOrderId":"1"
"$workOrderName": "electrician"
},
{
"$workOrderId":"2"
"$workOrderName": "plumber"
},
{
"$workOrderId":"3"
"$workOrderName": "carpenter"
}
]
}
I'm pretty sure you didn't mean to have the $ in the key, but this is easier and will give you the column names (work_order_id, workorder_name) from the table as keys. Make sure to use mysqli_fetch_assoc:
while($row = mysqli_fetch_assoc($r)){
$result[] = $row;
}
If you want to change the keys then you can alias them in the query and use the above:
$workName = "SELECT work_order_id AS workOrderID, workorder_name AS workOrderName FROM workorder_category";
Or you could build the array:
$result[] = array('workOrderID' => $row['work_order_id'],
'workOrderName' => $row['workorder_name']);
You need to tweak your code to add $workOrderId also in the array like below
array_push($result,array(
'$workOrderId' => $row['work_order_id']
'$workOrderName' => $row['workorder_name']
));
The below is my php code.Many thanks
$data = array();
foreach ($row as $rowk) {
$data[] = array(
'message' => $row['traditionalmessage'],
'phone' => $row['telMobile']
);
break;
}
echo json_encode($data);
My current result
[{"message":"B\u578b\u809d\u708e\u75ab\u82d7\u4e0b\u4e00\u500b\u6ce8\u5c04\u671f\u4e3a2017-06-30\r\n","phone":"96709394"}][{"message":"\u75ab\u82d7\u540d\u7a314\u4e0b\u4e00\u500b\u6ce8\u5c04\u671f\u4e3a2017-06-30\r\n","phone":"96709394"}][{"message":"\u4fe1\u606f","phone":"55503234"}]
My desired result
[{"message":"B\u578b\u809d\u708e\u75ab\u82d7\u4e0b\u4e00\u500b\u6ce8\u5c04\u671f\u4e3a2017-06-30\r\n","phone":"96709394"},{"message":"\u75ab\u82d7\u540d\u7a314\u4e0b\u4e00\u500b\u6ce8\u5c04\u671f\u4e3a2017-06-30\r\n","phone":"96709394"},{"message":"\u4fe1\u606f","phone":"55503234"}]
In JSON arrays represented in [], and objects in {}, so in desired case you should have array of objects while you have array of arrays.
Try something like this:
$data = array();
foreach ($row as $rowk) {
$obj = new stdClass();
$obj->message = $row['traditionalmessage'];
$obj->phone = $row['telMobile'];
$data[] = $obj;
}
echo json_encode($data);
I'm fairly new to Json and arrays and I'm just wondering whether someone can correct the code I've tried that is still outputting an invalid string.
$json = array(
'posts' => array(),
);
while($row = mysqli_fetch_array($result)) {
$posts = array();
$posts['count'] = $rowcount;
$posts['streamitem_id'] = $row['streamitem_id'];
$json['posts'] = $posts;
echo json_encode($json);
}
I have used http://jsonlint.com/ and posted my returned data from firebug errors on lines 6,11 and 16
} {
{
"posts": {
"count": 4,
"streamitem_id": "1976"
}
} {
"posts": {
"count": 4,
"streamitem_id": "1980"
}
} {
"posts": {
"count": 4,
"streamitem_id": "1099"
}
} {
"posts": {
"count": 4,
"streamitem_id": "1178"
}
}
Some have already commented with a valid answer however, I believe array_push is the most efficient method rather than redefining the $posts variable over and over.
$json = array(
'posts' => array()
);
while($row = mysqli_fetch_array($result)) {
array_push($json['posts'], $array(
'count' => $rowcount,
'streamitem_id' => $row['streamitem_id']
)
);
}
echo json_encode($json);
PS: I noticed that you're putting $rowcount in each post array. If this is not intended, I recommend the following change:
$json = array(
'posts' => array(),
'count' => $rowcount
);
And then in the while loop:
array_push($json['posts'], $array(
'streamitem_id' => $row['streamitem_id']
)
);
This way, you can reference the post count and it won't be a waste of memory.
console.log(json['count']);
I think you want this:
$posts = array();
$rowcount = 0;
while ($row = mysqli_fetch_array($result)) {
$posts[] = array(
'count' => $rowcount,
'streamitem_id' = $row['streamitem_id'],
);
$rowcount++;
}
$json['posts'] = $posts;
echo json_encode($json);
Put this outside of loop, you are overwriting and echoing each value instead whole at once
Inside loop
$posts[] = ["count"=> $rowcount,"streamitem_id"=>$row['streamitem_id']);
After loop
$json['posts'] = $posts;
echo json_encode($json);
I need help with my json output. At present my output is as so:
[
[
{
"ING_SW_CB": "5",
"SB_SW_CB": "3",
"NG3_SW_CB": "1",
"Mould_Close": "4",
"Leak_Test": "5",
"ML_Load": "6",
"Pre_Heat": "3",
"Dispense": "9",
"A310": "7",
............
}
]
]
I’d like it to be as following with "key" & "value" included in the output.
{"key":"ING_SW_CB", "value": "5"},
{"key":"SB_SW_CB", "value": "3"},
..............................
Hers is my PHP script:
$json_response = array();
while ($row = mysqli_fetch_array($result2, MYSQL_ASSOC)) {
$row_array['ING_SW_CB'] = $row['ING_SW_CB'];
$row_array['SB_SW_CB'] = $row['SB_SW_CB'];
$row_array['NG3_SW_CB'] = $row['NG3_SW_CB'];
$row_array['Mould_Close'] = $row['Mould_Close'];
$row_array['Leak_Test'] = $row['Leak_Test'];
$row_array['ML_Load'] = $row['ML_Load'];
$row_array['Pre_Heat'] = $row['Pre_Heat'];
$row_array['Dispense'] = $row['Dispense'];
$row_array['A310'] = $row['A310'];
$row_array['Gelation'] = $row['Gelation'];
$row_array['Platen'] = $row['Platen'];
$row_array['Mainline_Unload'] = $row['Mainline_Unload'];
$row_array['De_mould'] = $row['De_mould'];
$row_array['Clean_Up'] = $row['Clean_Up'];
$row_array['Soda_Blast'] = $row['Soda_Blast'];
$row_array['Miscellaneous'] = $row['Miscellaneous'];
//push the values in the array
array_push($json_response,$row_array);
}
$json_data = json_encode($json_response, JSON_PRETTY_PRINT);
file_put_contents('your_json_file.json', $json_data);
I need the keywords "key" & "value" added to the output so I can popuate a D3 chart.
You need to loop on the row's results and build more arrays:
while(...) {
$temp = array();
foreach($row as $key => $value) {
$temp[] = array('key' => $key, 'value' => $value);
}
$json_response[] = $temp;
}
Loop over the columns:
$json_response = array();
while ($row = mysqli_fetch_array($result2, MYSQLI_ASSOC)) {
$json_row = array();
foreach ($row as $key => $value) {
$json_row[] = array('key' => $key, 'value' => $value);
}
$json_response[] = $json_row;
}
$json_data = json_encode($json_response, JSON_PRETTY_PRINT);
file_put_contents('your_json_file.json', $json_data);
$json_response = array();
while ($row = mysqli_fetch_array($result2, MYSQL_ASSOC)) {
//push the values in the array
foreach($row as $k=>$v){
$my_array = array("key"=>$k, "value"=>$v);
}
array_push($json_response,$my_array);
}
$json_data = json_encode($json_response, JSON_PRETTY_PRINT);
file_put_contents('your_json_file.json', $json_data);