简体   繁体   中英

How to save an array into a PHP database

I try to put array in database. In edittext I write for example garlic,tomatoes,onions but when I save data to database and go to my php database, instead of that text it only say ingredients. If I add text manually (i added number manually instead of ingredients but it's the same thing) to database it looks good and create good json file. How can I fix this?

MainActivity.java

String id;
    ArrayList<String> ingredients = new ArrayList<String>();

    @BindView(R.id.etId) EditText etId;
    @BindView(R.id.etIngredients) EditText etIngredients;



    id = etId.getText().toString();
    ingredients = etIngredients.getText().toString();

Recipes.java

public class Recipes {
    String id;
    List<String> ingredients;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<String> getIngredients() {
        return ingredients;
    }

    public void setIngredients(List<String> ingredients) {
        this.ingredients = ingredients;
    }
}

And how to retrieve that ingredients in a column?

I use this to return it from json file but it return ingredients in a row [1,3,4,5,6]

Reipes rec = recipes.get(position);
    holder.textViewId.setText(rec.getId());
    holder.textViewIngredients.setText(rec.getIngredients().toString());

my online database

First 4 recipes is added manually, and that where say ingredients is added in android app.

how it looks when get data in android

You will have to replace

ArrayList<String> ingredients = new ArrayList<String>();

by

String ingredients;

and in your recipies class try to change the type of ingredients

String ingredients;
public String getIngredients() {
    return ingredients;
}

public void setIngredients(String ingredients) {
    this.ingredients = ingredients;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM