dongzhisang5342 2014-08-27 04:40
浏览 93
已采纳

Android Eclipse如何从php MySql数据库中显示特定数据到列表视图

Hello and good day all,

I have try android eclipse project connect to phpmysql with xampp server. For now i success to get and display all data from mysql database to listview.

But how to only get specific data only. Example :

Name : Class: CodeSubject: SubjectName: JOHN 2 TBE124

JOHN 2 TKE123

JOHN 2 TZE125

JOHN 2 TDE194

ADAM 2 TAE154

ADAM 2 TQE114

GORGE 2 TGE164

GORGE 2 TCE123

GORGE 2 TBE126

What i want is ListView will show All JOHN data/row/column if spinner select JOHN. If select ADAM then ListView will show All ADAM data/row/column. For my code now, it will show all the data from database. If any have tutorial related please share with me.

I used androidhive tutorial. http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/


<?php

 /*
 * Following code will list all the products
 */

// array for JSON response
$response = array();


// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

//here the search value is what you send from the app
if(isset($_GET["matricID"])){

$string_input = $_GET['matricID'];

$result = mysql_query("SELECT * FROM tbl_semester WHERE matricID LIKE '%$matricID%'") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {

// looping through all results
// products node
$response["tbl_semester"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$semester = array();
$semester["pid"] = $row["pid"];
$semester["matricID"] = $row["matricID"];
$semester["code"] = $row["code"];
$semester["course"] = $row["course"];
$semester["point"] = $row["point"];
$semester["crdhour"] = $row["crdhour"];
$semester["grdpoint"] = $row["grdpoint"];
$semester["reattempt"] = $row["reattempt"];
$semester["grd"] = $row["grd"];

// push single product into final response array
array_push($response["tbl_semester"], $semester);
}

// success
$response["success"] = 1;

// echoing JSON response
echo json_encode($response);
} else {

// no products found
$response["success"] = 0;
$response["message"] = "No products found";

// echo no users JSON
echo json_encode($response);
}
}
?>

  • 写回答

2条回答 默认 最新

  • drwo2014 2014-08-27 04:49
    关注

    You need to write a webservice which will get the data from your mysql database and send it to your android app.

    Write a php script on your server, get the data, encode it inside a JSON object and send it to your app.

    The tutorial you have quoted well describes the way to do it. You can follow the same.

    To handle the second part of your question, send the item selected on the spinner to the server via a POST or a GET. Create a query to retrieve the rows using the data sent to the server. Send the retrieved data back to the app the similar way you have done it earlier. Then populate the data received in your ListView.

    The same tutorial shows this demonstration as well.

    Hope it helps.

    You sample php at the server could be something like this

    //here the search value is what you send from the app
    if(isset($_POST["searchvalue"])){
    
        $string_input = $_POST['searchvalue'];
    
        $result = mysql_query("SELECT * FROM TABLENAME WHERE NAME_COLUMN LIKE '%$string_input%'") or die(mysql_error());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!