简体   繁体   English

通过选择下拉列表过滤mysql结果

[英]Filtering mysql results via select dropdown

I'm a new member of StackOverflow, and although I've been using the website for a long time, it's my first time posting a question, in a hope that someone will be able to help me. 我是StackOverflow的新成员,虽然我已经使用该网站很长一段时间了,但这是我第一次发帖提问,希望有人能够帮助我。 I'll start by saying that my knowledge of PHP and MySQL is basic, but what I'm trying to do isn't too complex in my opinion, so hopefully I won't be asking for much. 我首先要说的是我对PHP和MySQL的了解是基本的,但我认为我想要做的并不是太复杂,所以希望我不会要求太多。 I've done a lot of prior research, but I just couldn't find the right answer. 我做了很多先前的研究,但我找不到正确的答案。

In short, this is what I'm trying to do: 简而言之,这就是我要做的事情:

I've got an html form, which upon submission writes data to a database, and then publishes a table on a separate html page. 我有一个html表单,在提交时将数据写入数据库,然后在单独的html页面上发布表。 With each successful submission a new table gets generated and published, while the old one gets pushed underneath. 每次成功提交都会生成并发布一个新表,而旧表会被推到下面。 This all works fine, and I've also implemented pagination so that only 5 tables are visible per page. 这一切都很好,我也实现了分页,每页只能看到5个表。

What I'd like to be able to do is allow people to ONLY view/display results (tables) based on a specific criteria, in this case "rating", by selecting a rating from a drop-down on the page where tables are published. 我希望能够做的是允许人们仅根据特定标准查看/显示结果(表格),在本例中为“评级”,方法是从表格下拉页面的下拉列表中选择评级。出版。 Rating is one of the fields in my form which gets submitted to a database and then published in one of the rows in a table. 评级是我的表单中的一个字段,它被提交到数据库,然后在表中的一行中发布。

Below is the code which publishes tables. 下面是发布表的代码。 Thanks in advance for your help! 在此先感谢您的帮助!

<?php
include('dbconnect.php'); 

mysql_select_db("vtracker", $con);
$result  = mysql_query("SELECT * FROM userdata");
$age = "Age:";
$rating = "Rating:";
$country = "From:";
$name = "Name:";

        while($row = mysql_fetch_array($result))
        {

                        echo "<table id='mft_table' cellspacing='0'>";
                        echo "<tbody>";
                        echo "<tr>";
                        echo "<td class='row1'>" .$name . " " . $row['personsname'] . "</td>";
                        echo "<td rowspan='4'>";
                        echo "<div class='mft_column'>" . $row['mft'] . "</div>";
                        echo "</td>";
                        echo "</tr>";
                        echo "<tr>";
                        echo "<td class='row2'>" . $country . " " . $row['nationality'] . "</td>";
                        echo "</tr>";
                        echo "<tr>";
                        echo "<td class='row3'>" . $age . " " . $row['personsage'] . "</td>";
                        echo "</tr>";
                        echo "<tr>";
                        echo "<td class='row4'>" . $rating . " " . $row['rating'] . "</td>";
                        echo "</tr>";
                        echo "</tbody>";
                        echo "<br>";
                        echo "</table>"; 

        }
    ?>

for both true and false use can add thid in your code: 对于true和false使用,可以在代码中添加:

if($_POST['rating_dropdown']!='')
{
    $temp_rating = $_POST['rating_dropdown'];
    $query=mysql_query("SELECT * FROM userdata WHERE rating = '$temp_rating'");
}
else
{
    $query=mysql_query("SELECT * FROM userdata");
}

Dunno if this works, it's just a hinch. Dunno如果这样有效,那只是一个必要条件。 haha. 哈哈。 It will see if the rating is true(not null), if it's true it will echo the results. 它将查看评级是否为真(非空),如果它是真的,它将回显结果。

while($row = mysql_fetch_array($result))
{
if ($rating)
                echo "<table id='mft_table' cellspacing='0'>";
                echo "<tbody>";
                echo "<tr>";
                echo "<td class='row1'>" .$name . " " . $row['personsname'] . "</td>";
                echo "<td rowspan='4'>";
                echo "<div class='mft_column'>" . $row['mft'] . "</div>";
                echo "</td>";
                echo "</tr>";
                echo "<tr>";
                echo "<td class='row2'>" . $country . " " . $row['nationality'] . "</td>";
                echo "</tr>";
                echo "<tr>";
                echo "<td class='row3'>" . $age . " " . $row['personsage'] . "</td>";
                echo "</tr>";
                echo "<tr>";
                echo "<td class='row4'>" . $rating . " " . $row['rating'] . "</td>";
                echo "</tr>";
                echo "</tbody>";
                echo "<br>";
                echo "</table>"; 
}

}

Once the dropdown gets selected and posted to your display page, use this code: 选择下拉列表并将其发布到显示页面后,请使用以下代码:

$temp_rating = $_POST['rating_dropdown'];
mysql_query("SELECT * FROM userdata WHERE rating = '$temp_rating'");

Keep in mind, however, that you should be using PDO or mysqli extension, not the mysql extension. 但请记住,您应该使用PDO或mysqli扩展,而不是mysql扩展。 According to PHP's website: 根据PHP的网站:

This extension is deprecated as of PHP 5.5.0, and will be removed in the future. 自PHP 5.5.0起,此扩展已弃用,将来将被删除。 Instead, the MySQLi or PDO_MySQL extension should be used. 相反,应该使用MySQLi或PDO_MySQL扩展。 See also MySQL: choosing an API guide and related FAQ for more information. 另请参阅MySQL:选择API指南和相关的常见问题解答以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM