breaking data apart from HTML form textarea with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

breaking data apart from HTML form textarea with PHP

Post 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
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: breaking data apart from HTML form textarea with PHP

Post 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.
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Re: breaking data apart from HTML form textarea with PHP

Post 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
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: breaking data apart from HTML form textarea with PHP

Post 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);
irishmike2004
Forum Contributor
Posts: 119
Joined: Mon Nov 15, 2004 3:54 pm
Location: Lawrence, Kansas

Re: breaking data apart from HTML form textarea with PHP

Post 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>';
Post Reply