0

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!!!!

3
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by twiss
1

I don't really understand the logic in both of the snippets, but anyway, there's a function in php that "joins" an array to a string: implode .

Votes + Comments
helpful
0

I don't really understand the logic in both of the snippets, but anyway, there's a function in php that "joins" an array to a string: implode .

Many thanks twiss,
This is a great function that suits my purpose exactly.

0

For anyone looking on, the implode function that twiss suggested allows me to express the array created by the multi select element, as a string, so I can add the string directly to my database.

If I select all options from the multi select form (below)

<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>

and process as, below

$string=$_POST['sport'];
$value_for_db=implode(",",$string);

The value of the variable $value_for_db (FOOTBALL,CRICKET,TENNIS,SWIMMING) can be used in my mysql statement.

Thank you twiss.

0

Thanks for the info Thorby. Could you share the full php code. Because i don't get that how you echo (or print) the variable $value_for_db ?

0

Yes but it prompts comma seperated values. What i would like to do is, in wordpress, when i select football and cricket, showing all posts those have taxonomy terms football OR cricket. I had a look at query_posts page but nothing make any sense about my situation.

0

Ah, didn't see that you already opened a thread on this. But, please don't ask the same question in other threads, too.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.