Page 1 of 1

breaking data apart from HTML form textarea with PHP

Posted: Fri Feb 08, 2008 1:09 pm
by irishmike2004
Greetings:

I am working on a database driven application and this includes a form that the user submits.

I have a standard textarea in the form and i want to break the data out into an array (for insertion into a MySQL database) based on the return (\n).

In case my explanation is lacking, here is an example of what I would like to do:

here is the output of my form:

Code: Select all

echo $_POST[tracks];


outputs 1 some foo 4:05 2 more foo 5:01 3 even more foo 3:30 ...etc.

I am looking to break each of these items into their own variable like somearray[0]= 1 some foo 4:05 somearray[1] = 2 more foo 5:01 and so on...

The textarea should have returns between each track entered.

Can someone please help me write the code that will do this?

Thanks,

Mike

Re: breaking data apart from HTML form textarea with PHP

Posted: Fri Feb 08, 2008 1:21 pm
by Inkyskin
You could always try this:

Code: Select all

$array = explode("\n",$_POST[tracks]);
print_r($array);
But that depends on the new lines being there. If they are not, you might need to use regex instead. Infact, that would be best all round as you dont have to worry if the new line element isnt there.

Re: breaking data apart from HTML form textarea with PHP

Posted: Fri Feb 08, 2008 1:42 pm
by irishmike2004
@inkyskin

Thanks. The code worked great.

My next question is;

How do I get an accurate count from the array... there is going to be further breakout from the idea, so if there is a better way to do this other than the explode, I am interested (but that does work).

I got the array element count using the count(); function. Seems that if the user enters a final return this throws off the count quite a bit, so I need some logic to see if the next thing is null or not I suppose. For example:

if I have 3 elements filled in the array and I run the count() function, it returns 4 which would mess up my loop for entering the query later on. To get the array count to be zero I already had to put:

Code: Select all

$trackCount = count($array) -1;
but again this is an issue seemingly only if there is a last return like item 1 item 2 item 3 and then another "\n".

Hope this makes sense :-)

Thanks for the help!!!

Mike



Mike

Re: breaking data apart from HTML form textarea with PHP

Posted: Fri Feb 08, 2008 2:42 pm
by Inkyskin
This will trim off any extra line or spaces etc

Code: Select all

$_POST[tracks] = trim($_POST[tracks]);
$array = explode("\n",$_POST[tracks]);
print_r($array);

Re: breaking data apart from HTML form textarea with PHP

Posted: Fri Feb 08, 2008 2:58 pm
by irishmike2004
@inkyskin

Thanks a lot dude!

Just for everyone's enjoyment, here is that section of code which is working very well:

Code: Select all

  $_POST[tracks] = trim($_POST[tracks]);
   $tracksArray = explode("\n",$_POST[tracks]);
   $trackCount = count($tracksArray);
  
   if($tracksArray[0] == NULL || null){
        $trackCount = $trackCount - 1;
   }
   
   print_r($tracksArray);
   echo 'Number of Tracks: '.$trackCount.'<br>';