[英]Select all option values php mysql
I'm trying to select all values from my MySQL database. 我正在尝试从MySQL数据库中选择所有值。 Options a, b, and c work fine but I'm not sure of the syntax to select all three.
选项a,b和c都可以正常工作,但是我不确定选择这三个选项的语法。
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
<option value="1,2,3">All</option>
I think you want to use the select to fetch a item or all items if I understand your question correctly and by seeing your 'all' option's value. 我认为如果我正确理解了您的问题并查看了“全部”选项的值,则您想使用select来获取一个或所有项目。
If so then change your select option's value for all to <option value="all">all items</option>
. 如果是这样,则将所有您的选择选项的值更改为
<option value="all">all items</option>
。
Then change your PHP file (where you posting to with the form) to this: 然后将您的PHP文件(使用表单发布到的文件)更改为:
// is the all option send?
if($_POST['your_select'] == 'all') {
//query to get all the items (SELECT * FROM table)
} else {
// query with the post value as the id (SELECT * FROM table WHERE id = $_POST['your_select'])
}
Try this 尝试这个
<form action="my_page.php" method="post">
<select name="my_select">
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
<option value="1,2,3">All</option>
</select>
<input type="submit" name="submit" value="Submit" />
</form>
<?php
# in my_page.php page
# put submitted value of the select tag in an array
# (submitted value in this case equals "1", "2", "3" or "1,2,3")
$values = explode(",", $_POST["my_select"]);
# get number of values in the array
$num_of_values = count($values);
# escape all values before using them in your sql statement
foreach ($values as $key => $val) {
$values["$key"] = mysql_real_escape_string($val);
}
# if we have more than 1 value in the array
if (count($values) > 1) {
$sql = "SELECT * FROM table_name WHERE "; # note the space after "WHERE" keyword
for ($i = 0; $i < $num_of_values; $i++) {
# this "if" statement is for removing the "OR" keyword from the sql statement
# when we reach the last value of the array
if ($i != $num_of_values - 1) {
$sql .= "column_name = '{$values[$i]}' OR "; # note the space after "OR"
} else { #if we reached the last value of the array then remove the "OR" keyword
$sql .= "column_name = '{$values[$i]}'";
}
}
# execute your query
$result = mysql_query($sql);
} else { # if we have only one value in the array
$result = mysql_query("SELECT * FROM table_name WHERE column_name = '{$values[0]}'");
}
?>
i think you want multiple="multiple"
it will allow you to select multiple 我认为您想要
multiple="multiple"
,它将允许您选择多个
<select name="modules[]" multiple="multiple">
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
<option value="1,2,3">All</option>
</select>
now you will get array of selected option which you can get by either GET
or POST
现在您将获得选定选项的数组,可以通过
GET
或POST
to select all on selecting last you can use jquery like 要选择所有最后选择您可以使用jQuery的喜欢
$('option').click(function(){
if($(this).val() =='1,2,3'){
$("option").attr("selected", "selected");
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.