Jump to content

Multi select


Kay1021

Recommended Posts

I thought i was all set but now i've been faced with a problem and i'm not sure the best way to solve it.

 

I have a table in my db with the columns

 

cat_name and school

 

In the admin section when you were assigning a school to a category i was using a drop down select having the option of all or choosing one particular school from a list.

 

and when i wanted to show things depending on the school the user chose it would be WHERE school ='All' OR school=$school

 

 

But now i need to modify it so that when you are assigning a school to a category that instead of a select list i need to have a multi select list....so you could choose all or any combination of schools

 

I've never really used a multi select but i know it puts it into an array

 

The question is what is the best way to put those into my database, and then be able to them in a statement like WHERE school ='All' OR school=$school

 

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/164891-multi-select/
Share on other sites

Use

<select name="school" multiple="multiple">

<option value="All" selected="selected">All Schools</option>

<option value="school1">School1</option>

etc...

</select>


$query = "SELECT * FROM table";

if ( !empty($_POST['school']) )

{
   $query .= " WHERE school = '" . implode("' OR school = '", $_POST['school']) . "' ";
}

$result = mysql_query($query);

Link to comment
https://forums.phpfreaks.com/topic/164891-multi-select/#findComment-869535
Share on other sites

<script type="text/javascript">
<!--
   function selectAll()
   {
      var elem = document.getElementById('select');
         for ( var i = 0; i < elem.length; i++ )
         {
            elem.options[i].selected = 'selected';
         }
   }
-->
</script>
<form action="action.php" method="post">
<input type="text" name="school" etc... ><br >
<input type="checkbox" onclick="selectAll()">Select All Categories
<select name="cat[]" multiple="multiple" id="select">
<option value="cat1">First Category</option>
<option value="cat2">Second Category</option>
</select><br >
<input type="submit" name="submit" value="Submit Data" ><br >
<input type="reset" value="Clear Form" >
</form>

 

if ( isSet($_POST['submit']) )
{
   $school = mysql_real_escape_string($_POST['school']);

   foreach($_POST['cat'] As &$val)
   {
      $query = "INSERT INTO table (school, cat_name)
                VALUES
               ( '" . $school . "', '" . mysql_real_escape_string($val) . "' )";
      mysql_query($query);
   }
}

Something along those lines?

Link to comment
https://forums.phpfreaks.com/topic/164891-multi-select/#findComment-870109
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.