Tuesday, 3 June 2014

How to Make a Simple Search Engine using PHP and MySQLi

This post i make it small search engine using php and mysqli. this search engine search to user date in database.similar like google search engine. its should be find keywords in database record to search related description similar character. 

search-engine-img

Live Demo         Download



Database

Database connect to mysqli query  create below format table. the fields to store on user given information.


CREATE TABLE `search`
(
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(50) NOT NULL,
`desp` varchar(50) NOT NULL,
)

Index.php

<?php
$db = new mysqli('localhost', 'root', '', 'test');
session_start();
    if(isset($_POST['submit']))
    {
        $search=$_POST['search'];
        $_SESSION['title']= $search;
        if(($_SESSION['title'])!="")
        {
        header("location:index.php");
        }
        else
        {
        echo "<script> alert('search box as been empty') </script>";
        }
    }
?>
    <html>
    <head>
    <title>mostlikers</title>
    <link rel="stylesheet" href="st.css" />
    </head>
    <body>
    <div class="login">
    <form method="post">
    <p align="center"><img src="http://2.bp.blogspot.com/-Hl_N3uKjgCg/UXKQ-5Am80I/AAAAAAAAAP0/hQlND-p7P6I/s1600/logos.png" /></p>
    <p><?php if(isset($_SESSION['title'])) { ?>
    <input name="search" type="search" list="searchkey" value="<?php echo $_SESSION['title'];?>" class="search" />
    <?php } else { ?>
    <input name="search" type="search" list="searchkey" placeholder="Just type your text here press enter ex : Mostlikers"  class="search" />
    <?php } ?></p>
    <datalist id="searchkey">
    <?php
    $tile=$db->query("SELECT * FROM `search`");
    while($storetitle=mysqli_fetch_object($tile))
    {
    ?>
    <option  value="<?php echo $storetitle->title ?>">
    <?php } ?>
    </datalist>
    <p align="center">
    <input type="submit" name="submit" id="click" class="but" value="Mostliker search"  />
    <input type="submit" name="submit" class="but" value="I m Feeling lucky" /> </p> 
    <p class="lang">Add content: 
    <a href="add-view.php">Hindi</a>&nbsp;<a href="add-view.php"> Bengali </a>&nbsp;
    <a href="add-view.php">Telugu </a>&nbsp;<a href="add-view.php">Marathi </a>&nbsp;
    <a href="add-view.php">Tamil </a>&nbsp;<a href="add-view.php">Gujarati</a><a href="add-view.php">
    Kannada</a>&nbsp;<a href="add-view.php"> Malayalam</a></p>

    <?php if(isset($_SESSION['title'])) {
    if(($_SESSION['title']!=""))
    {
    $data=$_SESSION['title'];
    $view=$db->query("select * from search where title like '%$data%' || desp like '%$data%' limit 10 ");
    $check=mysqli_num_rows($view);
    if($check!="")
    {
    while($descri=mysqli_fetch_object($view))
    {
    ?>
    <div class="reslt">
    <h3 id="resuil-title"><?php echo $descri->title; ?></h3>
    <p class="Description">
    <?php $description = str_replace($data, '<span class="highlight">'.$data."</span>", $descri->desp);
    echo $description; ?><p>
    <hr>
    </div>
    <?php } } else { ?>
    <div class="reslt">
    <h3 id="resuil-title">Searching Data not fond</h3>
    <p class="Description">
    Add new data enter and check the correct keyword
    <p><hr>
    </div>
    <?php } } } ?>
    </form>
    </div>
    </body>
    </html>

add-view.php

<?php 
$db = new mysqli('localhost', 'root', '', 'test');// change the database connections
?>
<?php
if(isset($_POST['ADD'])!="")
 {   
    $title=$_POST['title'];
    $description=$_POST['desc'];
    $insert=$db->query("INSERT INTO `search`(title,desp) values('$title','$description')"); 
    if($insert)
    {
        echo "<script> alert('search the title now') </script>";
    }
    else
    { 
         echo "<script> alert('unable insert to your details') </script>";  
    } 
 }
?>

<html>
<head>
<title>mostlikers</title>
<link rel="stylesheet" href="st.css" />
</head>
<body>
<div class="mostlikers-top">
<a href="http://karthickinfotech.blogspot.in/">
<img  name="mostlikers" style="border:none;" src="demo.png" title="mostlikers" alt="mostlikers" /></a>
</div>
<div class="login">
<div class="addnew">
<form name="add" method="post">
<h2>Add New Record</h2>
<p><label>Title</label><br />
<input type="text" size="40" name="title" title="title" /></p>
<p><label>Descriptions</label><br />
<textarea name="desc" rows="5" cols="30"></textarea></p>
<input type="submit" name="ADD" class="submit" value="ADD" title="ADD" />
</form>
</div></div>
</div> <div class="mostlikers-bottom">
<h3><a href="karthickinfotech.blogspot.in/p/chat.html">Click more mostlikers Demos</a></h3></div>
</body></html>

CSS
#resuil-title {color:#1a0dab;font-weight: bold;
font-style: normal;  }
.login form { width:600px; height:0 auto;  margin:0 auto; } 
.search { border: 1px solid #ccc;width:572px;min-height: 30px;
padding: 4px 20px 4px 8px;font-size: 18px;-moz-transition: all .2s linear; 
-webkit-transition: all .2s linear;transition: all .2s linear;}
.search:hover { width: 572px;border-color:#999999;}       
.search:focus { width: 572px;border-color:#03F;outline: none;}
.but{ padding: 6px 10px;color: #222;border-radius:3px solid #00F;
border:1px solid #CCC;background: #f4f4f4;margin-bottom:4%;font-weight:bold;cursor:pointer;
font-size:75%;font-family: arial,sans-serif;-moz-border-radius: 3px ;-webkit-border-radius: 3px;
border-radius: 3px ;  }
.but:hover { padding: 6px 10px;color: #222;background: #f4f4f4;margin-bottom:4%;
cursor:pointer;font-size:75%;border-color:#f7f7f7;font-family: arial,sans-serif;
-moz-border-radius: 3px ;-webkit-border-radius: 3px; border-radius: 3px ;  }
.but:before { content: '🔍'; } 
.lang a{ font-weight:100px;text-decoration:none;}
.highlight { font-weight:bolder; color:red; font-style:bold; }

3 comments:

  1. okay so if i want to search for files ( i mean when someone like to list all files that talk about...html!!) how can i do it anyone tell me the code for this pleaase
    ! i make a link for my files in my website
    and i put files in the same directory as index.php and i test it on wamp and it work and i download the file so pleaase help me and thanks
    help me please

    ReplyDelete