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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀