Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering mysql results via select dropdown

Tags:

php

mysql

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. 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. 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. 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.

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>"; 

        }
    ?>
like image 807
Zeus Avatar asked Mar 10 '13 13:03

Zeus


1 Answers

for both true and false use can add thid in your code:

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");
}
like image 178
There is a dream I am living T Avatar answered Sep 27 '22 19:09

There is a dream I am living T