1

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>
4
  • 2
    Is it the HTML you're struggling with or the PHP / MySQL code? Commented Dec 5, 2012 at 13:11
  • I think he's trying to ask how to get all the items form the db. I think he uses something like: SELECT * FROM table WHERE id = ' . $_POST['form_select_val'] . '. in the php file. Check the value of the ALL option, Commented Dec 5, 2012 at 13:12
  • Are u trying to select <option value="1,2,3">All</option> Commented Dec 5, 2012 at 13:13
  • I'm struggling with the HTML bit. Maybe it can be fixed using php/mysql too though. Commented Dec 5, 2012 at 13:15

3 Answers 3

5

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'])
}
Sign up to request clarification or add additional context in comments.

Comments

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");
   }

})

4 Comments

I think he wants to select <option value="1,2,3">All</option>
No luck there, unfortunately .
I want a fourth option to select all three categories.
This advice, while well-intended, extends the functionality beyond what the OP has posted. This would allow people to select 1 and 2 without 3 OR 1 and 1,2,3.
0

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]}'");
}

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.