两个下拉菜单的que SQL查询

时间:2017-04-13 07:57:16

标签: javascript php jquery html mysql

我会尽力解释。我正在尝试创建两个下拉菜单,在SQL查询中使用AND函数计算和显示不同的产品。请参阅下面的代码。此时它正在工作,但我希望第二个选项可以使用从第二个下拉菜单name="categorie"中选择的选项动态排队。此时,我在我的查询中硬编码了第二个选项,以测试它是否正常工作:

$query = "SELECT COUNT(".$q.") c FROM MirrorWebProductsExpanded WHERE Subcategorie = 'Citygames'";

我想要的是WHERE Subcategorie =类似于WHERE Subcategorie =".$y.",其中包含从下拉菜单name="categorie"中选择的选项。我认为问题在于$_SESSION['categorie'];这个会话没有保存并通过AJAX脚本运行。关于如何使其发挥作用的任何想法?

HTML

<div class="row">
    <form class="header-search-form">
        <select name="thema" onchange="sortboottochten(this.value)" class="selectpicker">
            <option value="t1" data-icon="glyphicon-option-vertical" class="special">&nbsp;&nbsp;Kies een thema</option>
                <optgroup label="Kies een thema">
                    <option value="Actief_Avontuur" data-icon="glyphicon-sort-by-alphabet" class="special">&nbsp;&nbsp;Actief, sportief en avontuurlijk</option>
                    <option value="Creatief" data-icon="glyphicon-sort-by-alphabet-alt" class="special">&nbsp;&nbsp;Creatief</option>
                    <option value="Culinair" data-icon="glyphicon-sort-by-attributes" class="special">&nbsp;&nbsp;Culinair</option>
                    <option value="Fun_Entertainment" data-icon="glyphicon-sort-by-attributes-alt" class="special">&nbsp;&nbsp;Fun en entertainment</option>
                    <option value="Kunst_Cultuur" data-icon="glyphicon-sort-by-attributes" class="special">&nbsp;&nbsp;Kunst en cultuur</option>
                    <option value="Muziek_Zang_Dans" data-icon="glyphicon-sort-by-attributes" class="special">&nbsp;&nbsp;Muziek, zang en dans</option>
                    <option value="Teambuilding" data-icon="glyphicon-sort-by-attributes" class="special">&nbsp;&nbsp;Teambuilding</option>
                    <option value="Vrijgezellen" data-icon="glyphicon-sort-by-attributes" class="special">&nbsp;&nbsp;Vrijgezellen</option>
                </optgroup>
        </select>
    </form>
</div>
</div>
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-4">
    <form class="header-search-form">
        <select name="categorie" onchange="sortboottochten(this.value)" class="selectpicker">
            <option value="CAST(Ranking AS int) ASC" data-icon="glyphicon-option-vertical" class="special">&nbsp;&nbsp;Kies een categorie</option>
                <optgroup label="Sorteren op...">
                    <option value="Citygames" data-icon="glyphicon-sort-by-alphabet" class="special">&nbsp;&nbsp;Naam (A-Z)</option>
                    <option value="" data-icon="glyphicon-sort-by-alphabet-alt" class="special">&nbsp;&nbsp;Naam (Z-A)</option>
                    <option value="" data-icon="glyphicon-sort-by-attributes" class="special">&nbsp;&nbsp;Prijs (laag - hoog)</option>
                    <option value="" data-icon="glyphicon-sort-by-attributes-alt" class="special">&nbsp;&nbsp;Prijs (hoog - laag)</option>
                </optgroup>
        </select>
    </form>
</div>
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-4">
    <div class="row">
        <div id="txtHint" class="btn review-button">Bekijk alle <?php echo $row['c'];?> uitjes &nbsp;&nbsp;<i class="fa fa-chevron-right" aria-hidden="true" style="font-size:1rem;color:#fff;"></i></div>
        <div class="load-screen-2">
            <i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i><span>Bezig met sorteren...</span>
        </div>
    </div>
</div>

SCRIPT

function sortboottochten(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                $('#txtHint').hide().fadeIn(2100);
                document.getElementById("txtHint").innerHTML = this.responseText;
                setTimeout(function() {$('.load-screen-2').hide().fadeOut(2000);}, 2000);
                console.log('Loading Done');
            }
        };

        $('.load-screen-2').show().fadeIn(2000);
        xmlhttp.open("GET","/header-search.php?q="+str,true);
        xmlhttp.send();
        console.log('Loading Start');

    }
}

标题-search.php中

<?php
    session_start();
    $servername = "localhost";
    $username = "";
    $password = "";
    $dbname = "";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);

    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
$q = $_GET['q'];
$_SESSION['thema'] = $q;
$_SESSION['categorie'] = $y;

$query = "SELECT COUNT(".$q.") c FROM MirrorWebProductsExpanded WHERE Subcategorie = 'Citygames'";
$result = mysqli_query($conn,$query);
$row = mysqli_fetch_assoc($result);
echo "<a href='http://mysite.nl/zoeken/'>Bekijk alle " . $row['c'] .  " uitjes</a>";
?>

0 个答案:

没有答案