Majdi_la
Majdi_la

Reputation: 1028

Android Eclipse How to display specific data from phpMySql database to list view

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

Upvotes: 0

Views: 1737

Answers (2)

codePG
codePG

Reputation: 1744

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());

Upvotes: 0

Vishal
Vishal

Reputation: 537

This can be done in two ways:

  1. By Querying mysql database on each spinner selection.

    • on every selection of the spinner send the request to the database.
    • server will return a JSON object, parse that json object and populate the listview
    • by this way app load will be less bu the server load will be a bit more.
  2. By handling all stuff in java file only

    • make a JSON object corresponding to the key="name": value={all fields related to the name}
    • make a logic for looking into this JSON object on the basis of the name for each spinner selection and populate the list view with the value fields corresponding to the key name
    • but this will load the app with the data which you may not be needing, because all the names will be loaded with their properties in the JSON object

I suggest you to implement the First method.

Upvotes: 1

Related Questions