使用多个Dropdown创建动态MySQL语句并生成

时间:2017-07-27 00:49:12

标签: php mysql

我正在使用多个下拉列表中的第一个页面: -

function createCFAList()
{
$ajax = false;
if(isset($_GET['action']) && $_GET['action'] == 'ajax' && isset($_GET['cl'])) 
{
    $SelectedCFAName =  $_GET['cl'];
    $ajax = true;   
}

$sql = "SELECT DISTINCT `cfalist`.`CFA`
        FROM `amwplist` 
        INNER JOIN `cfalist` ON `cfalist`.`CFA_ID` = `amwplist`.`CFA_ID`";
$result = mysql_query($sql);            
$CFAlist = "<select id='cfa5' name='cfa5'>";            
while ($row = mysql_fetch_array($result)) {         
    $CFAlist = $CFAlist . "<option value='" . $row['CFA'] ."'>" . $row['CFA'] ."</option>";         
}           
$CFAlist = $CFAlist . "</select>";  

return ($CFAlist);
}

第二个清单是: -

function createStatusList()
    {
    $ajax = false;
    if(isset($_GET['action']) && $_GET['action'] == 'ajax' && isset($_GET['ss'])) 
    {
        $SelectedStatus =  $_GET['ss'];
        $ajax = true;   
    }

    $sql = "SELECT `Status_ID`, 
            `Status_Type`
            FROM `workstatuslist` ";
    $result = mysql_query($sql);            
    $Statuslist = "<select id='status4' name='status4'>";           
    while ($row = mysql_fetch_array($result)) {     
        if($row['Status_Type'] == "BOO in Progress")            
        {           

        $Statuslist = $Statuslist . "<option selected='" . $row['Status_Type'] ."'>" . $row['Status_Type'] ."</option>";    
        }           
    else            
        {           
        $Statuslist = $Statuslist . "<option value='" . $row['Status_Type'] ."'>" . $row['Status_Type'] ."</option>";   
        }           
    }           
    $Statuslist = $Statuslist . "</select>";



    return ($Statuslist);
    }

这两个函数放在function.php中,并从AMWP.php调用以显示Dropdown。选定的选项存储在变量中   global $ SelectedStatus和全局$ SelectedCFAName 在AMWP.php中使用以下脚本: -

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> -->
    <script>

        $('#status4').change(function()
        {
            var status4 = $('#status4').val();
            var req = $.get('functions.php', {ss: status4, action: 'ajax'});

        });

        $('#cfa5').change(function()
        {
            var cfa5 = $('#cfa5').val();
            var req = $.get('functions.php', {cl: cfa5, action: 'ajax'});

        });
    </script>



How may I store the Selected Option onChange to the variables declared global $SelectedStatus and  global $SelectedCFAName 
and then use them to create SQL Statement like whenever the Selected option is changed by the user



$sql="SELECT * FROM abc WHERE status LIKE $SelectedStatus AND cfa LIKE $SelectedCFAName";

2 个答案:

答案 0 :(得分:0)

我在此网址找到了解决方案 SourceCodester Dot Com

答案 1 :(得分:0)

在此链接中找到了另一个好的示例来源

PHP Jquery Dropdown sample. Good One