有问题的网址查询

时间:2016-11-22 11:19:03

标签: php json url

我有这个PHP脚本给了我一个json响应。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        layout="@layout/activity_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <FrameLayout

        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />



</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:layout_gravity="start"
    app:headerLayout="@layout/activity_navigation_header"
    app:menu="@menu/drawer_view"


    >
    <include layout="@layout/activity_navigation_header"/>

  </android.support.design.widget.NavigationView>

我得到了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;?>

我想执行一个查询,该查询将返回标题中包含单词clean的对象。

所以我正在使用这个网址

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

Hoewever,我得到了和以前一样的json响应。一个或多个对象不会被过滤掉。为什么会这样?

谢谢,

西奥。

1 个答案:

答案 0 :(得分: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只会有相关的书籍

- ADDITION ----

通过id

显示书籍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将其内爆分离器

它未经测试。