0

I'm getting posts details from 'postsTable' with php and encoding it in JSON Like this way

$result_json = mysqli_fetch_all ($result, MYSQLI_ASSOC);
echo json_encode($result_json);

each post has a unique ID

Then I have another table called 'postsLikes' I want to see how many Likes the post have using mysqli_num_rows()

But my question is how can I add the data it returns to each object in Encoded JSON ?

$query_checkup = "SELECT * FROM postsTable WHERE Post_AgeFrom < $age AND $age < Post_AgeTo AND Post_Reviewed = 1";


$result=mysqli_query($con, $query_checkup);
$result_json = mysqli_fetch_all ($result, MYSQLI_ASSOC);

echo "{\"result\":";
echo json_encode($result_json);
echo "}";
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
M4HdYaR
  • 1,124
  • 11
  • 27
  • You should be getting this in the same query, with a JOIN or something like that. Have you considered this possibility? – Ed de Almeida Mar 21 '16 at 14:30

3 Answers3

4

You should append extra data to your array before encoding to json.

foreach ($result_json as $key => $result) {
    $result_json[$key]['likes'] = getLikes();
}
echo json_encode($result_json);

And you need to implement getLikes function as you wish or can do the operation inside foreach loop.

There a note that you need to pay attention: you need to query for each product to get likes. It is better to join tables and format your array as your need in a loop.

Ali Farhoudi
  • 5,350
  • 7
  • 26
  • 44
0

Maybe you can create a new array and use the foreach construct to add the previous values and the new values.

$new_array = array();

foreach($result_json as $result){
   $new_array[] = array(
      'id' => $result['id'],
      'likes' => getlikes($result['id'])
   );
}

function getlikes($id){
   // your code to get likes number with mysqli_num_rows()
}

echo json_encode($new_array);
Vicent Ibáñez
  • 439
  • 1
  • 6
  • 10
0

You should join the 'postsLikes' table in your original query to pull in all the data you need with a single trip to the database.

Something like this: (Guessing on how your tables are setup)

$query = "
SELECT 
  P.* ,
  L.Likes

FROM postsTable P
LEFT JOIN postsLikes L ON L.Post_id = P.Post_Id

WHERE 
  P.Post_AgeFrom < :age AND 
  :age < P.Post_AgeTo AND 
  P.Post_Reviewed = 1 ";

$params = array( "age" => $age );

$pdo = new PDO('mysql:dbname=testdb;host=127.0.0.1', $user, password);
$stmt = $pdo->prepare( $query );
$stmt->execute( $params );
$results = $stmt->fetchAll( PDO::FETCH_ASSOC );

$response = array( "results" => $results );
echo json_encode( $response );

Other bits of advice:

1) Use bound parameters to prevent SQL injection.

2) Don't manually create any of the JSON in your response, create the response array first and let json_encode do the rest of the work

  • Isn't mysqli_real_escape_string() enough for preventing sql injection ? – M4HdYaR Mar 21 '16 at 15:07
  • @M4HdYaR - No, not all the time - [http://stackoverflow.com/a/5741264/1725265](http://stackoverflow.com/a/5741264/1725265). Also, the mysql* functions have been deprecated - [http://stackoverflow.com/q/12859942/1725265](http://stackoverflow.com/q/12859942/1725265) – syndicate_software Mar 21 '16 at 16:28