php - 如何根据当前登录用户显示所有数据

标签 php android json android-volley

用户部分.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;
            }

        };

但它仍然对我不起作用。

最佳答案

volley中的GET请求参数是作为url的一部分传递的,所以把url改成

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

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

关于php - 如何根据当前登录用户显示所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52415994/

相关文章:

php - 将 PHP 变量插入到 Html5 "Placeholder"属性中

jquery - 如何使用 jQuery 操作 JSON 多维数组

javascript - 运行代码来单击 Android 应用程序中的 javascript 按钮?尝试抓取 craigslist.org 页面上的回复按钮联系信息

json - 如何从存储过程捕获 JSON 数据以使用 tSQLt 进行测试

sql - 从 postgreSQL 中的 JSON 数组获取单个元素

php - 在存储定位器 mysql 查询中添加 where 子句

java - 在我的 Elasticsearch 查询中将逗号放在哪里

php - 模式搜索的 Ajax 回调问题

android - 计算丢包、抖动和延迟

android - picasso 没有将 URL 加载到图像中