I am writing a code and where I need to read from a database the amount of quantities of each parts, the problem is that the column quantity is the same to all the parts and when I assign them to my Android Studio code it prints me the same Value for all the requested fields. I need help on how can I tell the app to get the other Quantity Query:
<?php
include "connection.php";
$consulta = "SELECT * from parts ; ";
$result = $con -> query($consulta);
while($fila = $result -> fetch_array()){
$usuarios[] = array_map('utf8_encode', $fila);
}
echo json_encode($usuarios);
$result -> close();
Output:
[
{"0":"1","idparts":"1","1":"resistor","partsName":"resistor","2":"13","Quantity":"13"},
{"0":"2","idparts":"2","1":"capacitor","partsName":"capacitor","2":"10","Quantity":"10"},
{"0":"3","idparts":"3","1":"inductor","partsName":"inductor","2":"10","Quantity":"10"},
{"0":"4","idparts":"4","1":"multimeter","partsName":"multimeter","2":"10","Quantity":"10"},
{"0":"5","idparts":"5","1":"arduino","partsName":"arduino","2":"10","Quantity":"10"},
{"0":"6","idparts":"6","1":"rasberry","partsName":"rasberry","2":"10","Quantity":"10"},
{"0":"8","idparts":"8","1":"wires","partsName":"wires","2":"10","Quantity":"10"},
{"0":"9","idparts":"9","1":"breadboard","partsName":"breadboard","2":"10","Quantity":"10"}
]
I want to display output of quantity on android studio
here is android studio code for reading from database:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_action);
logOut = findViewById(R.id.signOut);
resistor = findViewById(R.id.resistorQtyField);
capacitor = findViewById(R.id.capacitorField);
breadboard = findViewById(R.id.breadBoardField);
inductor = findViewById(R.id.inductorQtyField);
arduino = findViewById(R.id.arduinoQtyField);
rasberry = findViewById(R.id.rasberryField);
multimeter= findViewById(R.id.multimetroField);
wires = findViewById(R.id.wireField);
readInfo("http://192.168.0.16/APPEX/readInfo.php");
logOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent back = new Intent(ActionActivity.this, MainActivity.class);
startActivity(back);
}
});
}
private void readInfo(final String URL) {
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(URL, new
Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jsonObject;
for (int i = 0; i < response.length(); i++) {
try {
jsonObject = response.getJSONObject(i);
resistor.setText(jsonObject.getString("Quantity"));
capacitor.setText(jsonObject.getString("Quantity"));
breadboard.setText(jsonObject.getString("Quantity"));
inductor.setText(jsonObject.getString("Quantity"));
arduino.setText((jsonObject.getString("Quantity")));
multimeter.setText((jsonObject.getString("Quantity")));
rasberry.setText(jsonObject.getString("Quantity"));
wires.setText((jsonObject.getString("Quantity")));
} catch (JSONException e) {
Toast.makeText(getApplicationContext(),
e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
}
fetch_array
will by default return both a numeric array and an assoc array togther, you are just getting confused.
Instead us fetch_assoc
and you will only get the assoc array and it will all make more sense
while($fila = $result->fetch_assoc()){
$usuarios[] = array_map('utf8_encode', $fila);
}
Also from the json output you show, all but the first row, which has a
Quantity
of 13 have aQuantity
of 10.