Translate

Friday 25 July 2014

Autocomplete Searchbox in database using PHP

index.php:

<form method="post" action="search.php" id="form1">
<div  style="margin: 0px 0px 0px 516px;">
<input type="text" id="category" name="category"   value=""/>
<input type="submit" class="but_style" name="search_list" value="Search" style="width:auto; border:solid thin  #b32d00;
border-radius:.3em; color:#FFF; background-color:#f7803b; cursor:pointer; height:27px;" ></div>
</form>

search.php:

<?php
if(isset($_POST['search_list'])) {
        if(trim($_POST['category'])) {
            $con[] = " category_name LIKE '%".trim($_POST['category'])."%' ";
        }
        if($con) {
            $condition = " WHERE ";
            $condition .= implode(" AND ", $con);           
        }}
        $query4 = "SELECT * FROM demo $condition ORDER BY category ASC";
    $result4 = mysql_query($query4);
    while($row4 = mysql_fetch_array($result4)) {
        $city[] = $row4;
    }
?>
 

View the output:

<form method="post" action=""> 
<table width="100%" id="table_layout"  border="1">
<tr>
<th>S.No</th>
<th>Category</th>
<th>Category Name</th>
<th>Sub Category</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>

</tr>

<?php 
if($city) { 
foreach($city as $key1 => $val1) {
$slno = $key1+1;
if($slno > 10 ) {
echo "<tr class='table_tr2' id='row$slno' style='display:none;'>";
}else{
echo "<tr class='table_tr2' id='row$slno'>";
}
echo '<td width="5%">'.$slno.'</td>';
echo '<td width="12%">'.$val1['category'].'</td>';
echo '<td width="12%">'.$val1['category_name'].'</td>';
echo '<td width="12%">'.$val1['sub_category'].'</td>';
echo '<td width="15%">'.$val1['price'].'</td>';
echo '<td width="15%">'.$val1['quantity'].'</td>';
echo '<td width="15%">'.$val1['total'].'</td>';

//echo '<td><img src=upload/$val1['image] width=50px height=50px></td>';
echo '</tr>';
}
} else {
echo "<tr class='table_tr2'><td colspan='7' style='text-align:center;height:125px;'><span class='NoResult'>No Records Found</span></td></tr>";
}
?>
</table>
</form> 


Database Structure:

--
-- Table structure for table `demo`
--

CREATE TABLE IF NOT EXISTS `demo` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `category` varchar(150) NOT NULL,
  `category_name` varchar(150) NOT NULL,
  `sub_category` varchar(150) NOT NULL,
  `price` int(30) NOT NULL,
  `quantity` int(30) NOT NULL,
  `total` int(30) NOT NULL,
  `image` varchar(150) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=31 ;

--
-- Dumping data for table `demo`
--

INSERT INTO `demo` (`id`, `category`, `category_name`, `sub_category`, `price`, `quantity`, `total`, `image`) VALUES
(25, '4', 'jobs', '6', 5000, 30, 150000, 'rose1.jpg'),
(23, '2', 'food', '3', 30, 3, 90, '191356xcitefun-cartoon-pluto.jpg'),
(24, '3', 'names', '8', 400, 30, 12000, 'ajax-loader.gif'),
(19, '1', 'juice', '1', 40, 30, 1200, 'rose2.jpg'),
(17, 'food', 'juice', 'rice', 10, 41, 410, 'download.jpg'),
(26, 'food', 'food', 'dosai', 34, 8, 272, 'images.jpg');

Explanitation:



In this Coding we no need to use the javascript and any other library files to do autocomplete 
search concept and it can be coded by the Core PHP only and it is easy to understand.I hope this coding is to be very usefull.

No comments:

Post a Comment