Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PHP MySQL
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']; ?>">
<table>
    <tr>
        <td>name:</td>
        <td><input type="text" name="name" /></td>
    </tr>
    <tr>
        <td>mail:</td>
        <td><input type="text" name="mail" /></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td><input type="submit" name="submit" value="Search" /></td>
    </tr>
</table>
</form>
 

 

 

<?php
$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 />";
    }
}
?>
Posted 13-Feb-13 11:54am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 ....from ";
//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;
</add_city_field_value></add_name_field_value>
 
Your dynamic sql query ready
 
enjoy
  Permalink  

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

  Print Answers RSS
0 CPallini 375
1 Sergey Alexandrovich Kryukov 370
2 BillWoodruff 314
3 George Jonsson 274
4 OriginalGriff 257
0 OriginalGriff 5,030
1 CPallini 4,115
2 Sergey Alexandrovich Kryukov 3,554
3 George Jonsson 2,826
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100