0

以下のような2つのデータテーブルがあります。

表1:

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

表 2:

chkNo
------
 201
 203

200 から 205 までのすべての応答であるテーブル 1 の情報を含むドロップダウン ボックスを動的に作成しました。つまり、ドロップダウンには 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>";
        }
    }
4

3 に答える 3

0

これを試して。これは、ドロップダウンを作成する前に、table2 から除外リストを作成します。

$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>";
            }
        }
    }
于 2012-10-14T06:55:23.017 に答える
0

次のように、array_diff() を活用して、ドロップダウンの最初の作成を 2 番目のテーブルに存在しない値のみに制限することをお勧めします。

//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

于 2012-10-14T07:09:57.780 に答える
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>";
        }
    }
于 2012-10-14T07:10:43.353 に答える