php动态下拉框内容
我有两个数据表,如下所示 表1:php动态下拉框内容,php,Php,我有两个数据表,如下所示 表1: -------------------- sbstart sbend totsb -------------------- 200 205 6 表2: chkNo ------ 201 203 我有一个动态创建的下拉框,其中包含表1的信息,即从200到205的所有响应。换句话说,下拉列表有200201202…205。我现在需要的是在创建下拉框后排除表2中的数字。例如,当下拉列表显示时,它应该只有2002004和2005 下面是我所做的代码,
--------------------
sbstart sbend totsb
--------------------
200 205 6
表2:
chkNo
------
201
203
我有一个动态创建的下拉框,其中包含表1的信息,即从200到205的所有响应。换句话说,下拉列表有200201202…205。我现在需要的是在创建下拉框后排除表2中的数字。例如,当下拉列表显示时,它应该只有2002004和2005
下面是我所做的代码,用于按照表1获得起始编号和结束编号之间的所有响应。有人能告诉我,一旦创建下拉列表,如何排除表2的数字吗。谢谢
$con=mysql_connect('localhost','root') or die ("Server connection failure!");
$db=mysql_select_db('regional_data',$con) or die ("Couldn't connect the database");
$SQLx="SELECT * FROM table1";
$runx=mysql_query($SQLx,$con) or die ("SQL Error");
$norx=mysql_num_rows($runx);
while ($rec = mysql_fetch_array($runx))
{
for($i=$rec['sbstart']; $i<=$rec['sbend']; $i++)
{
echo "<option id='options' value='$i'>$i<br></option>";
}
}
$con=mysql\u connect('localhost','root')或die(“服务器连接失败!”);
$db=mysql\u select\u db('regional\u data',$con)或die(“无法连接数据库”);
$SQLx=“从表1中选择*”;
$runx=mysql_query($SQLx,$con)或die(“SQL错误”);
$norx=mysql\u num\u行($runx);
而($rec=mysql\u fetch\u数组($runx))
{
对于($i=$rec['sbstart'];$i请尝试此操作。这将在生成下拉列表之前从表2中生成排除列表:
$con=mysql_connect('localhost','root') or die ("Server connection failure!");
$db=mysql_select_db('regional_data',$con) or die ("Couldn't connect the database");
$exclude = array();
$query = 'SELECT * FROM table2';
$runx=mysql_query($query,$con) or die ("SQL Error");
$norx=mysql_num_rows($runx);
while ($rec = mysql_fetch_array($runx))
{
$exclude[] = $rec['chkNo'];
}
$SQLx="SELECT * FROM table1"
$runx=mysql_query($SQLx,$con) or die ("SQL Error");
$norx=mysql_num_rows($runx);
while ($rec = mysql_fetch_array($runx))
{
for($i=$rec['sbstart']; $i<=$rec['sbend']; $i++)
{
if (!in_array($i, $exclude))
{
echo "<option id='options' value='$i'>$i<br></option>";
}
}
}
$con=mysql\u connect('localhost','root')或die(“服务器连接失败!”);
$db=mysql\u select\u db('regional\u data',$con)或die(“无法连接数据库”);
$exclude=array();
$query='SELECT*FROM table2';
$runx=mysql\u query($query,$con)或die(“SQL错误”);
$norx=mysql\u num\u行($runx);
而($rec=mysql\u fetch\u数组($runx))
{
$exclude[]=$rec['chkNo'];
}
$SQLx=“从表1中选择*
$runx=mysql_query($SQLx,$con)或die(“SQL错误”);
$norx=mysql\u num\u行($runx);
而($rec=mysql\u fetch\u数组($runx))
{
对于($i=$rec['sbstart'];$i,我建议利用array_diff()将下拉列表的初始创建限制为第二个表中不存在的值,如下所示:
//set up the db connection
$con = mysql_connect('localhost','root') or die ("Server connection failure!");
$db = mysql_select_db('regional_data', $con) or die ("Couldn't connect the database");
//get the range of values from the first table
$SQLx = "SELECT * FROM table1";
$runx = mysql_query($SQLx, $con) or die ("SQL Error");
$rec = mysql_fetch_array($runx);
//create an array representing the range of values
$table1_array = array();
for($i = $rec['sbstart']; $i <= $rec['sbend']; $i++)
{
$table1_array[] = $i;
}
//get the values to be omitted from the second table
$SQLz = "SELECT * FROM table2";
$runz = mysql_query($SQLz, $con) or die ("SQL Error");
//create an array representing the values to be omitted
$table2_array = array();
while ($rec2 = mysql_fetch_array($runz))
{
$table2_array[] = $rec2['chkNo'];
}
//compare the arrays
//this results in an array of only the values you wish to include
$final_array = array_diff($table1_array, $table2_array);
//create the dropdown from the resulting array
foreach ($final_array as $value)
{
echo "<option id='options' value='$value'>$value<br></option>";
}
//设置数据库连接
$con=mysql_connect('localhost','root')或die(“服务器连接失败!”);
$db=mysql\u select\u db('regional\u data',$con)或die(“无法连接数据库”);
//从第一个表中获取值的范围
$SQLx=“从表1中选择*”;
$runx=mysql_query($SQLx,$con)或die(“SQL错误”);
$rec=mysql\u fetch\u数组($runx);
//创建表示值范围的数组
$table1_array=array();
对于($i=$rec['sbstart'];$i$con=mysql\u connect('localhost','root')或die(“服务器连接失败!”);
$db=mysql\u select\u db('regional\u data',$con)或die(“无法连接数据库”);
$SQLx=“从表1中选择*”;
$SQLy=“从表2中选择*”;
$runx=mysql_query($SQLx,$con)或die(“SQL错误”);
$runy=mysql\u query($SQLy,$con)或die(“SQL错误”);
$norx=mysql\u num\u行($runx);
而($rec=mysql\u fetch\u数组($runx))
{
对于($i=$rec['sbstart'];$imysql.*
函数不推荐使用pdo或mysqlipasse错误:语法错误,第12行的E:\xampp\htdocs\ss\docs\testone.php中意外的“$table1_数组”(T_变量)中有一个分号。:)Fixed.分号缺少seth。否则此方法也可以使用。谢谢!
$con=mysql_connect('localhost','root') or die ("Server connection failure!");
$db=mysql_select_db('regional_data',$con) or die ("Couldn't connect the database");
$SQLx="SELECT * FROM table1";
$SQLy="SELECT * FROM table2";
$runx=mysql_query($SQLx,$con) or die ("SQL Error");
$runy=mysql_query($SQLy,$con) or die ("SQL Error");
$norx=mysql_num_rows($runx);
while ($rec = mysql_fetch_array($runx))
{
for($i=$rec['sbstart']; $i<=$rec['sbend']; $i++)
{
$exist=0;
while($rec2=mysql_fetch_array($runy)){
if($i==$rec2['chkNo']){
$exist=1;
}
}
if ($exist==0)
echo "<option id='options' value='$i'>$i<br></option>";
}
}