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

Recommended Answers

All 7 Replies

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 .

commented: helpful +5

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.

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.

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 ?

Yeah, echoing stuff is hard indeed. Have you tried this:

echo $value_for_db;

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.