开发者

php, how to use GET to grab and display an array into a drop down?

this question has actually 2 parts

i have some vars: $cat = 41; $cat = 2; $cat = 3; and i want to be able to pass more than one as an array here $next_page .= "?cat=开发者_如何学Go [] "; so that the final get to look like this

http://www.xxx.com/csxxx.php?&cat=3, 41

what i actually want to do is to pass this integers to a drop down menu as a multiple selection.

for that i got this function:

<?
function display($cat, $name)
{?>
<select name="<?=$name?>">

<option value=<?php if ($cat == "2" ) {echo"2 selected";} else {echo"0";}?>> 2</option>
<option value=<?php if ($cat == "41") {echo"41 selected";} else {echo"41";}?>>41</option>
<option value=<?php if ($cat == "3") {echo"3 selected";} else {echo"1";}?>>3</option>
<? 
if ($cat == 'xx') // any
{
print "<option value=\"\" selected>Any</option>";
}
?>
</select>
<?
}
?>

any ideas? thanks


You need to do something like this:

<select name="test[]" multiple="multiple">
//> option

So you can parse it with:

 foreach ($_POST['test'] as $t){
   echo 'You selected ',$t,'<br />';
 }


If you want use only native functional, you must change code of dropdown to this look

<select multiple="multiple" name="cat[]">
   <option value="1">first</option>
   <option value="2">second</option>
   <option value="3">third</option>
</select>

add to name "[]" and multiple option

In this case you have url page?cat[]=1&cat[]=2...

And available in the $_GET['cat'] as an array.

If you need comma separated "cat" in url you must use javascript.

Exaple for jQuery.

<form id="multiform">
    <select multiple="multiple" name="_cat[]" id="cat-list">
        <option value="1">first</option>
        <option value="2">second</option>
        <option value="3">third</option>
    </select>
    <input type="hidden" name="cat" id="hidden-cat"/>
    <input type="submit" value="Send" />
</form>
<script>
    $("#multiform").bind("submit",function(){
        var new_val = $("#cat-list").val().join(",");//joins array by comma
        $("#cat-list").val("");//cleans temporary variable
        $("#hidden-cat").val(new_val);
    });
</script>


If my understanding is correct then $cat is a comma delimited string of integers. You could use PHP's explode function to convert this into an array and then use in_array to test to see if that value is in the array:

<?
function display($cat, $name)

$cat = explode(',', $cat);

{?>
<select name="<?=$name?>">

<option value=<?php if (in_array("2", $cat)) {echo"2 selected";} else {echo"0";}?>> 2</option>
<option value=<?php if (in_array("41", $cat)) {echo"41 selected";} else {echo"41";}?>>41</option>
<option value=<?php if (in_array("3", $cat)) {echo"3 selected";} else {echo"1";}?>>3</option>
<? 
if (in_array("xx", $cat)) // any
{
print "<option value=\"\" selected>Any</option>";
}
?>
</select>
<?
}
?>

Equally you could use implode with an existing $cat array to create a comma delimited string that you could output as part of a url.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜