2014-11-14 66 views
-1

我正在用戶登錄的地方構建一個移動應用程序,並輸出名爲「公告」的數據庫表格的內容。如何過濾數據庫輸出? (android php)

我想要做的是根據存儲用戶的「accounts」表中的「department」列過濾掉這些輸出。

「公告」表格具有名爲「receiver」的列。

僅當登錄的用戶的「部門」列與「公告」列的「接收者列」具有相同的值或者接收者的值爲「全部」時,內容纔會顯示。

我該怎麼做?

我的PHP腳本

<?php 
$host="localhost"; //replace with database hostname 
$username="root"; //replace with database username 
$password=""; //replace with database password 
$db_name="sunshinedb"; //replace with database name 

$con=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 
$sql = "select * from 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類

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; 

// ALL 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); 

    // get listview 
    ListView lv = getListView(); 

    lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> arg0, View view, int arg2, 
       long arg3) { 

      Intent i = 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 a 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 your log cat for JSON reponse 
     Log.d("Service history ", json.toString()); 

     // Create the array 
     arraylist = new ArrayList<HashMap<String, String>>(); 

     try { 
      // Locate the array name 
      jsonarray = json.getJSONArray("services"); 

      for (int i = 0; i < 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 the JSON Objects into the 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 from Background Thread 
     runOnUiThread(new Runnable() { 
      public void run() { 

       /** 
       * Updating parsed JSON data into 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); 

      } 
     }); 

    } 
} 
+2

請[不要使用'mysql_ *'函數](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql在php中),它們不再被維護,並且[正式被棄用](https://wiki.php.net/rfc/mysql_deprecation)。學習[準備的語句](http://en.wikipedia.org/wiki/Prepared_statement),並使用[PDO](http://us1.php.net/pdo)或[MySQLi](http:// us1.php.net/mysqli)。你還會想[防止SQL注入!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – 2014-11-14 16:06:10

+0

SQL [WHERE](http:// en.wikipedia.org/wiki/Where_%28SQL%29) – 2014-11-14 16:06:24

+0

是的,我知道WHERE關鍵字我只是很難寫出比較登錄用戶的部門和公告的接收者的邏輯。 – Idonthavefingers 2014-11-14 16:13:00

回答

0

你是不是在你的代碼進行任何過濾的任何地方......

的步驟去做它應該是這些的(按此順序):

  1. Android的側面:調用您的Web服務(PHP代碼)與用戶的部門(以GET或POST參數)
  2. WS方:用像SELECT * FROM announcement WHERE receiver = '<department'> OR receiver = 'ALL',其中部門用戶的部門
  3. WS側請求您的數據庫:構建JSON響應
  4. 的Android側:處理JSON響應以顯示結果

使它像這樣的優點:

  1. 極限數據的數目(限制Android設備上的網絡消耗,並限制PHP服務器的負載)
  2. 限制處理Android數據的數據數量(限制Android應用程序的負載:它不是桌面應用程序!決不原諒)

!PS:讀您的文章和你的評論,我真的覺得你應該考慮這些問題開始讓你的應用程序之前:SQL請求,PHP MySQL的訪問(如@Jay指出Blanchard),Web服務和HTTP協議,Android AsyncTask

+0

謝謝你的回答。在閱讀了一些東西之後,我得到了你和傑伊說的話,但這僅僅是一個學校項目。我只想問你的建議。我得到的步驟,但我應該寫在'雖然? – Idonthavefingers 2014-11-14 16:29:04

+0

您的用戶的部門。你應該把它傳遞給你的web服務 – mithrop 2014-11-14 16:36:56