Reputation: 832
Sections.class
private void loadSection() {
final String username = SharedPrefManager.getInstance(getActivity()).getUsername();
final String userSection_URL = Constants.USER_SECTION + "?username=" + username;
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, userSection_URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject sections = (JSONObject) jsonArray.get(i);
sectionList.add(new ListGradeData(
sections.getInt("id"),
sections.getString("section"),
sections.getString("year_level"),
sections.getString("school_year")
));
//creating adapter object and setting it to recyclerview
LatestGradeAdapter adapter = new LatestGradeAdapter(getActivity(), sectionList);
recyclerView.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getContext(), "No Data", Toast.LENGTH_LONG).show();
}
// Stopping swipe refresh
mSwipeRefreshLayout.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Stopping swipe refresh
mSwipeRefreshLayout.setRefreshing(false);
headerSection.setVisibility(View.GONE);
noConnectionLayout.setVisibility(View.VISIBLE);
}
});
//adding our stringrequest to queue
Volley.newRequestQueue(getActivity().getApplicationContext()).add(jsonObjectRequest);
}
userSection.php
require_once "../include/Constants.php";
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if($_SERVER['REQUEST_METHOD']=='GET'){ $uname = trim($_GET['username']);
if(isset($uname)){
$query = "SELECT * FROM students WHERE s_id = '".$uname."' ";
$tbl = mysqli_query($conn, $query);
if(mysqli_num_rows($tbl)>0) {
$row = mysqli_fetch_array($tbl, MYSQLI_ASSOC);
$uid = $row['id'];
$stmt = $conn->prepare("SELECT students.id, sections.name AS section_name, years.name AS year_level, CONCAT(academic_years.start, '-', academic_years.end) AS school_year FROM students INNER JOIN section_student ON students.id = section_student.student_id INNER JOIN sections ON section_student.section_id = sections.id INNER JOIN years ON sections.year_id = years.id INNER JOIN academic_years ON sections.academic_year_id = academic_years.id WHERE students.id = ?");
$stmt->bind_param("s", $uname);
$stmt->execute();
$stmt->bind_result($id, $section, $year_level, $school_year);
$sections = array();
//traversing through all the result
while($stmt->fetch()){
$temp = array();
$temp['id'] = $id;
$temp['section'] = $section;
$temp['year_level'] = $year_level;
$temp['school_year'] = $school_year;
array_push($sections, $temp);
}
echo json_encode(array('data' => $sections));
}else {
echo json_encode(array('data' => "No Data"));
}
}
} PROBLEM SOLVED!
I have a problem, When it comes to retrieving a data.
I want to retrieve a information based on the username that current logged in.
But it doesn't work. I already try to display all the data, but it is not based on the current user logged in. It will display all the data. the main problem is How I will retrieve a data based on the current user logged in. my current user logged in is based on this code final String username = SharedPrefManager.getInstance(getActivity()).getUsername();
Upvotes: 0
Views: 69
Reputation: 5114
In your PHP code you are looking for a username
url parameter at $uname = trim($_GET['username']);
. But you are not sending it from Volley StringRequest. Your url is defined by Constants.USER_SECTION
. If this is the endpoint for the request you should add the parameter to the url with something like this:
String url = Constants.USER_SECTION + '?username=' + username; //considering that Constants.USER_SECTION doesn't have any url params
StringRequest stringRequest = new StringRequest(Request.Method.GET, url ,
new Response.Listener<String>() {
...
After fixing the errors above, debug your code with these steps:
final String username
value. If it is wrong, verify where you have saved it to SharedPreferences.url
value before StringRequest. If it is wrong check where Constants.USER_SECTION
is defined.response
from StringRequest. If it is wrong debug your server side code using a REST client of even a browser.Upvotes: 0
Reputation: 3228
I will suggest using JSONObjectRequest
. It will allow you to retrieve objects.
String url = YOUR_OWN_URL + "?username=" + username;
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
int id = jsonObject.getInt("id");
String section = jsonObject.getString("section");
String year_level = jsonObject.getString("year_level");
String school_year = jsonObject.getString("school_year");
// insert your own logic
}
} catch (JSONException e) {
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
And, in the PHP, I will change the json_encode
part into like this.
echo json_encode(array('data' => $sections));
Upvotes: 0