Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 显示下拉选择中的数据库内容_Php_Jquery_Mysql_Ajax - Fatal编程技术网

Php 显示下拉选择中的数据库内容

Php 显示下拉选择中的数据库内容,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,在我的网站上,我有一个页面显示推荐信。我编写此代码是为了显示数据库中的所有推荐信息 这是我目前的代码: while ( $row = mysqli_fetch_array( $r, MYSQLI_ASSOC)) { $testimonial = $row['testimonial']; //echo $testimonial; $mytestimonial = nl2br($testimonial); $city = $row['cit

在我的网站上,我有一个页面显示推荐信。我编写此代码是为了显示数据库中的所有推荐信息

这是我目前的代码:

while ( $row = mysqli_fetch_array( $r, MYSQLI_ASSOC)) {
    $testimonial = $row['testimonial'];
    //echo $testimonial;
    $mytestimonial = nl2br($testimonial);
    $city               = $row['city_name'];
    $name               = $row['name'];
    $url                = $row['web_address'];
    $imageName      = $row['image_name'];
    $type               = $row['membership_type'];
} 
有了这段代码,我可以把我所有的推荐信都放到页面上。这对我来说很有效。我的问题是现在我需要根据类型筛选我的推荐信。我的数据库里有3种不同的证明。导师、学院、学生

我将使用一个选择框来过滤数据。当从选择框中选择一个选项时,我需要根据所选类型显示推荐信

<div class="filter-box">    
    <div id="select_box">
        <form method="post" action="">          
            <div class="variation2">
                <label>Filter By</label>
                <select class="select">
                    <option>Tutor</option>
                    <option>Institute</option>
                    <option>Student</option>
                </select>
            </div>
        </form> 
    </div>  
</div>
有人能帮我找到方向吗


谢谢

一旦用户选择了3个选项中的一个并显示了您所需的推荐,为什么不使用AJAX动态重新加载选择框呢?我想这会解决您的问题。

所以您想根据推荐的类型来选择推荐

$q = "SELECT testimonial, city_name, name, web_address, image_name, membership_type
        FROM testimonials 
        INNER JOIN city ON city.city_id = testimonials.city_id
        WHERE type = '$type'
        ORDER BY date_added DESC LIMIT $start, $display";
现在您还需要从用户那里获取类型

<select name="type" class="select">
更改时自动提交表单示例

Javascript:

<script type="text/javascript">
    function submitform(){
        document.frmType.submit();
    }
</script>
表格:


进一步的例子可以找到

您有两个选择。第一个是与。 第二种是在更改或提交时提交表单:

<select class="select" name="type" onchange="document.forms[0].submit();">
    <option value="Tutor">Tutor</option>
    <option value="Institute">Institute</option>
    <option value="Student">Student</option>
</select>

当然,一定要检查表单是否已提交。

在每个推荐div中按名称添加一个类

 <div class="student">testimonial goes here</div>

我希望它能帮助您在查询中插入where子句。我还建议使用Ajax.1。为选择框2指定一个名称。接受此值作为类型3。使用此类型变量在查询中进行筛选-从city.city\u id=Testicials.city\u id中的Certicials内部加入city中选择Certicials、city\u name、name、web\u address、image\u name、membership\u type,其中type=$type ORDER BY date\u added DESC LIMIT$start,$display;在这种情况下,我不能在表单中使用提交按钮。没有按钮是可能的吗?为什么不能使用提交按钮。是的,这是可能的,通过javascript或Ajax,我的页面无法显示提交按钮。这就是为什么我在寻找一个不同的解决方案。谢谢回复。我可以知道我可以使用jqery吗?我对JQuery了解不多,但是你可以检查一下我将使用你的代码。现在我有了这个错误——脚本“C:\wamp\www\website\estiminationals.php”中的第92行出现了一个错误:未定义索引:类型我按照您所说的做了。现在没有任何错误,但没有发生过滤。echo$q是什么;当您选择一个类型时给您?where子句不显示在我的查询中。问题是相同的。我用你的代码更新了。但我将值改为-`tutorinstitute Student`我不能使用show/hide函数。因为我需要重新查询我的查询。我的想法是在不浪费带宽的情况下在浏览器上执行过滤部分:
<select class="select" name="type" onchange="document.forms[0].submit();">
    <option value="Tutor">Tutor</option>
    <option value="Institute">Institute</option>
    <option value="Student">Student</option>
</select>
$type = '';
if(!empty($_POST) && isset($_POST[type])){
    $type = " WHERE testimonials.type = '".$_POST[type]."'";
}
$q = "SELECT testimonial, city_name, name, web_address, image_name, membership_type
        FROM testimonials 
        INNER JOIN city ON city.city_id = testimonials.city_id
        ".$type."
        ORDER BY date_added DESC LIMIT $start, $display";
 <div class="student">testimonial goes here</div>
 $('.student').show();