从同一个表

时间:2016-09-18 09:49:16

标签: php jquery mysql sql-server drop-down-menu

我有一个像这样的SQL表。

id     name     cname
1      Ash      abc       
2      Ash      abc
3      Ashu     abc
4      Ashu     xyz
5      Yash     xyzz
6      Ash      xyyy

我希望用户从第一个选择下拉列表中选择一个值,该列表显示DISTINCT名称值及其工作正常。

第一选:

<select id="select1" required="required" class="custom-select standard">
<option value="0" selected="selected">Choose Category</option>
<?php 
    $resultd = mysqli_query($mysqli,"SELECT DISTINCT name FROM advertise");
    if ($resultd)
        {
              while($tier = mysqli_fetch_array($resultd)) 
                {
                    echo '<option value="' .$tier['name'] . '">' . $tier['name'] . '</option>';
                }
        }
?>
</select>

现在我想基于第一个显示第二个选择下拉框的值。我正在使用的Jquery是:

<script>
    $(function(){
    var conditionalSelect = $("#select2"),
    // Save possible options
    options = conditionalSelect.children(".conditional").clone();

    $("#select1").change(function(){
    var value = $(this).val();                  
    conditionalSelect.children(".conditional").remove();
    options.clone().filter("."+value).appendTo(conditionalSelect);
}).trigger("change");
});
</script>

第二选择框

<select id="select2" required="required" class="custom-select standard">
<option value="0" selected="selected">Choose Location</option>
<option class="conditional name" value="">cname</option>
</select>

所有我想知道的是我应该使用什么php查询来获取第二个选择框中的值。我尝试了很多寻找解决方案,但我没有找到任何从数据库中获取其价值的解决方案......提前感谢...

1 个答案:

答案 0 :(得分:0)

  

如果你做的事情略有不同,你可能不需要第二次查询和所有这些复杂性。这意味着,您可以使用一个查询来实现目标。下面的代码演示了如何。 注意:此解决方案使用JQuery使事情变得更简单。

要对此进行测试; 只需将代码 原样 复制并粘贴到新文件中并查看它是否按预期工作。

CHEERS&amp;好运!!!

<?php

    // USE YOUR CONNECTION DATA... PDO WOULD BE HIGHLY RECOMMENDED.
    // INTENTIONALLY USING mysqli (NOT RECOMMENDED) TO MATCH YOUR ORIGINAL POST.
    $conn       = mysqli_connect("localhost", "root", 'root', "test");
    if (mysqli_connect_errno()){
        die("Failed to connect to MySQL: " . mysqli_connect_error());
    }

    $resourceID = mysqli_query($conn, "SELECT * FROM advertise");
    $all        = mysqli_fetch_all($resourceID, MYSQLI_ASSOC);
    $uniques    = [];
    $options1   = "";

    if( !empty($all) ){
        foreach($all as $intKey=>$advertiseData) {
            $key    = $advertiseData['name'];
            $cName  = getCNameForName($all, $key);
            if (!array_key_exists($key, $uniques)) {
                $uniques[$key] = $advertiseData;
                $options1 .= "<option value='{$key}' data-cname='{$cName}'>";
                $options1 .= $key . "</option>";
            }
        }
    }

    function getCNameForName($all, $name){
        $result = [];
        foreach($all as $iKey=>$data){
            if($data["name"] == $name){
                $result[] = $data['cname'];
            }
        }
        return $result ? implode(", ", array_unique($result)) : "";
    }
?>

<html>
<body>
<div>
    <select id="select1" required="required" class="custom-select standard">
        <option value="0" selected="selected">Choose Category</option>
        <?php echo $options1; ?>
    </select>
    <select id="select2" required="required" class="custom-select standard">
        <option value="0" selected="selected">Choose Location</option>
    </select>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript">

    (function($) {
        $(document).ready(function(){
            var firstSelect     = $("#select1");
            var secondSelect    = $("#select2");

            firstSelect.on("change", function(){
                var main        = $(this);
                var mainName    = main.val();
                var mainCName   = main.children('option:selected').attr("data-cname");
                var arrCName    = mainCName.split(", ");
                var options2    = "<option value='0' selected >Choose Location</option>";

                for(var i in arrCName){
                    options2   += "<option value='" + arrCName[i]  + "' ";
                    options2   += "data-name='" + mainName + "'>" + arrCName[i] + "</option>\n";
                }

                secondSelect.html(options2);
            });

        });
    })(jQuery);
</script>

</body>
</html>