I'm currently struggling of how to correctly display the details from a table that is linked to another table in PHP. Right now my Android code have multiple fragment of displaying different details, and I can get to display those information from php into android code, but when I login the username, those details are shown differently.
Lets say I have profile fragment, balance fragment and record fragment. Right now when I log into the given username, those fragments are display other user details.
Additionally, in my PHP server, I have created 3 tables, each table are linked to one another. In my PHP code I create separate file for each table, because I follow some guide on the internet that can fetch data from php.
Table 1:
Table 2
This is balance.php code:
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','ips');
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "select * from account_details";
$res = mysqli_query($con,$sql);
$result = array();
$row = mysqli_fetch_assoc($res);
array_push($result,
array(
"Balance"=>$row['Balance'],
));
echo json_encode(array("result"=>$result));
mysqli_close($con);
?>
This is parking_record.php code
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','ips');
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "select * from parking_record";
$res = mysqli_query($con,$sql);
$result = array();
$row = mysqli_fetch_assoc($res);
array_push($result,
array(
"Parking_Amount"=>$row['Parking_Amount'],
"Date_Time"=>$row['Date_Time'],
));
echo json_encode(array("result"=>$result));
mysqli_close($con);
?>
Finally this is the profile_details.php code
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','ips');
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "select * from profile_details";
$res = mysqli_query($con,$sql);
$result = array();
$row = mysqli_fetch_assoc($res);
array_push($result,
array(
"FullName"=>$row['FullName'],
"DateOfBirth"=>$row['DateOfBirth'],
));
echo json_encode(array("result"=>$result));
mysqli_close($con);
?>
Android code
Balance.java
public class dri_balance extends ArrayAdapter<String> {
private String[] Acc_Balance;
private Activity context;
public dri_balance(Activity context, String[] Acc_Balance) {
super(context, R.layout.fragment_balance, Acc_Balance);
this.context = context;
this.Acc_Balance = Acc_Balance;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.fragment_balance, null,
true);
TextView tvBlnf = (TextView) listViewItem.findViewById(R.id.tvBlnf);
tvBlnf.setText(Acc_Balance[position]);
return listViewItem;
}
}
BalanceFragment.java
public class BalanceFragment extends Fragment implements
View.OnClickListener {
public BalanceFragment() {
// Required empty public constructor
}
public static final String JSON_URL =
"http://192.168.1.2/json_balance_records.php";
private Button buttonGet;
private ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.activity_listview, container,
false);
buttonGet = (Button) rootView.findViewById(R.id.buttonGet);
buttonGet.setOnClickListener(this);
listView = (ListView) rootView.findViewById(R.id.listView);
return rootView;
//return inflater.inflate(R.layout.fragment_profile, container, false);
}
private void sendRequest(){
StringRequest stringRequest = new StringRequest(JSON_URL, new
Response.Listener<String>() {
@Override
public void onResponse(String response) {
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity().getApplicationContext(),error.getMessage(),
Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue =
Volley.newRequestQueue(getActivity().getApplicationContext());
requestQueue.add(stringRequest);
}
private void showJSON(String json){
Dri_Balance pj = new Dri_Balance(json);
pj.parseJSON();
dri_balance cl = new dri_balance(getActivity(),
Dri_Balance.Acc_Balance);
listView.setAdapter(cl);
}
@Override
public void onClick(View v) {
sendRequest();
}
}
parseJson Balance.java
public class Dri_Balance {
public static String[] Acc_Balance;
public static final String JSON_ARRAY = "result";
public static final String KEY_DriBalance = "Acc_Balance";
private JSONArray users = null;
private String json;
public Dri_Balance(String json){
this.json = json;
}
public void parseJSON(){
JSONObject jsonObject=null;
try {
jsonObject = new JSONObject(json);
users = jsonObject.getJSONArray(JSON_ARRAY);
Acc_Balance = new String[users.length()];
for(int i=0;i<users.length();i++){
JSONObject jo = users.getJSONObject(i);
Acc_Balance[i] = jo.getString(KEY_DriBalance);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
How can show the correct data in Android?
I'm sorry for asking this too much, I did search for the internet but it didn't give me what I wanted. I am aware that I'm asking someone to do the homework for me but I am very new to Android and PHP.
New PHP code
I've edited the balance.php code (version 2):
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','ips');
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "select Balance from account_details WHERE Username='$Username'";
$res = mysqli_query($con,$sql);
$result = array();
$row = mysqli_fetch_assoc($res);
array_push($result,
array(
"Balance"=>$row['Balance'],
));
echo json_encode(array("result"=>$result));
mysqli_close($con);
?>
I try to run the php file it gave me null value
{
"result": [{
"Balance": null
}]
}
Edited balance.php code (version 3)
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','ips');
$Username = $_GET["Username"];
$mysqli = mysqli_connect(HOST,USER,PASS,DB);
$stmt = $mysqli->prepare ("SELECT Balance from account_details WHERE
Username=?");
$stmt->bind_param('s', $Username);
$stmt->execute();
$stmt->bind_result($Balance);
$rows = array();
while($stmt->fetch()) {
array_push($rows, array('Balance'=>$Balance));
}
$stmt->close();
$mysqli->close();
echo json_encode($rows);
?>
Result
[{"Balance":"50.512"}]
OK, I have successfully joined the table in PHP and can get the result that I wanted. Right now I try to run my Android code, it crashes when I click to display the data, how do I correct the code?