dongxie8906 2012-10-14 06:47
浏览 119
已采纳

php动态下拉框内容

I've got two data tables like below.

Table 1:

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

Table 2:

chkNo
------
 201
 203

I have got a dropdown box created dynamically which contains table 1 information which is all the responses from 200 to 205. In other words that dropdown has 200,201,202...205. What I now need is to exclude the numbers in table 2 once the dropdown box is created. For instance then the dropdown should have only 200,2004 and 2005 when it is displayed.

Here is the code I've done to get all the responses between start and end number as per table 1. Can someone please tell me how do I exclude the table 2 numbers once the dropdown is created. Thanks.

    $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>";
        }
    }
  • 写回答

3条回答 默认 最新

  • dongren9739 2012-10-14 07:09
    关注

    I would suggest leveraging array_diff() to limit the initial creation of the dropdown to only those values which don't exist in the second table, like this:

    //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>";
    }
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻看一个题
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)