For building my Android App, I am currently converting my Wordpress SQL database into JSON.
Here's the script that I am currently using which I found on Stack Overflow here:
<?php
$host = "localhost";
$user = "*********";
$password = "********";
$db = "********";
$sql = "select * FROM wp_postmeta WHERE meta_key = 'event_start_date' OR meta_key = 'event_end_date' OR meta_key = 'event_start_time' OR meta_key = 'event_end_time' OR meta_key = 'event_phone' OR meta_key = 'post_views_count' OR meta_key = 'event_address_longitude' OR meta_key = 'event_address_latitude' OR meta_key = 'event_address_zip' OR meta_key = 'event_address_address' ORDER BY post_id ASC";
$con = mysqli_connect($host,$user,$password,$db);
$result = mysqli_query($con,$sql);
$response = array();
while($row = mysqli_fetch_array($result)){
array_push($response,array('post_id'=>$row[1],'meta_tag'=>$row[2],'meta_Value'=>$row[3]));
}
echo json_encode(array("server_response"=>$response));
mysqli_close($con);
?>
A small part of the result that I am getting is this:
{
"post_id": "3726",
"meta_tag": "event_start_date",
"meta_Value": "11\/25\/2015"
}, {
"post_id": "3726",
"meta_tag": "event_start_time",
"meta_Value": "08:00 PM"
}, {
"post_id": "3726",
"meta_tag": "event_end_date",
"meta_Value": "11\/25\/2015"
}, {
"post_id": "3726",
"meta_tag": "event_end_time",
"meta_Value": "11:00 PM"
}, {
"post_id": "3726",
"meta_tag": "event_address_address",
"meta_Value": "Tippler On The Roof 276, 100 FEET ROAD, INDIRANAGAR"
}, {
"post_id": "3726",
"meta_tag": "event_address_zip",
"meta_Value": "560038"
}, {
"post_id": "3726",
"meta_tag": "event_address_latitude",
"meta_Value": "12.9782859"
}, {
"post_id": "3726",
"meta_tag": "event_address_longitude",
"meta_Value": "77.63875669999993"
}, {
"post_id": "3726",
"meta_tag": "post_views_count",
"meta_Value": "65"
}, {
"post_id": "3727",
"meta_tag": "event_address_address",
"meta_Value": "F BAR & KITCHEN 18, ALI ASKAR ROAD OFF CUNNINGHAM ROAD"
}, {
"post_id": "3727",
"meta_tag": "event_address_zip",
"meta_Value": "560001"
}, {
"post_id": "3727",
"meta_tag": "event_phone",
"meta_Value": "9731110594"
}, {
"post_id": "3727",
"meta_tag": "event_address_latitude",
"meta_Value": "12.9887093"
}, {
"post_id": "3727",
"meta_tag": "event_address_longitude",
"meta_Value": "77.5937212"
}, {
"post_id": "3727",
"meta_tag": "post_views_count",
"meta_Value": "54"
}, {
"post_id": "3729",
"meta_tag": "event_start_date",
"meta_Value": "11\/25\/2015"
}, {
"post_id": "3729",
"meta_tag": "event_start_time",
"meta_Value": "08:00 PM"
}, {
"post_id": "3729",
"meta_tag": "event_end_date",
"meta_Value": "11\/25\/2015"
}, {
"post_id": "3729",
"meta_tag": "event_end_time",
"meta_Value": "11:30 PM"
}, {
"post_id": "3729",
"meta_tag": "event_address_address",
"meta_Value": "FUNKY VILLA 4TH FLOOR, NGV COMMERCIAL COMPLEX, KHB GAMES VILLAGE"
}, {
"post_id": "3729",
"meta_tag": "event_address_zip",
"meta_Value": "560047"
}, {
"post_id": "3729",
"meta_tag": "post_views_count",
"meta_Value": "77"
}, {
"post_id": "3731",
"meta_tag": "event_start_date",
"meta_Value": "12\/19\/2015"
}, {
"post_id": "3731",
"meta_tag": "event_start_time",
"meta_Value": "09:00 PM"
But this is not how I want my results to appear. Therefore, after fetching the array of results in my php script, I want to sort the values there itself and then pass it on in JSON format to my app.
As you can see, each piece right now contains,3 values: post_id, meta_tag and meta_value. So, there are two things that I want to do with it:
- Combine all small arrays with common post_id into one.
- Then, the 3 values that are appearing right now are not how I need them. Actually the value that meta_tag is showing should replace meta_tag and the value of meta_value should become its value.
To avoid the confusion, and put things into perspective, here's how I would like the above results to appear:
{
"post_id": "3726",
"event_start_date": "11\/25\/2015",
"event_start_time": "08:00 PM"
"event_end_date": "11\/25\/2015"
"event_end_time": "11:00 PM"
"event_address_address": "Tippler On The Roof 276, 100 FEET ROAD, INDIRANAGAR"
"event_address_zip": "560038"
"event_address_latitude": "12.9782859"
"event_address_longitude": "77.63875669999993"
"post_views_count": "65"
}
I have shown the output Json example of one post_id. The same has to be done for all posts. I need a loop solution that can be used to cover all the posts in the database (and there are 100s of them).