I have successfully uploaded some data into a table called posts. Now I want to generate some JSON,to use it for android development purposes. So here is my code for getting JSON.
<?php
include("../functions/connect.php");
$string = "";
$posts_array = array();
$get_posts = "select * from posts";
$run_posts = mysqli_query($con,$get_posts);
while($posts_row = mysqli_fetch_array($run_posts)){
$row_array['post_id'] = $posts_row['post_id'];
$row_array['category_id'] = $posts_row['category_id'];
$row_array['post_title'] = $posts_row['post_title'];
$row_array['post_date'] = $posts_row['post_date'];
$row_array['post_author'] = $posts_row['post_author'];
$row_array['post_keywords'] = $posts_row['post_keywords'];
$row_array['post_image'] = $posts_row['post_image'];
$row_array['post_content'] = $posts_row['post_content'];
array_push($posts_array,$row_array);
$string = json_encode($posts_array);
echo $string;
}
?>
My JSON though is shown below.
[
{
"post_id":"6",
"category_id":"1",
"post_title":"Turkey challenges Russia over IS oil trade claim",
"post_date":"12-01-15",
"post_author":"BBC",
"post_keywords":"bbc,war",
"post_image":"post1.jpg",
"post_content":"Turkey has challenged Russia to prove its claim that Ankara shot down a Russian plane in order to protect its oil trade with Islamic State.\nIf you allege something you should prove it, Turkish President Recep Tayyip Erdogan said."
}
][
{
"post_id":"6",
"category_id":"1",
"post_title":"Turkey challenges Russia over IS oil trade claim",
"post_date":"12-01-15",
"post_author":"BBC",
"post_keywords":"bbc,war",
"post_image":"post1.jpg",
"post_content":"Turkey has challenged Russia to prove its claim that Ankara shot down a Russian plane in order to protect its oil trade with Islamic State.\nIf you allege something you should prove it, Turkish President Recep Tayyip Erdogan said."
},
{
"post_id":"8",
"category_id":"1",
"post_title":"Faulty part caused AirAsia crash",
"post_date":"12-01-15",
"post_author":"BBC",
"post_keywords":"aircrash,AirAsia",
"post_image":"breaking_news.png",
"post_content":"A faulty component was a major factor when an AirAsia plane crashed into the Java Sea, killing 162 people last December, Indonesian officials say.\nThe first major report into the crash found that actions by the crew in response to the malfunction also contributed to the disaster.\nThe Airbus A320-200, going from Surabaya to Singapore, was 40 minutes into the flight when contact was lost.\nThe report is the result of a year-long investigation."
}
]
I want it to be something like this.
[
{
"post_id":"6",
"category_id":"1",
"post_title":"Turkey challenges Russia over IS oil trade claim",
"post_date":"12-01-15",
"post_author":"BBC",
"post_keywords":"bbc,war",
"post_image":"post1.jpg",
"post_content":"Turkey has challenged Russia to prove its claim that Ankara shot down a Russian plane in order to protect its oil trade with Islamic State.\nIf you allege something you should prove it, Turkish President Recep Tayyip Erdogan said."
},
{
"post_id":"8",
"category_id":"1",
"post_title":"Faulty part caused AirAsia crash",
"post_date":"12-01-15",
"post_author":"BBC",
"post_keywords":"aircrash,AirAsia",
"post_image":"breaking_news.png",
"post_content":"A faulty component was a major factor when an AirAsia plane crashed into the Java Sea, killing 162 people last December, Indonesian officials say.\nThe first major report into the crash found that actions by the crew in response to the malfunction also contributed to the disaster.\nThe Airbus A320-200, going from Surabaya to Singapore, was 40 minutes into the flight when contact was lost.\nThe report is the result of a year-long investigation."
}
]
I suppose it would be an error caused by my php code. Any ideas?
Thanks.