Thursday, 17 March 2016

search example on selection wise search

01:14

How to write code  for following example  suppose
(1) if 
You   select  city Mumbai then it will display only city Mumbai record
(2)if you select country   India  then it will display only  country  India record
(3)if you select both city=Mumbai and country=India  then it will  record where Mumbai and India is matched..together..

(1)create table details :
create  table  details
(id   int    primary   key   auto_increment ,
 city  varchar(20),
 country  varchar(20)
 );
(2)write code for  search.php file given below :

<form action=search.php method=post>
                City        :<input type=text name=city>
                Country                :<input type=text name=country>
                <input type=submit name=submit value=Filter>
</form>
<?php
if(isset($_POST['submit']))
{
                $city=$_POST['city'];
                $country=$_POST['country'];
                $con=mysql_connect('localhost','root','');
                mysql_select_db('abhi',$con);
                if($_POST['city']==true && $_POST['country']==false)
                {
                                $sql="select * from details where city='$city'";
                                $result=mysql_query($sql);
                                echo '<table border=5><tr><td>Id</td><td>City</td><td>Country</td></tr>';
                                while($row=mysql_fetch_array($result))
                                {
                                                echo '<tr>';
                                                                echo '<td>';
                                                                                echo $row['id'];
                                                                echo '</td>';
                                                                echo '<td>';
                                                                                echo $row['city'];
                                                                echo '</td>';
                                                                echo '<td>';
                                                                                echo $row['country'];
                                                                echo '</td>';
                                                echo '</tr>';
                                }
                                echo '</table>';
                }
               
                else if($_POST['city']==false && $_POST['country']==true)
                {
                                $sql="select * from details where country='$country'";
                                $result=mysql_query($sql);
                                echo '<table border=5><tr><td>Id</td><td>City</td><td>Country</td></tr>';
                                while($row=mysql_fetch_array($result))
                                {
                                                echo '<tr>';
                                                                echo '<td>';
                                                                                echo $row['id'];
                                                                echo '</td>';
                                                                echo '<td>';
                                                                                echo $row['city'];
                                                                echo '</td>';
                                                                echo '<td>';
                                                                                echo $row['country'];
                                                                echo '</td>';
                                                echo '</tr>';
                                }
                                echo '</table>';
                }
                else if($_POST['city']==true && $_POST['country']==true)
                {
                                $sql="select * from details where city='$city' and country='$country'";
                                $result=mysql_query($sql);
                                echo '<table border=5><tr><td>Id</td><td>City</td><td>Country</td></tr>';
                                while($row=mysql_fetch_array($result))
                                {
                                                echo '<tr>';
                                                                echo '<td>';
                                                                                echo $row['id'];
                                                                echo '</td>';
                                                                echo '<td>';
                                                                                echo $row['city'];
                                                                echo '</td>';
                                                                echo '<td>';
                                                                                echo $row['country'];
                                                                echo '</td>';
                                                echo '</tr>';
                                }
                                echo '</table>';
                }
                else
                {
                                echo "No record";
                }
                }

?>

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

PHP Training Mumbai, PHP MySQL Training Mumbai, PHP Course Mumbai, PHP Institute Mumbai

 

© 2013 PHP TRAINING IN MUMBAI BY OM SIR . All rights resevered. Designed by OM SIR

Back To Top