如何从Android将变量发布到PHP脚本,执行查询,然后使用不同的Post变量将数组返回到Listview中?

时间:2013-08-13 15:10:00

标签: php android json parsing

我能够将信息插入数据库并从数据库中获取信息。我遇到问题的地方是发布可以由php读取的变量,同时从查询中接收信息。这是用于搜索过滤器,因此第一组变量用于定义查询,然后其他后变量用于读取URL。 这是我用来发布变量的代码:

    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(SORTUSERS);  

    try {
        // Add your data
        List nameValuePairs = new ArrayList(4);
        nameValuePairs.add(new BasicNameValuePair("sortid", sortid));
        nameValuePairs.add(new BasicNameValuePair("sortlastname", sortlastname));
        nameValuePairs.add(new BasicNameValuePair("sorturgency", sorturgency));
        nameValuePairs.add(new BasicNameValuePair("xfiltered", xfiltered));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

这是PHP:

<?php 

require("config.inc.php");

//filters
$sortid=$_POST['sortid'];
$sortlastname=$_POST['sortlastname'];
$sorturgency=$_POST['sorturgency'];

$xsortby="urgency";
$xsortby2="lastchange";
$xsortbydirection="DESC";
$xsortby2direction="DESC";

$xfiltered=$_POST['xfiltered'];

if ($xfiltered=="") { $xsortby="urgency"; $xsortbydirection="DESC";    $xsortby2="lastchange"; $xsortby2direction="DESC";  }

//filters
if ($sortid=='' || $sortid=='All Account Numbers') { $gosortid="%"; } else { $gosortid="%".$sortid."%"; }
if ($sortlastname=='' || $sortlastname=='Any Last Name') { $gosortlastname="%"; } else { $gosortlastname="%".$sortlastname."%"; }
if ($sorturgency=='') { $gosorturgency="%"; } else { $gosorturgency=$sorturgency; }

$query = "SELECT * FROM table WHERE (accnum LIKE '".$gosortid."') AND (lastname LIKE '".$gosortlastname."') AND (urgency LIKE '".$gosorturgency."') ORDER BY ".$xsortby." ".$xsortbydirection.",".$xsortby2." ".$xsortby2direction;

    //execute query
try {
    $stmt   = $db->prepare($query);
    $result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
    $response["success"] = 0;
    $response["message"] = "Database Error!";
    die(json_encode($response));
}

// Finally, we can retrieve all of the found rows into an array using fetchAll 
$rows = $stmt->fetchAll();

if ($rows) {
    $response["success"] = 1;
    $response["message"] = "Post Available!";
    $response["posts"]   = array();

    foreach ($rows as $row) {
        $post             = array();
        $post["firstname"]  = $row["firstname"];
        $post["middlename"] = $row["middlename"];
        $post["lastname"] = $row["lastname"];
        $post["accnum"] = $row["accnum"];
        $post["lastchange"] = $row["lastchange"];
        $post["urgency"] = $row["urgency"];


        //update our repsonse JSON data
       array_push($response["posts"], $post);

    }
 //echoing JSON response
 echo json_encode($response);
} else {
    $response["success"] = 0;
    $response["message"] = "No Post Available!";
    die(json_encode($response));
}

?>

主要问题是无法识别特定查询,并返回表中的所有内容。

任何不明确或有问题的评论。 感谢

0 个答案:

没有答案