2016-11-22 49 views
0

我有這個PHP腳本,給了我一個JSON響應。有問題的網址查詢

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

並且json我得到

[{"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"}] 

我想執行一個查詢,將返回其標題中包含乾淨的字的對象。

所以我用這個網址

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

Hoewever,我得到同樣的JSON響應前。該對象或對象不會被過濾掉。這是爲什麼發生?

謝謝,

泰奧。

回答

1

如果我理解正確,您希望sample_data.php能夠返回過濾的數據嗎? 首先,你需要更新sample_data.php處理q參數(我會用它作爲GET,因爲它更簡單: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;?> 

這樣$posts_row只需將相關的書籍

- 通過ID -ADDITION ----

秀書JSON

http://www.theo-android.co.uk/books/sample_data.php?id=1

<?php 
include("init.php"); 
$string=""; 
$newString=""; 
$query = mysqli_real_escape_string($con,$_GET['q']); // get and escape the q param 
$id = (int)$_GET['id']; // get and cast to int the id var from GET 
$where_cond = array(); 
$get_posts = "select * from books_table"; 
if($query != '') $where_cond[] = " title LIKE '%{$query}%'"; // if $query is not empty string - query using a wild card 
if($id > 0) $where_cond[] = " id = {$id}"; // if $id is a number 
if(!empty($where_cond)) $get_posts .= " WHERE " . implode(" AND ",$where_cond); 
$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;?> 

,因爲你希望它爲雙方idq(和只是其中之一)工作,我將每個條件數組,然後用AND分離

這是未經測試破滅了。

+1

哇。感謝您的幫助:)你是一個聰明的狗。 – Theo

+1

我的榮幸:-) –

+0

還有另外一個問題。如果你想使用一個url來獲得你的id對象並將其數據顯示爲一個json對象,該怎麼辦?即http://www.theo-android.co.uk/books/sample_data.php?id=1。謝謝 – Theo