In ASP, the multiple-select Drop-down list has been processed into an array, such as:
<select name= "Sltype" id= "Sltype" multiple= "multiple" >
<option value= "News" >news</option>
<option value= "Product" >product</option>
<option value= "User" >user</option>
</select>
Press CTRL to select the news and product options, click the Submit button, and use Request ("Sltype") to return the Sltype value and get news,product.
However, in PHP, when holding down CTRL multiple selections, use $_post[' sltype ' to always return the last selected value, how do you get all the values?
First, name the multiple-select Drop-down list as an array, such as:
<select name= "sltype[]" id= "sltype[" multiple= "multiple" >
<option value= "News" >news</option>
<option value= "Product" >product</option>
<option value= "User" >user</option>
</select>
Second, use a foreach loop
$temp = ","//Why initialize the TEMP variable to ",", and then explain why
foreach ($_post[' strtype '] as $key => $value)
{
$temp = $temp. $value. ",";
}
If multiple options are selected entirely, the final $temp return value is News,product,user, in which case it is convenient to integrate the values from the multiple-selection Drop-down list into News,product,user, and then enter the value into the database.
When editing a multiple-selection box, when you read multiple options from the database, how to make some options selected. This problem can be solved well by the explode function.
For example, the value of Sltype from the database is ", Product,user," and assigns it to $temp,
<select name= "sltype[]" id= "sltype[" multiple= "multiple" >
<option value= "News". if ($count =count (Explode (", news, $temp)) > 1) {echo" selected ";}? >>news</option>
<option value= "Product" if ($count =count (Explode (", product,, $temp)) > 1) {echo" selected ";}? >>product</option>
<option value= "User" if ($count =count (Explode (", user,, $temp)) > 1) {echo" selected ";}? >>user</option>
</select>
This allows the product and user options to be selected by default at the time of editing. Why add a "," to the front, because the accuracy is higher when you split the array, and if you do not add this symbol, you will split the string with "user," so if you have an option in the database that is Vipuser, The Drop-down list will not only be vipuser when edited, but the user option will also be selected.
Report:
The foreach (as $key => $value) function traverses the array_expression array, in which the value of the current cell is assigned to the $value, the key value of the current cell is assigned to the variable $key, and the pointer inside the array moves forward one step at a time.
Explode
Explode
(PHP 4, PHP 5)
explode-use a string to split another string
Description
Array explode (string $separator, string $string [, int $limit])
This function returns an array of strings, each of which is a substring of string, which is split by separator as a boundary point. If the limit parameter is set, the returned array contains up to limit elements, and the last element will contain the remainder of the string.
If separator is an empty string (""), Explode () returns FALSE. If the value contained in separator is not found in string, explode () returns an array that contains a single element of string.
If the limit parameter is a negative number, all elements except the last-limit element are returned. This feature is new in the PHP 5.1.0.
For historical reasons, although implode () can receive two parameter orders, explode () is not. You must ensure that the separator parameter is not preceded by a string parameter.
Note: The parameter limit is added in the PHP 4.0.1.
Example 2301. Explode () example
<?php
Example 1
$pizza = "Piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = Explode ("", $pizza);
echo $pieces [0]; Piece1
echo $pieces [1]; Piece2
Example 2
$data = "Foo:*:1023:1000::/home/foo:/bin/sh";
List ($user, $pass, $uid, $gid, $gecos, $home, $shell) = Explode (":", $data);
Echo $user; Foo
Echo $pass; // *
?>
Example 2302. Limit parameter Example
<?php
$str = ' One|two|three|four ';
Limit of positive numbers
Print_r (Explode (' | ', $STR, 2));
Negative limit (from PHP 5.1)
Print_r (Explode (' | ', $STR,-1));
?>
The example above will output:
Array ([0] => one [1] => Two|three|four) Array ([0] => one [1] => two [2] => three)