So I'm kinda new to php. I've made a search but i'm looking for some help so that i can add more inputs and it will narrow the results down. for example i search "John" and 100 johns come up. But then i search "john" and then select Canada only 15 Johns come up. I have some code written for the single line search.

<?php
      mysql_connect ("localhost", "root","root")  or die (mysql_error());
	  mysql_select_db ("server_db");
	  $term = $_POST['term'];
	  if(isset($_POST['submit'])){
	  if($term == ""){
	  echo "Please Enter a search term";}
	  $sql = mysql_query("select * from new_file where name like '%$term%'");
	  $rows = mysql_num_rows($sql);
while ($row = mysql_fetch_array($sql)){
    echo 'ID: '.$row['ID'];
    echo '<br/> First Name: '.$row['first'];
    echo '<br/> Last Name: '.$row['last'];
    echo '<br/> Country: '.$row['country'];
    echo '<br/><br/>';
    }
	if(!$rows)
	echo "No results where found for $term";
	  }
?>
<form action="search.php" method="post">
     Search: <input type="text" name="term" value="Search" /><br />
    <input type="submit" name="submit" value="Submit" />
    </form>

Help is greatly appreciated

Recommended Answers

All 7 Replies

$sql = mysql_query("select * from new_file where name like '%$term%'");

Just try this code...

$sql = mysql_query("select * from new_file where name like '%$term%' and country like '%$term%' ");

Try this

<?php
    mysql_connect ("localhost", "root","root") or die (mysql_error());
    mysql_select_db ("server_db");
    $term = $_POST['term'];
    $country=$_POST['country'];
    if(isset($_POST['submit'])){
    if($term == ""){
    echo "Please Enter a search term";}
    $sql = mysql_query("select * from new_file where name like '%$term%' and country like '%$country%'");
    $rows = mysql_num_rows($sql);
    while ($row = mysql_fetch_array($sql)){
    echo 'ID: '.$row['ID'];
    echo '<br/> First Name: '.$row['first'];
    echo '<br/> Last Name: '.$row['last'];
    echo '<br/> Country: '.$row['country'];
    echo '<br/><br/>';
    }
    if(!$rows)
    echo "No results where found for $term";
    }
    ?>
<form action="search.php" method="post">
    Search: <br>Name <input type="text" name="term" value="Search" /><br />
    country <select name="country">
               <option value="India">India</option>
               <option value="Australia">Australia</option>
               ....
               ....
            </select>
    <input type="submit" name="submit" value="Submit" />
    </form>

@karthik-I think so he is asking like we do in facebook....
When we enter name and country ...it helps to search for that...you will enter the search term in one testbox only....that's why we must use a single variable '$term' instead of using '$term' and '$country'.....
It is just my point of view....as i intercepted this problem...

@karthik-I think so he is asking like we do in facebook....
When we enter name and country ...it helps to search for that...you will enter the search term in one testbox only....that's why we must use a single variable '$term' instead of using '$term' and '$country'.....
It is just my point of view....as i intercepted this problem...

oh.. ok.. ok..

I have made this code in past.
Hope this will help you.Add it in your code and echo query you will understand it.
You can change upto your requirement.

<?
function searchString($str,$matchAr)
{
	$out = '';
	$str = preg_replace('/\s\s+/', ' ', $str);
	$temp = explode(' ',$str);
	foreach($matchAr as $match)
	{
		$out.="(";
		foreach($temp as $value)
		{
			$out.="$match LIKE '%$value%' AND ";
		}
		$out = substr($out,0,-4);
		$out.=") OR ";
	}
	return substr($out,0,-4);
}
$where.=" AND (".searchString($key,array('title','name')).")"; 
// if user type aaa , it will match with title field and name field
// if user type aaa bbb , it will check search must contain aaa bbb both. Either in title or name. 
echo $sql = "select * FROM tbl_name WHERE $where ";
?>

Thanks for the posts guys. Sorry if i confused some of you guys. What i was looking for was more of the person puts in the name and then there is a drop down menu where he can select country and then province/state and it will narrow down the searches that way. for example you type "John" into name search. Then you select "Canada" from the Country drop down menu, then you select British Columbia from the province and it will search for John in British Columbia.
Thanks for the help

Thanks for the posts guys. Sorry if i confused some of you guys. What i was looking for was more of the person puts in the name and then there is a drop down menu where he can select country and then province/state and it will narrow down the searches that way. for example you type "John" into name search. Then you select "Canada" from the Country drop down menu, then you select British Columbia from the province and it will search for John in British Columbia.
Thanks for the help

Mark as solved if your problem solved...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.