Costin
Costin

Reputation: 224

How to store multiple rows within a variable, to use it as JSONArray in android studio

I am trying to mark some points on my maps app, points that have the coordinates stored in a database. I have managed to solve the java issues with the JSONArray, but my problem right now it's that the PHP sends from the database only one row, not all of them as an array. I have tried to store them in one variable, tried to implement implode, but still couldn't manage to send all the rows to the app. On the app, JSONArray is supposed to get the data from location.

.php code that sends only 1 row:

$problema = $_POST['problema'];

$stmt = $conn->prepare("SELECT latitudine, longitudine, tip_problema FROM alerte WHERE tip_problema = ?");
$stmt->bind_param("s", $problema);
$stmt->execute();
$stmt->store_result(); 
if($stmt->num_rows > 0){
    $stmt->bind_result($latitudine, $longitudine, $tip_problema);
    $stmt->fetch();

    $locatie = array(
    'latitudine'=>$latitudine,
    'longitudine'=>$longitudine,
    'tip_problema'=>$tip_problema
    );

    $response['error'] = false; 
    $response['message'] = 'Alerta raportata';
    $response['locatie'] = $locatie;    
}else{
    $response['error'] = true; 
    $response['message'] = 'Eroare de identificare a alertelor';
}

Logically, there should be some kind of while that would go row by row, but as I am a beginner in PHP I couldn't implement any on my own. Searched some ways, but nothing worked for me. Thanks!

JSON code:

@Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            //hiding the progressbar after completion

            try {
                //converting response to json object
                JSONObject obj = new JSONObject(s);

                //if no error in response
                if (!obj.getBoolean("error")) {
                    Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();

                    JSONArray locatieArray = obj.getJSONArray("locatie");
                    for (int i = 0; i < locatieArray.length(); i++) {
                        JSONObject locatie = locatieArray.getJSONObject(i);
                        // check latitudine and longitudine is not null and if not null then cast these values and call the addMarker() method.
                        if(!locatie.isNull("latitudine") && !locatie.isNull("longitudine")) {
                            latitudine_sql =Double.valueOf(locatie.getString("latitudine"));
                            longitudine_sql = Double.valueOf(locatie.getString("longitudine"));
                            addMarker(latitudine_sql, longitudine_sql); // this method is implemented below
                        }
                        tip_problema_sql = locatie.getString("tip_problema");
                    }

                } else {
                    Toast.makeText(getApplicationContext(), "Some error occurred", Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

Upvotes: 0

Views: 68

Answers (1)

user2342558
user2342558

Reputation: 6722

You must loop through results with a while loop and make $response a multidimensional array like this:

$problema = $_POST['problema'];

$stmt = $conn->prepare("SELECT latitudine, longitudine, tip_problema FROM alerte WHERE tip_problema = ?");
$stmt->bind_param("s", $problema);
$stmt->execute();
$stmt->store_result(); 
$response = array();
$x = 0;
if($stmt->num_rows > 0){
    $stmt->bind_result($latitudine, $longitudine, $tip_problema);

    while($stmt->fetch()) {
        $x++;
        $locatie = array(
        'latitudine'=>$latitudine,
        'longitudine'=>$longitudine,
        'tip_problema'=>$tip_problema
        );

        $response[$x]['error'] = false; 
        $response[$x]['message'] = 'Alerta raportata';
        $response[$x]['locatie'] = $locatie;    
    }
}else{
    $response['error'] = true; 
    $response['message'] = 'Eroare de identificare a alertelor';
}

Note that in case of 0 results, $response will be a monodimensional array.

You can learn more about while in the official documentation.

Upvotes: 2

Related Questions