2017-09-01 53 views
0

answered_questions:隨機生成的問題不會在Android應用工作

enter image description here

問題:

enter image description here

我有這些表和下面的PHP腳本

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


//makes it work 
$category = (string)filter_input(INPUT_GET, 'category'); 
$game_id = (string)filter_input(INPUT_GET, 'game_id'); 

require_once('dbConnect.php'); 

$query = "SELECT question FROM questions 
    WHERE category = '$category' 
    and question 
    NOT IN 
    (SELECT question 
    FROM answered_questions 
    WHERE game_id='$game_id')ORDER BY Rand() limit 1"; 

$r = (mysqli_query($con, $query)); 

$res = mysqli_fetch_array($r); 

$result = array(); 

array_push($result, array(
     "question" => $res['question'], 
    ) 
); 

echo json_encode(array("result" => $result)); 

mysqli_close($con); 

} 

一切工作,直到我在android中運行應用程序。當我點擊getQuestion按鈕/調用方法時,該應用程序產生已經回答的問題。該應用旨在產生一個問題,是不是在與每個回答問題表點擊

private void getQuestion() { 

    String url =""; 

    String cat = category.getText().toString(); 
    String id = game_id.getText().toString(); 

    if (cat.equals("Control Questions")){ 
     url = "http://192.168.0.20/Articulate/getControlQuestion.php?game_id="+id; 
    }else { 
     url = "http://192.168.0.20/Articulate/getQuestion.php?category="+cat+"&game_id="+id; 
    } 

    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      showJSON(response); 
     } 
    }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
       } 
      }); 

    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(stringRequest); 
} 

private void showJSON(String response){ 
    String ques=""; 
    try { 
     JSONObject jsonObject = new JSONObject(response); 
     JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY); 
     JSONObject collegeData = result.getJSONObject(0); 
     ques = collegeData.getString("question"); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    question.setText(ques); 
} 

回答

0

你們爲什麼不乾脆合併兩個表,並添加一個布爾(TINYINT)字段answered?那麼你只需要將你的SQL請求更改爲SELECT question FROM questions WHERE category = '$category' AND answered = 0

+0

狀態可以被回答,空或傳遞,並且需要我改變應用程序的其餘部分。而這仍然可能無法正常工作,考慮到上述查詢在應用程序 – cmc12345

+0

中的應用程序將由許多用戶播放時似乎不工作,所以如果有一個表,則表示沒有game_id,表會被更新對於所有用戶 – cmc12345

+0

好吧好吧^^,我試着創建你的數據庫並執行你寫的請求,它看起來沒問題。我認爲問題在其他地方,也許是放入網址 – RasAlGhul