java - How to filter database output? (android PHP) -
i building mobile app in user logs in , outputs contents of database table named "announcements".
what i'm trying filter out these output based on "department" column "accounts" table in users stored.
the "announcements" table has column named "receiver".
the contents show if "department" column of user logged in has same value "receiver column" of "announcements" column or if value of receiver "all".
how do this?
my php script
<?php $host="localhost"; //replace database hostname $username="root"; //replace database username $password=""; //replace database password $db_name="sunshinedb"; //replace database name $con=mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select db"); $sql = "select * announcement"; $result = mysql_query($sql); $json = array(); if(mysql_num_rows($result)){ while($row=mysql_fetch_assoc($result)){ $json['services'][]=$row; } } mysql_close($con); echo json_encode($json); ?>
java class
jsonobject jsonobject; jsonarray jsonarray; listview listview; arraylist<hashmap<string, string>> arraylist; progressdialog mprogressdialog; jsonparser jsonparser = new jsonparser(); string email; string[] services; private string url = "http://10.0.3.2/sunshine-ems/services.php"; string user_id; // json node names private static final string tag_trans_id = "announcement_id"; private static final string tag_date = "date"; private static final string tag_services = "title"; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.videos_layout); // listview listview lv = getlistview(); lv.setonitemclicklistener(new android.widget.adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view view, int arg2, long arg3) { intent = new intent(getapplicationcontext(), single_list.class); string transaction_id = ((textview) view .findviewbyid(r.id.transac_id)).gettext().tostring(); i.putextra("announcement_id", transaction_id); startactivity(i); } }); new downloadjson().execute(); } // downloadjson asynctask private class downloadjson extends asynctask<string, string, string> { @override protected void onpreexecute() { super.onpreexecute(); // create progressdialog mprogressdialog = new progressdialog(videosactivity.this); // set progressdialog title mprogressdialog.settitle("loading services"); // set progressdialog message mprogressdialog.setmessage("loading..."); mprogressdialog.setindeterminate(false); // show progressdialog mprogressdialog.show(); } @override protected string doinbackground(string... params) { jsonobject json = jsonfunctions.getjsonfromurl(url); // check log cat json reponse log.d("service history ", json.tostring()); // create array arraylist = new arraylist<hashmap<string, string>>(); try { // locate array name jsonarray = json.getjsonarray("services"); (int = 0; < jsonarray.length(); i++) { hashmap<string, string> map = new hashmap<string, string>(); json = jsonarray.getjsonobject(i); string transac_id = json.getstring(tag_trans_id); string date = json.getstring(tag_date); string service = json.getstring(tag_services); // retrive json objects map.put(tag_services, service); map.put(tag_date, date); map.put(tag_trans_id, transac_id); // set json objects array arraylist.add(map); } } catch (jsonexception e) { log.e("error", e.getmessage()); e.printstacktrace(); } return null; } @override protected void onpostexecute(string file_url) { mprogressdialog.dismiss(); // updating ui background thread runonuithread(new runnable() { public void run() { /** * updating parsed json data listview * */ listadapter adapter = new simpleadapter( videosactivity.this, arraylist, r.layout.listview_services, new string[] { tag_trans_id, tag_services, tag_date }, new int[] { r.id.transac_id, r.id.txt_service, r.id.txt_date }); // updating listview setlistadapter(adapter); } }); } }
you not making filtering anywhere in code...
the steps should these ones (in order) :
- android side : calling webservice (php code) user's department (in or post parameter)
- ws side : requesting database
select * announcement receiver = '<department'> or receiver = 'all'
department user's department - ws side : construct json response
- android side : process json response display results
the advantages of making :
- limit number of data transfered (limit network consumption on android device , limit load on php server)
- limit number of data processed android side (limit load of android app : it's not desktop app ! never forgive !)
ps : reading post , comment, think should these points before starting make app : sql request, php mysql access (as pointed out @jay blanchard), web services , http protocol, android asynctask
Comments
Post a Comment