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']);
}
Related
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 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']
));
I am trying to know if the new data can be added to JSON before encoding it?
I am retrieving the data from MySQL database in the following way:
//fetch the data from the database
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$to_encode[] = $row;
}
which gives me this:
[
{
name: "aaa"
},
{
name: "bbb"
}
]
Then I encode it to JSON with:
$array1 = json_encode($to_encode)
I wanted to know if I can add more data into the array before encoding it to make it like this?
[
{
name: "aaa"
age: '5'
},
{
name: "bbb"
age: '5'
}
]
or should I decode the encoded JSON, add the new values and then encode it back?
Simply you can do like this:
//fetch the data from the database
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$to_encode[] = $row;
}
for ($i = 0; $i < count($to_encode); $i++) {
$to_encode[$i]['age'] = '14';
}
$array1 = json_encode($to_encode);
print_r($array1);
Try something like this :
$i=0;
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$to_encode[$i]["name"] = $row;
$to_encode[$i]["age"] = 5;
$i++;
}
$array1 = json_encode($to_encode)
You can push an array to the $row variable, the idea is to build the array before you use json_encode
$to_encode = [];
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$age = array('age'=>5);
array_push($to_encode,$row);
array_push($to_encode,$age);
}
$array = json_encode($to_encode);
My Mysqli Updated Query and it Outputs this
SELECT milestone.id, milestone.name, milestone.date, milestone.location, milestone.story_body, milestone.short_link, milestone.created_at,
GROUP_CONCAT(images.path) as path , images.update_type
FROM milestone
INNER JOIN images ON milestone.id = images.update_id
WHERE milestone.business_id = '1' && milestone.status = '1' && images.update_type = '3'
GROUP BY milestone.id
How Can I form JSON object using above query?
I've tried below method which doesn't give any result
$result_array = array();
while($row = mysql_fetch_assoc($result))
{
$result_array[] = $row;
}
I want something like this -
[
{
"id":"4",
"name":"2nd anniversary",
"date":"2015-12-17",
"location":"Mumbai",
"story_body":"Gzjjs jdk djks jdks jdkd jx djdb djd JD djbd djdj d",
"short_link":"izWfs",
"created_at":"2015-12-11 03:49:52",
"path":
[
{"\/SupportData\/ImpalzB2B\/uploads\/90294930451448437444826.jpg"},
{"\/SupportData\/ImpalzB2B\/uploads\/90294930451449758248579.jpg"}
],
"update_type":"3"
},
{
"id":"7",
"name":"#1styearAnniversary",
"date":"2016-01-20",
"location":"Mumbai",
"story_body":"Bsjsj jdkdk djdkdk dkdkf kdkf dkfj fjfj fjfkjdd djkd",
"short_link":"FHXh0",
"created_at":"2016-01-20 23:10:54",
"path":"\/SupportData\/ImpalzB2B\/uploads\/11453356652175.jpg",
"update_type":"3"
}
]
Note: I know Mysql is not being used in PHP 7. I need to replace it with PDO & Mysqli so please neglect that mistake. I am working on same meanwhile I am facing this query.
You can do this now -
$result_array = array();
while($row = mysql_fetch_assoc($result))
{
$temp= explode(',', $row['path']); // explode by ,
if(count($temp) > 1) { // More than one element then assign
$row['path']= $temp;
}
$result_array[] = $row;
}
Hi I have some table say fro example table1 ,table2 in my mysql databse.
I need to get following json result from php.
Can anyone suggest how to achieve this?
any good tutorial doing same is also helpful.
I am able to convert database result in simple json response but custom response is something difficult for me.
{
response:ok
tables:[
{
name:table name
data:[
{
fieldname1:value1
fieldname2:values2
},
{
fieldname1:value1
fieldname2:value2
}
.
.
]
},
{
name:table name1
data:[
{
fieldname1:value1
fieldname2:values2
},
{
fieldname1:value1
fieldname2:value2
}
.
.
]
},
]
}
}
Quoting from How to convert mysql data base table data in json using php, once you have your table names you can do for each of them.
$result = array();
$result['response'] = 'ok'
foreach ($tables as $tableName) {
$query = mysql_query("SELECT * FROM $tableName");
$rows = array();
while($row = mysql_fetch_assoc($query)) {
$rows[] = $row;
}
$result['tables'][] = array(
'name' = $tableName,
'data' = $rows
)
}
print json_encode($result);