Pages

Tuesday, July 10, 2012

Conducting a search Query


<?php

/*

    I have assumed you have created a database with a table named address_bk
    It should have the following fields:
    id: int(3) Set as primary
    fname: VARCHAR(20)
    lname: VARCHAR(20)
    phone: VARCHAR(10)
    email_address: VARCHAR(30)
    NB: This table will be used for the next several code samples
    Reach me at: allan.koskei@gmail.com
  
*/

class SearchContact
{
  
    //The user will be conducting search based on the first name
    private $fname;
  
    //Class constructor
    public function __construct($fname)
    {
        $this->fname = $fname;
        $this->searchQuery();
    }
  
    public function searchQuery()
    {
        //The '%$this->fname%' is a wildcard search that brings results corresponding to the inputed search item
        $sql = "SELECT * FROM address_bk WHERE fname LIKE '%$this->fname%'";
        $result = mysql_query($sql);
        $num_rows = mysql_num_rows($result);
      
        //If the query is working
        if($result){
            //If results exists
            if($num_rows > 0){
                ?>
                <table>
                    <tr>
                        <td><b>Firstname</b></td>
                        <td><b>Lastname</b></td>
                        <td><b>Phone number</b></td>
                        <td><b>Email Address</b></td>
                    </tr>
                <?php
                while($db_field = mysql_fetch_assoc($result)){
                    ?>
                    <tr bgcolor="#F2F2F2">
                        <td><?php echo $db_field['fname']; ?></td>
                        <td><?php echo $db_field['lname']; ?></td>
                        <td><?php echo $db_field['phone']; ?></td>
                        <td><?php echo $db_field['email_address']; ?></td>
                    </tr>  
                    <?php              
                }
                ?>
                </table>
                <?php
            }
            //If results doesn't exists
            else{
                echo "$this->fname does not exist!";
            }
        }
        //If the query fails or the connection fails
        else{          
            echo 'Error fetching data';
        }
    }
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search Contact Demo</title>
</head>

<body>
<h2>Search by name Demo</h2>
<form method="post" action="<?php $_SERVER['PHP_SELF']?>">
    <input type="text" name="fname_post" value="" />
    <input type="submit" value="Search Contact" />
</form>
<br /><br />
<!--I have placed the php code here so that the results will be displayed under the search textbox-->
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
    include_once 'connection.php';
    new SearchContact($_POST['fname_post']);
    mysql_close($server_connect);
}
?>

</body>
</html>

No comments:

Post a Comment