Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I made the following

PHP
$dropdown = "<form action='get'><select name='Test' multiple='multiple'>";
while($row = mysql_fetch_assoc($result)) {
$dropdown .= "\r\n<option value='{$row['Test']}'>{$row['Test']}</option>";
}
$dropdown .= "\r\n</select><input type='submit' name='submit' value='submit' /></form>";

now I am using
PHP
$query2 = "SELECT * FROM tests WHERE id='".$_GET['Test']."'"; 

now what i have to use instead of $_GET['Test']for multiple selection of combo box
Help would be appreciated,

thanks.
Posted
Updated 10-Jan-18 19:22pm
v2

Sure, does this code help?

Note: The [] chars in the name of the select element ARE required.
Selecting all options gives the output of: (with the combo below that)
1
2
3
4


PHP
<?php
    if (isset( $_GET['test']))
    {
        foreach ($_GET['test'] as $selectedOption)
            echo $selectedOption."<br>";
    }
?>

<html>
<head>
</head>
<body>
    <form action='testCombo.php'>
        <select name='test[]' multiple='multiple'>
            <option value='1'>a</option>
            <option value='2'>b</option>
            <option value='3'>c</option>
            <option value='4'>d</option>
        </select>
        <input type='submit' value='submit'/>
    </form>
</body>
 
Share this answer
 
Comments
project virus 13-Aug-12 3:33am    
huh, sorry dnt understand what u want say.
I have to retrieve values using that multiple options
enhzflep 13-Aug-12 3:54am    
Save the thing as a file and run it. Surely you can see that all of the selected options are reported?

That code will extract the selected options.
You now have to formulate a new query using the info!

$sql = "select * from tests where id='1' or id='2' or id='3' or id='4'"

You need to work out how to construct the [id='1' or id='2' or id='3' or id='4'] part from the number of options that were selected.

If you put the results of $_GET['test'] into an array, you know how many elements you have and how many times you need to add OR after each id='xx' statement.

$i=0;
foreach $_GET['test] as $selectedOption
{
$options[$i++] = $selectedOption;
}

$sql = "select * from Tests where ";
$num = count($options);
for ($i=0; $i<$num-1; $i++)
{
$sql .= "id='".$options[$i]."' OR ";
}
$sql .= "id='".$options[$i]."'";
project virus 13-Aug-12 4:43am    
what is $options??
enhzflep 13-Aug-12 4:51am    
an array to hold each of the <option>s that were selected in the <select> element.
It's defined and used on the same line.
enhzflep 13-Aug-12 4:56am    
Here's an update to the Solution:

<?php
if (isset( $_GET['test']))
{
$i = 0;
foreach ($_GET['test'] as $selectedOption)
{
$options[$i++] = $selectedOption;
}

$sql = "select * from Test where ";
$num = count($options);
for ($i=0; $i<$num-1; $i++)
{
$sql .= "id='".$options[$i]."' OR ";
}
$sql .= "id='".$options[$i]."'";

echo $sql . "<br>";
}
?>

<html>
<head>
</head>
<body>
<form action='testCombo.php'>
<select name='test[]' multiple='multiple'>
<option value='1'>a</option>
<option value='2'>b</option>
<option value='3'>c</option>
<option value='4'>d</option>
</select>
<input type='submit' value='submit'/>
</form>
</body>

Output: - All 4 options selected
select * from Test where id='1' OR id='2' OR id='3' OR id='4'
Okay then. Let's see what we have here.
I think you should be able to solve it from here.

Here's the import.sql file I have exported
SQL
-- phpMyAdmin SQL Dump
-- version 3.2.4
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Aug 13, 2012 at 10:43 PM
-- Server version: 5.1.41
-- PHP Version: 5.3.1

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `test`
--

-- --------------------------------------------------------

--
-- Table structure for table `import`
--

CREATE TABLE IF NOT EXISTS `import` (
  `id` int(11) NOT NULL,
  `srno` int(11) NOT NULL,
  `date` varchar(64) NOT NULL,
  `mobno` int(11) NOT NULL,
  `city` varchar(64) NOT NULL,
  `state` varchar(64) NOT NULL,
  `type` varchar(64) NOT NULL,
  `telecaller` varchar(64) NOT NULL,
  `date1` varchar(64) NOT NULL,
  `time` varchar(64) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `import`
--

INSERT INTO `import` (`id`, `srno`, `date`, `mobno`, `city`, `state`, `type`, `telecaller`, `date1`, `time`) VALUES
(0, 11, '27-Jul-2012', 2147483647, '', 'Maharashtra', 'ADMIN', 'Sitara', '7-Aug-2012', '12:48.PM'),
(0, 12, '27-Jul-2012', 2147483647, '', 'Andhra Pradesh', 'ADMIN', 'Sitara\r', '7-Aug-2012', '12:49.PM'),
(0, 13, '27-Jul-2012', 2147483647, '', 'Kerala', 'ADMIN', 'Snehal\r', '7-Aug-2012', '12:49.PM'),
(0, 14, '27-Jul-2012', 2147483647, '', 'Maharashtra', 'ADMIN', 'Sitara\r', '7-Aug-2012', '12:49.PM'),
(0, 15, '27-Jul-2012', 2147483647, '', 'Gujrat', 'ADMIN', 'Sitara', '7-Aug-2012', '12:40.PM'),
(0, 16, '27-Jul-2012', 2147483647, '', 'Maharashtra', 'ADMIN', 'Anuja\r', '7-Aug-2012', '12:49.PM'),
(0, 17, '27-Jul-2012', 2147483647, '', 'West Bengal', 'ADMIN', 'Anuja', '7-Aug-2012', '12:39.PM'),
(0, 18, '27-Jul-2012', 2147483647, '', 'Maharashtra', 'ADMIN', 'Anuja\r', '7-Aug-2012', '12:49.PM'),
(0, 19, '27-Jul-2012', 2147483647, '', 'Karnataka', 'ADMIN', 'Anuja\r', '7-Aug-2012', '12:49.PM'),
(0, 20, '27-Jul-2012', 2147483647, '', 'Maharashtra', 'ADMIN', 'Anuja\r', '7-Aug-2012', '12:49.PM');

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;



Here's the test php file I've been using (sqlTest.php):
PHP
<?php

	// step 1 - establish connection to MySql
	$server = 'localhost';
	$user = '** YOUR USERNAME HERE **';
	$password = '** YOUR PASSWORD HERE **';
	$dbLink = mysql_connect($server, $user, $password);  
	if (!$dbLink) 
		die('Can\'t establish a connection to the database: ' . mysql_error());  
  
	$dbSelected = mysql_select_db('test', $dbLink);  
		if (!$dbSelected) die ('We\'re connected, but can\'t use the table: ' . mysql_error());  


	// step 2 - output a <select> list containing all of the distinct times of day
	echo "<form action='sqlTest.php'>";
	echo "<select name='test[]' multiple='multiple'>";
		$sql = "SELECT DISTINCT time FROM import ORDER BY time asc";
		$res1=mysql_query($sql);
		while($row=  mysql_fetch_array($res1))
		{
			echo "<option value='". $row['time'] . "'>".$row['time']."</option>";
		}
		echo "</select>";
		echo"<input type='submit' value='submit'/>";
	echo"</form>";


	// step 3 - if the form has been submitted, extract the selected times (times come from table just above on line 13)
	if (isset($_GET['test']))
	{
		// step 3 - create sql statement to include the times that were selected.
		
		$i = 0;
		foreach ($_GET['test'] as $selectedOption)
			{
				$options[$i++] = $selectedOption;
			}

		$sql = "select DISTINCT date,mobno,city,state,type,telecaller,time FROM import WHERE time IN(";
		$num = count($options);
		for ($i=0; $i<$num-1; $i++)
		{
			$sql .= "'".$options[$i]."', ";
		}
		$sql .= "'".$options[$i]."')";
		$sql .= "OR date1='01/01/0000'";
		echo $sql . "<br>";		


		$result = mysql_query($sql);

		print "<div id='print'>";
		print "<center>List of Mobile Numbers for Date <font color='#FF00FF'>";

		// TODO:
		// need to add the selected dates here
		print "<u>01/01/000</u></font> or Time <font color='#FF00FF'>";
		
		// TODO:
		// need to add the selected times here
		print "<u>12:49.PM</u></font> <center> <br/>";
		
		print "<table border='1'  cellspacing='5' cellpadding='12'>";
		print "<tr  bgcolor='#82CAFF'>";
		print "<th>Sr.No</th>";
		print "<th>Date</th>";
		print "<th>Mobile No</th>";
		print "<th>City</th>";
		print "<th>State</th>";
		print "<th>Type</th>";
		print "<th>CIExe.</th>";
		print "<th>Time</th>";
		print "</tr>";
		
		$srno1=0;
		while($row=mysql_fetch_array($result))
		{
			$srno1=$srno1+1;
			print"<tr>";
				print"<td>" .$srno1. "</td>";
				print"<td>" . $row['date'] . "</td>";
				print"<td>" . $row['mobno'] . "</td>";
				print"<td>" . $row['city'] . "</td>";
				print"<td>" . $row['state'] . "</td>";
				print"<td>" . $row['type'] . "</td>";
				print"<td>" . $row['telecaller'] . "</td>";
				print"<td>" . $row['time'] . "</td>";
			print"</tr>";
		}
		print"</table>";
		print"</div>";
		echo"<br/>";
	}
?>



Here's the output when opened for the first time
HTML
<html>
<head>
</head>
<body>
    <form action="sqlTest.php">
    <select name="test[]" multiple="multiple">
        <option value="12:39.PM">12:39.PM</option>
        <option value="12:40.PM">12:40.PM</option>
        <option value="12:48.PM">12:48.PM</option>
        <option value="12:49.PM">12:49.PM</option>
        </select>
    <input type="submit" value="submit">
    </form>
    </body>
</html>


Here's the output when the first 3 options are selected:

select DISTINCT date,mobno,city,state,type,telecaller,time FROM import WHERE time IN('12:39.PM', '12:40.PM', '12:48.PM')OR date1='01/01/0000'
List of Mobile Numbers for Date 01/01/000 or Time 12:49.PM

Sr.NoDateMobile NoCityState Type
CIExe.
Time
1
27-Jul-2012
2147483647
Maharashtra
ADMIN
Sitara
12:48.PM
2
27-Jul-2012
2147483647
Gujrat
ADMIN
Sitara
12:40.PM
3
27-Jul-2012
2147483647
West Bengal
ADMIN
Anuja
12:39.PM
 
Share this answer
 
Comments
project virus 14-Aug-12 1:44am    
why , SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; ?
enhzflep 14-Aug-12 2:16am    
Dont know - probably because you've not set id to auto_increment. It's normal that at least one field is guaranteed to be unique - the most normal is the id field, this is done by making it auto increment.
Notice in the first column of your (html) table you have a column Sr.No - also in your (sql) table you have a column srno. This sql column should (a) be an auto_increment (b) be3 the source of the column in the html table. You shouldn't be maintaining a variable in the php loop.
project virus 14-Aug-12 2:12am    
there is a warning Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Rank_tele1\systemdate.php on line 128 and select DISTINCT date,mobno,city,state,type,telecaller,time FROM import WHERE time IN('')OR date1='11-AUG-2012'
project virus 14-Aug-12 2:17am    
Solved this problem.Done !! very very thanks :)
enhzflep 14-Aug-12 2:21am    
That's okay - please DONT remove helpful error messages, I see you changed your comment above - I've changed it back so that it helps anybody in the future.

Please do accept & rate my answer if it has helped you. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900