0

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

4
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by karthik_ppts
0
$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%' ");
0

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

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

0

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

0

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

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

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.