Theo Theo - 11 days ago 5
JSON Question

Problematic url query

I have this php script that gives me a json response.

<?php
include("init.php");
$string="";
$newString="";
$get_posts = "select * from books_table";
$run_posts = mysqli_query($con,$get_posts);
$posts_array = array();
while ($posts_row = mysqli_fetch_array($run_posts)){
$row_array['title'] = $posts_row['title'];
$row_array['author'] = $posts_row['author'];
$row_array['bookUrl'] = $posts_row['bookUrl'];
$row_array['imageUrl'] = $posts_row['imageUrl'];
$row_array['displayDate'] = $posts_row['displayDate'];
$row_array['numberOfPages'] = $posts_row['numberOfPages'];
array_push($posts_array,$row_array);
}
$string = json_encode($posts_array,JSON_UNESCAPED_UNICODE);
echo $string;?>


And the json I get

[{"title":"Clean Code","author":"Robert Martin","bookUrl":"http:\/\/amzn.to\/1DJybxH","imageUrl":"http:\/\/adavis.github.io\/adept-android\/images\/clean_code.jpg\"","displayDate":"August 11, 2008","numberOfPages":"464"},{"title":"Effective Java","author":"Joshua Bloch","bookUrl":"http:\/\/amzn.to\/1Ku8Xel","imageUrl":"http:\/\/adavis.github.io\/adept-android\/images\/effective_java.jpg","displayDate":"May 28, 2008","numberOfPages":"346"},{"title":"Working Effectively with Legacy Code","author":"Michael Feathers","bookUrl":"http:\/\/amzn.to\/1Jqe1PA","imageUrl":"http:\/\/adavis.github.io\/adept-android\/images\/legacy_code.jpg","displayDate":"October 2, 2004","numberOfPages":"456"},{"title":"Refactoring: Improving the Design of Existing Code","author":"Martin Fowler","bookUrl":"http:\/\/amzn.to\/1Lx4cjR","imageUrl":"http:\/\/adavis.github.io\/adept-android\/images\/refactoring.jpg","displayDate":"July 8, 1999","numberOfPages":"464"}]


I want to perform a query that will return the object whose title contains the word clean.

So I am using this url

[http://www.theo-android.co.uk/books/sample_data.php/q=clean][1]


Hoewever,I get the same json response as before. The object or objects are not filtered out. Why is this happening?

Thanks,

Theo.

Answer

If I understand correctly, you want sample_data.php to be able to return filtered data? first of all, you'll need to update sample_data.php to handle the q param (I would use it as GET since it's simpler: http://www.theo-android.co.uk/books/sample_data.php?q=clean

<?php 
include("init.php");
$string="";
$newString="";
$query = mysqli_real_escape_string($con,$_GET['q']); // get and escape the q param
$get_posts = "select * from books_table"; 
if($query != '') $get_posts .= " WHERE title LIKE '%{$query}%'"; // if $query is not empty string - query using a wild card
$run_posts = mysqli_query($con,$get_posts);     
$posts_array = array(); 
while ($posts_row = mysqli_fetch_array($run_posts)){
    $row_array['title'] = $posts_row['title'];
    $row_array['author'] = $posts_row['author'];
    $row_array['bookUrl'] = $posts_row['bookUrl'];
    $row_array['imageUrl'] = $posts_row['imageUrl'];
    $row_array['displayDate'] = $posts_row['displayDate'];
    $row_array['numberOfPages'] = $posts_row['numberOfPages'];
    array_push($posts_array,$row_array);            
 }     
   $string = json_encode($posts_array,JSON_UNESCAPED_UNICODE);
   echo $string;?>

this way $posts_row will only have the relevant books

Comments