Once again foreach lops are driving me mad.
I have a simple multiple select list, where ther user can select one or all of the options
<form name="checkdata" id="checkdata" method="post" action="test.php">
<label>
<select name="sport[]" size="6" multiple="multiple">
<option value="FOOTBALL">Football</option>
<option value="CRICKET">Cricket</option>
<option value="TENNIS">Tennis</option>
<option value="SWIMMING">Swimming</option>
</select>
</label>
<input type="submit" name="submit" value="submit" />
</form>
I want to prepare the user selection to insert into my database as a variable, however, I do not know how to manipulate my data to get my results.
What I want to achieve, is to extract each value from the array(sports), separate each value with a comma, and merge each value into one variable so I can add it to a single database column.
For example, if the user selects all sports, I would like to get values defined in one variable
$sports="FOOTBALL,CRICKET,TENNIS,SWIMMING";
I have tried using the following, but of course, the variables overwrite each other every time the loop executes.
$string=$_POST['sport'];
$i=0;
foreach ($string as $value)
{
$num=$i++;
$num.$value_for_db=$value;
}
$sports=$num.$value_for_db;
I have even tried the following, but get an error.
$string=$_POST['sport'];
foreach ($string as $value)
{
$value_for_db[]=$value;
}
$sports=$num.$value_for_db[];
PLEASE HELP!!!!