首页 > 解决方案 > 如何根据当前登录的用户显示所有数据

问题描述

用户节.php

<?php  
require_once '../include/DbOperations.php';

$response = array(); 
if($_SERVER['REQUEST_METHOD']=='GET'){
    $uname = trim($_GET['username']);
    if(isset($uname)){
       $db = new DbOperations(); 

    if($db->studentLogin($uname)){
        $sections = $db->studentSection($uname);
        $response['error'] = false;         
        $response['id'] = $sections['id'];
        $response['lastname'] = $sections['lastname'];
        $response['section'] = $sections['section'];
        $response['year_level'] = $sections['year_level'];
        $response['school_year'] = $sections['school_year'];

    }else{
        $response['error'] = true; 
        $response['message'] = "No Data";          
    }

}else{
    $response['error'] = true; 
    $response['message'] = "Required fields are missing";
}
}
echo json_encode($response);

sectionList.class

StringRequest stringRequest = new StringRequest(Request.Method.GET, Constants.USER_GRADE,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                  //it should display the data of current user id
                  //On this part it doesn't display anything.
                  //example `id`=`1`. it should display all `id` equals to `1`

                            //converting the string to json array object
                            JSONArray array = new JSONArray(response);
                            if(sectionList!=null) {
                                sectionList.clear();
                            }
                                //traversing through all the object
                                for (int i = 0; i < array.length(); i++) {

                                //getting product object from json array
                                JSONObject sections = array.getJSONObject(i);

                                if (!sections.getBoolean("error")) {
                                        //adding the product to product list
                                        sectionList.add(new ListGradeData(
                                                sections.getInt("id"),
                                                sections.getString("section"),
                                                sections.getString("level"),
                                                sections.getString("schoolyear")
                                        ));
                                }
                            }

                        //creating adapter object and setting it to recyclerview
                        LatestGradeAdapter adapter = new LatestGradeAdapter(getActivity(), sectionList);
                        recyclerView.setAdapter(adapter);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    //adding our stringrequest to queue
    Volley.newRequestQueue(getActivity().getApplicationContext()).add(stringRequest);

如何显示id=的所有数据1。我当前的用户名id1. 但每次我运行应用程序时它都不会显示任何空白。我也试着把这个,

{
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("username", username);
                return params;
            }

        };

但它仍然对我不起作用。

标签: phpandroidjsonandroid-volley

解决方案


volley 中的GET请求参数作为url的一部分传递,因此将 url 更改为

String url = "http://youraddress.com/userSection.php?username=" + username;

通过getParams()方法传递参数是在POST请求中完成的。


推荐阅读