JSON Multi-dimensional Array using mysql_fetch_assoc - php

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;
}

Related

JSON format getting only one column

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']
));

Output JSON array append additional element PHP

I'm pretty sure I'm quite close so hopefully quick answer.
I'm able to generate this JSON:
apps: [
{
0: {
PublisherCount: "7"
},
Id: "87",
AppName: "Productivity, Focus, Habits & Life Success by Audiojoy",
AppBundle: "productivitymind"
}
]
But I'm trying to get to:
apps: [
{
Id: "87",
AppName: "Productivity, Focus, Habits & Life Success by Audiojoy",
AppBundle: "productivitymind",
PublisherCount: "7"
}
]
Here is my output loop (I think the issue is in the 5th row where I array_push the new value for PublisherCount. It creates an additional node instead of adding it to the end.
$temp_array = array();
$i = 0;
while ($row = mysqli_fetch_assoc($publisher_apps)) {
$temp_array[] = $row;
$temp_array[$i][] = fetch_all(get_publisher_count_by_app_id($row['Id']))[0];
$i++;
}
$publisher_apps = $temp_array;
$result = array("apps"=>$publisher_apps);
output_json($result);
Thanks.
You have rows like this:
['Id' => "87",
'AppName' => "Productivity, Focus, Habits & Life Success by Audiojoy",
'AppBundle' => "productivitymind"]
and fetch_all(get_publisher_count_by_app_id($row['Id']))[0] returns an array like this:
['PublisherCount' => 7]
so when you append it with $temp_array[$i][], the entire array gets assigned to the 0 key of $temp_array[$i].
There are various different ways you could get just the PublisherCount value. One way is to use array_merge to combine the result of get_publisher_count_by_app_id with $row, and then add the modified $row to your main array.
while ($row = mysqli_fetch_assoc($publisher_apps)) {
$count = fetch_all(get_publisher_count_by_app_id($row['Id']))[0];
$temp_array[] = array_merge($row, $count);
}
If you do it this way, $i should become unneccessary.
Change it to this:
$temp_array = array();
while ($row = mysqli_fetch_assoc($publisher_apps)) {
$row['PublisherCount'] = fetch_all(get_publisher_count_by_app_id($row['Id']))[0]['Pub‌​lisherCount'];
$temp_array[] = $row;
}
$publisher_apps = $temp_array;
$result = array("apps"=>$publisher_apps);
output_json($result);

php - adding data into json

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);

create custom json array from database result in php?

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);

MySQL data to JSON via PHP

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']);
}

Categories