简体   繁体   中英

Select all option values php mysql

I'm trying to select all values from my MySQL database. Options a, b, and c work fine but I'm not sure of the syntax to select all three.

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

If so then change your select option's value for all to <option value="all">all items</option> .

Then change your PHP file (where you posting to with the form) to this:

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

<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

to select all on selecting last you can use jquery like

$('option').click(function(){
   if($(this).val() =='1,2,3'){
    $("option").attr("selected", "selected");
   }

})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM