-1

I have a textarea where I can enter as many items as I want separated by a line break.

Then, in the php file I have this to get the values:

$colors = $_POST["colors"];

foreach ( preg_split ('/[\s*,\s*]*,+[\s*,\s*]*/', $colors) as $value)
{

    echo $value.'<br>';

}

The items are passed correctly as with that code I can see all the items but in one line. I tried to store that in the database and it does, but only in one row.

For example, if I enter in the textarea this:

Red
   Black
  Dark Black
Blue    

It prints like this:

Red Black Dark Black Blue 

That is, it removes all the spaces at the beginning and at the end, but not the ones in the middle (which is exactly what I want).

My problem is in the way I deal with the array. I would like to print an element per line and therefore being able to store in the database one color per row.

Can anybody please help?

Thanks a lot!

cHao
  • 84,970
  • 20
  • 145
  • 172
saysmus
  • 81
  • 1
  • 1
  • 12
  • You mention array, but there is no array in your question. Can you please clarify the question? – random_user_name Dec 13 '17 at 14:57
  • _“That is, it removes all the spaces at the beginning and at the end”_ - no, they have not actually been removed - you are just viewing this in a context now that does not preserve whitespace, but collapses it to one single space character when it gets displayed (a.k.a. normal HTML behavior.) – CBroe Dec 13 '17 at 15:02
  • 1
    Besides that, it is rather unclear what you are actually trying to ask here. You already have a loop, and inside you have access to each such “line” on its own. So if you want to write it to the database ... all you need to do is actually do that. What is the _problem_ you are having with that? – CBroe Dec 13 '17 at 15:03

1 Answers1

1

In your example $value is a string. If you want to insert data to the array you need to :

$myarray[]=$value;

The fact that you can echo it first place means that is not an array. So in your foreach you add the line of code i provide above. After that you can use print_r($myarray) outside of the foreach and see the structure of your array.

But according to your code you can execute your query inside the foreach and store your values in the database. No need an array for that.

So you can have something like this:

1st way:

<?php
$colors = $_POST["colors"];

//declare your array
$myArray=array();

foreach ( preg_split ('/[\s*,\s*]*,+[\s*,\s*]*/', $colors) as $value)
{
    //insert data inside your array
    $myArray[]=$value;

}

//output of your new array
print_r($myArray);

Or simpler in the 2nd way:

<?php
$colors = $_POST["colors"];

$servername = "my_server_name";
$username = "username";
$password = "password";
$dbname = "my_db_name";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

foreach ( preg_split ('/[\s*,\s*]*,+[\s*,\s*]*/', $colors) as $value)
{

    //$value variable holds all the info you need so you just do the INSERT QUERY inside the foreach

    $stmt = $conn->prepare("INSERT INTO my_table (my_column) VALUES (?)");
    $stmt->bind_param("s", $value);
    $stmt->execute();



}

You just do the query inside the foreach. Make sure you use prepared statements.

pr1nc3
  • 8,108
  • 3
  • 23
  • 36
  • 2
    Nice start of an answer. I'd recommend making it a bit more robust. For example, you need to declare the array variable in the first place to avoid a PHP notice.... and it would be useful to OP to see _where_ your code would go into _their_ code. – random_user_name Dec 13 '17 at 15:04
  • I followed your advice. I tried to be as analytic as i could. – pr1nc3 Dec 13 '17 at 15:13