Luis
Luis

Reputation: 235

Separating textarea line breaks into different database rows

I have a textarea form input where users input ingredients for a recipe. What I originally had was a dynamic, Javascript based 'add an ingredient' field but realised this was not good for accessibility. Originally I was separating each input field out into it's own row in my database and was wondering how I could do this with a textarea?

What I assume is done is separating out each line break and placing it into it's own row in the table but I do not know the syntax for that.

Further Details

I'm using PHP and processing the textarea with a 'post' variable.

The reason for wanting to put each ingredient in it's own row is because I've been informed that using relational databases for unpredictable amounts of text is the way to go.

Can anyone help?

Answer

$variable = $_POST['fieldName'];

$variable_array = split("\n", $variable);
foreach($variable_array as $for)
    {
Insert into db
    }

Upvotes: 2

Views: 1540

Answers (1)

schellmax
schellmax

Reputation: 6094

you can split the text by the linebreaks:

$data_array = split("\n", $data);

so now you have an array to loop over, saving each array item into the db separately

Upvotes: 2

Related Questions