BlackTyger Posted August 15, 2010 Share Posted August 15, 2010 Here is my HTML code: <html> <head> <title>Simple Search Form</title> </head> <body> <form name="searchform" method="get" action="/search.php"> Select Gender: <select name="gender"> <option value="Male">Male</option> <option value="Female">Female</option> </select> Select City: <select name="gender"> <option value="all">All Cities</option> <option value="newyork">New York</option> <option value="toronto">Toronto</option> <option value="london">London</option> <option value="paris">Paris</option> </select> <form> </body> </html> Here is my PHP code: <?php // get the data from the search form # get the gender (male or female) $gender = $_GET['gender']; # get the city $city = $_GET['city']; // connect to mysql and select db mysql_connect('localhost', 'root', 'pass') or die(mysql_error()); mysql_select_db($test_db); // send query $query = mysql_query("SELECT * FROM `visitors_location` WHERE gender='$gender' AND city='$city'"); $count = mysql_num_rows($query); // display data while ( $show = mysql_fetch_assoc($query) ) { echo $gender . " " . $city; } ?> My script basically shows # of males or females in a specific city. How can I show all males in all cities? In other words, let's say I want to show # of Females from all those 4 cities combined. I don't know how to do that. Can someone please help me? Link to comment https://forums.phpfreaks.com/topic/210810-getting-and-filter-results-from-mysql-help-needed/ Share on other sites More sharing options...
kickstart Posted August 15, 2010 Share Posted August 15, 2010 Hi I would code it something like this $query = mysql_query("SELECT * FROM `visitors_location` WHERE gender='$gender'".(($city == 'all') ? '' : " AND city='$city'")); All the best Keith Link to comment https://forums.phpfreaks.com/topic/210810-getting-and-filter-results-from-mysql-help-needed/#findComment-1099677 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.