2017-10-01 4 views
0

以下のコードは、mysqlからデータを取り出し、idでフェッチするすべての値を表示します。しかし、私はそれだけで選択comment_id = 3を取得しましょう、彼らはそれはので、これはAndroidのコード選択したIDで値を取得するには

コメント_idのすべての関連コメントの表示のみ値3

id    comment_id     comment 
1     1     love this product 
2     1     not bad 
3     2     too expensive 
4     2     how much 
5     2     too old 
6     3     not that new 
7     5     hate it 
8     7     go away 
9     8     what wrong with you 
10     4     hi 
11     7     hi 
12     4     helo 
13     7     hola 
14     4     go away 
15     6     nice 
16     3     bye 

をされます代わりに、すべてのデータのIDを取得するコードを変換したいです

package com.androidjson.serverupdate_androidjsoncom; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.ArrayList; 
import java.util.List; 

public class test2 extends AppCompatActivity { 

    ListView StudentListView; 
    ProgressBar progressBar; 
    String HttpUrl = "http://192.168.0.199/edit/AllStudentData.php"; 
    List<String> IdList = new ArrayList<>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_show_all_students); 

     StudentListView = (ListView)findViewById(R.id.listview1); 

     progressBar = (ProgressBar)findViewById(R.id.progressBar); 

     new GetHttpResponse(test2.this).execute(); 

     //Adding ListView Item click Listener. 
     StudentListView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
     { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

       // TODO Auto-generated method stub 

       Intent intent = new Intent(test2.this,ShowSingleRecordActivity.class); 

       // Sending ListView clicked value using intent. 
       intent.putExtra("ListViewValue", IdList.get(position).toString()); 

       startActivity(intent); 

       //Finishing current activity after open next activity. 
       finish(); 

      } 
     }); 
    } 

    // JSON parse class started from here. 
    private class GetHttpResponse extends AsyncTask<Void, Void, Void> { 
     public Context context; 

     String JSonResult; 

     List<Student> studentList; 

     public GetHttpResponse(Context context) 
     { 
      this.context = context; 
     } 

     @Override 
     protected void onPreExecute() 
     { 
      super.onPreExecute(); 
     } 

     @Override 
     protected Void doInBackground(Void... arg0) 
     { 
      // Passing HTTP URL to HttpServicesClass Class. 
      HttpServicesClass httpServicesClass = new HttpServicesClass(HttpUrl); 
      try 
      { 
       httpServicesClass.ExecutePostRequest(); 

       if(httpServicesClass.getResponseCode() == 200) 
       { 
        JSonResult = httpServicesClass.getResponse(); 

        if(JSonResult != null) 
        { 
         JSONArray jsonArray = null; 

         try { 
          jsonArray = new JSONArray(JSonResult); 

          JSONObject jsonObject; 

          Student student; 

          studentList = new ArrayList<Student>(); 

          for(int i=0; i<jsonArray.length(); i++) 
          { 
           student = new Student(); 

           jsonObject = jsonArray.getJSONObject(i); 

           // Adding Student Id TO IdList Array. 
           IdList.add(jsonObject.getString("id").toString()); 

           //Adding Student Name. 
           student.StudentName = jsonObject.getString("name").toString(); 

           studentList.add(student); 

          } 
         } 
         catch (JSONException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
        } 
       } 
       else 
       { 
        Toast.makeText(context, httpServicesClass.getErrorMessage(), Toast.LENGTH_SHORT).show(); 
       } 
      } 
      catch (Exception e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) 

     { 
      progressBar.setVisibility(View.GONE); 

      StudentListView.setVisibility(View.VISIBLE); 

      ListAdapterClass adapter = new ListAdapterClass(studentList, context); 

      StudentListView.setAdapter(adapter); 

     } 
    } 
} 

これはあなたがIDを送信し、WHEREあなたのmysqlにそれを追加する必要があり、私のPHPコード

<?php 

if($_SERVER['REQUEST_METHOD']=='POST'){ 

include_once($_SERVER['DOCUMENT_ROOT']."/config/config.php"); 

$id= $_POST['id']; 

// Create connection 
$conn = new mysqli($host, $user, $pass, $database); 

if ($conn->connect_error) { 

die("Connection failed: " . $conn->connect_error); 
} 

$sql = "SELECT * FROM comments where comment_id = '$id'" ; 

$result = $conn->query($sql); 

if ($result->num_rows >0) { 


while($row[] = $result->fetch_assoc()) { 

$tem = $row; 

$json = json_encode($tem); 

} 

} else { 
echo "No Results Found."; 
} 
echo $json; 

$conn->close(); 
} 
?> 
+0

あなたはすべてのデータをダウンロードして、あなたのAndroidのコードでデータをフィルタリングしたいんが、あなたが必要なPHPコードからのデータのみを返すようにしたい場合、あなたはあなたたちを示さなければなりませんPHPコードを使用すると、必要な特定のヘルプを提供することができます。 – Barns

+0

あなたがフルコードで私を助けることができれば幸せになります。私のアプリでコメントを実装しようとしているので、製品がクリックされてアクティビティが開き、コメントが読み込まれるようにしたい。 comment_idは別のテーブルのproduct_idと同じですので、アクティビティを開くとproduct_idとcomment_idをリンクしてその製品のコメントのみをロードします –

+0

PHPファイル –

答えて

0

です。それが役立つことができ : link

+0

をアップロードしましたどのようにコーディングがうまくいかないでしょう –

+0

リストビューに値があり、mysqlのすべてを表示する –

+0

thats私が望むもの –

関連する問題