Jump to content

[SOLVED] Splitting up textarea forms into different variables


anolan13

Recommended Posts

Hi,

 

Using PHP and MySQL to get user information.

 

I have a textarea field (html) and  I ask for people to list their favorite animals, seperated by commas and spaces.

 

Example, a user may enter something like this:

 

monkeys, dogs, tigers

 

We all know though that this textarea is going to enter it into the database(MySQL) as one variable say...$animals.

 

and $animals =  monkeys, dogs, tigers 

 

What if I want it to automatically split those into three different variables?

 

$animal1 = monkeys

$animal2 = dogs

$animal3 = tigers

 

I imagine it would use loops, and something else. That something else is what confuses me. Can this even be done? I know this is a little confusing but any help would be greatly appreciated thank you.

 

 

Hey,

 

I think i can help.

 

<?php
$animal  = "monkeys dogs tigers";
$pieces = explode(" ", $animal);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
?>

 

Or if they're seperated by commas

<?php
$animal  = "monkeys,dogs,tigers";
$pieces = explode(",", $animal);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.