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']
));
Related
I need a bit of help, i have this code:
$arr = [
"inventory_id" => 2937,
"products" => [],
];
$q = $dbc->query("SELECT quantity,productId FROM `Products` LIMIT 1");
while ($rs = $q->fetch_assoc()) {
$arr['products'][] = [$rs['productId'] => ["bl_3369" => $rs['quantity'] ] ];
}
which is returning
{
"inventory_id":2937,
"products":[
{
"154801353":{
"bl_3369":"10"
}
}
]
}
and i need to return like this:
{
"inventory_id": "2937",
"products": {
"154801353": {
"bl_3369": "10"
}
}
}
Could someone helping me? I search everywhere but i don't see how to adjust array to get needed structure
You're adding an extra array layer with the []. Change the code to this:
while ($rs = $q->fetch_assoc()) {
$arr['products'][$rs['productId']] = ["bl_3369" => $rs['quantity']];
}
Hi iam currently working on json based project.But iam having problem in encoding my data from my table like the below format.....I have seen a lot of json formats.....If any one can help me providing
php code snippet to encode exactly to such format.....
I need exactly such output where "data" is the array name
{
"data":[
{
"Id":1,
"Name":"Haben",
"Surname":"Dave",
"Age":22
},
{
"Id":12,
"Name":"Tomas",
"Surname":"Haleka",
"Age":32
},
{
"Id":123,
"Name":"Henok",
"Surname":"Dave",
"Age":28
},
{
"Id":1,
"Name":"Nafta",
"Surname":"Dave",
"Age":22
}
]
}
This will iterate through all selected rows and will encode them as Array. If you want to receive column names as they're in the table use below code.
$sql_query = "........";
$result = $db_connection->query($sql_query);
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
}
print json_encode(array('data'=> $rows));
If you don't want to receive same column names as in database you can change how you want to receive them. You'll have to edit your while loop to do so.
while($r = mysqli_fetch_assoc($result)) {
$id = $r['user_id'];
$name = $r['user_name'];
$age = $r['user_age'];
$surname = $r['user_surname'];
$rows[] = array('Id' => $id, 'Name' => $name, 'Age' => $age, 'Surname' => $surname);
}
I'm writing a PHP code for inserting the work order name in a spinner.
I need a JSON String called success = 1 along with JSON Array.
I want to use that JSON string in the android spinner.
I'm getting only JSON array.
here is my code:
<?PHP
require_once('connection.php');
$workName = "SELECT 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 an output as like this:
{"success":1,result":[{"$workOrderName":"electrician"},{"$workOrderName":"plumber"},{"$workOrderName":"carpenter"}]}
currently, I am getting the output like
{
"result": [
{
"$workOrderName": "electrician"
},
{
"$workOrderName": "plumber"
},
{
"$workOrderName": "carpenter"
}
]
}
Instead of creating two arrays, one for the result and one for the success, define the structure in just one array and append to that:
// The main structure
$result = [
'success' => 1,
'result' => [],
];
while($row = mysqli_fetch_array($r)){
// Append to the sub key "result" (which we already defined as an array)
$result['result'][] = ['$workOrderName'=>$row['workorder_name']];
}
// Just encode the complete array
echo json_encode($result);
Note: This example uses the short array syntax [] (introduced in PHP 5.4) instead of the long syntax: array(). They do the exact same thing.
I also changed array_push() to a shorter version: $someArray[] =.
$result = array();
$resultArr['success'] =true;
while($row = mysqli_fetch_array($r))
{
array_push($result,array('$workOrderName'=>$row['workorder_name']));
}
$resultArr['result'] = $result;
echo json_encode($resultArr);
This will work fine.
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;
Basically idea is to grab data from MySQL table and transform it to JSON.
This is how database table looks:
And this how should output be:
[
{"group1":[
{"val":"somevalue"},
{"val":"somevalue"}
]
},
{"group2":[
{"val":"somevalue"},
{"val":"somevalue"}
]
},
{"group3":[
{"val":"somevalue"}
]
}
]
My PHP script looks like this, for now:
$arr = [];
$result = mysql_query("SELECT * FROM thetable WHERE section='sect1'");
while($row = mysql_fetch_array($result))
{
// ???
}
echo json_encode($arr);
My main issue is how to output/sort data in "groups".
Thanks for your help!
try this
while($row = mysql_fetch_array($result))
{
$arr[$row['group']][] = array('val' => $row['value']);
}