随机生成的问题在Android应用程序中无效

时间:2017-09-01 10:17:46

标签: php android mysql

answered_questions:

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

1 个答案:

答案 0 :(得分:0)

为什么不简单地合并两个表并添加一个布尔(tinyint)字段answered?然后你只需要将你的SQL请求更改为SELECT question FROM questions WHERE category = '$category' AND answered = 0