Click here to Skip to main content
15,070,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating a website company ladies.

Basically what I want is that users can filter results by name, age, height, weight, city, etc..

example if they choose filter by name and write the name of hillary, display all the women who call hillary, but the name and if want to filter by name and write new york city and then display all the hillary living in new york, and if filtered by name and city more height 1.65 inches, then display all the hillary living in new york and measure 1.65 inches tall.

And if you want to filter only by age 25, appearing only all who are 25 years of age.

I leave some codes that I have, I can not move over there

<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
        <td><input type="text" name="name" /></td>
        <td><input type="text" name="mail" /></td>
        <td><input type="submit" name="submit" value="Search" /></td>

$con = mysql_connect("localhost","user","password");
if (!$con)
  die('Could not connect: ' . mysql_error());

mysql_select_db("database", $con);

require_once "common.php";*/

if(isset($_POST['submit'])) {

    $Nombre = $_POST['name'];
    $Email = $_POST['mail'];

    if($Nombre != '' && $Email != '') {
         $conditions = "name LIKE '%".mysql_real_escape_string($name)."%' AND mail LIKE '%".mysql_real_escape_string($mail)."%'";

    $query = "SELECT * FROM usuarios ";
    if(count($conditions) > 0) {
        $query .= "WHERE " . $conditions;

    $result = mysql_query($query);

    while($row = mysql_fetch_array($result)) {
        echo $row['name'] . "<br />";

1 solution

I dont have any idea about php. But here is the logic.In case of filter search you have to create dynamic query. Dynamic query is not just complex , its a simple query which is creating depending the user choice.
sql= "Select .<add the="" column="" name="" to="" display="">...from <tablename>";
//not complete missing where condition

Now check
define variable condition;
 if(name given){
    condition +=" name = '<add_name_field_value>'";
if(condition not emtp){
 condition +=" and "; //repeat this as required
if(city given){
   condition += " city = '<add_city_field_value>'";
 and so on
finally add the where clouse

if(condition not emtp){
condition =" where "+condition;

Now sql = sql+condition;

Your dynamic sql query ready


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900