2012-10-14 72 views
0

我有兩個數據表如下。php動態下拉框內容

表1:

-------------------- 
sbstart sbend totsb 
-------------------- 
    200 205 6 

表2:

chkNo 
------ 
201 
203 

我有動態創建一個下拉框包含表1信息,其爲200的所有響應到205換句話說該下拉菜單有200,201,202 ... 205。我現在需要的是在創建下拉框後排除表2中的數字。例如,下拉菜單顯示時應該只有200,2004和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>"; 
     } 
    } 
+0

'mysql_ *'函數已不再使用PDO或mysqli的 –

回答

0

我會建議利用和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>"; 
} 

參見: http://php.net/manual/en/function.array-diff.php

+0

解析錯誤:語法錯誤,第12行E:\ xampp \ htdocs \ ss \ docs \ testone.php中意外的'$ table1_array'(T_VARIABLE) –

+0

錯過了分號。 :)修正。 – coderabbi

0

試試這個。這將構建從表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>"; 
      } 
     } 
    } 
+0

分號是缺少一套H。否則這種方法也起作用。謝謝! –

0
$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>"; 
     } 
    }