Log In

Email:
Password:
Keep me logged in

Forgot Password

Email:




Social Media
     
CategoriesCSS (5)
jQuery (73)
PHP (30)
.htaccess (4)
Sponsored Links


Bold Search Term in Results
PHP Ajax Javascript jQuery
Demo | Browse Category | Friday, February 10, 2012 by




MySQL:
CREATE TABLE `your_database`.`bold_search_term` (
`result` TEXT NOT NULL ,
) ENGINE = MYISAM ;
PHP/Ajax:
search-results.php, Ajax file where the search results are processed.
<?php

mysql_connect
("server""username""password");

$q $_GET['q'];

$page 1;
if(
$_GET['page']){
    
$page $_GET['page'];
}

$rrp 5;
$o = ($page-1)*$rrp;

$x explode(" "$q);
for(
$i=0$i<count($x); $i++){
    
$t $x[$i];
    if(
$i==0){
        
$a .= "result LIKE '%$t%'";
    } else {
        
$a .= " OR result LIKE '%$t%'";
    }
}
$query mysql_query("SELECT * FROM your_database.bold_search_term WHERE $a ORDER BY result DESC LIMIT $o, $rrp");
$n mysql_num_rows($query);

if(
$q){
while(
$r=mysql_fetch_array($query)){
     
$results .= $r['result'].'<br>';
}

for(
$i=0$i<count($x); $i++){
    
$t $x[$i];
    
$results eregi_replace("(".$t.")+""<b>".$t."</b>"$results);
}

echo 
$results;

$query mysql_query("SELECT * FROM your_database.bold_search_term");
$n mysql_num_rows($query);
$pages ceil($n/$rrp);
$range 4;

if(
$pages>1){
    if(
$page>1){
    
$first '<a class="page" id="1">First</a> ';
    
$prev '<a class="page" id="'.($page-1).'">Prev</a> ';
    }
    if(
$page<$pages){
    
$next '<a class="page" id="'.($page+1).'">Next</a> ';
    
$last '<a class="page" id="'.$pages.'">Last</a> ';
    }
    
    for(
$p=($page-$range); $p<=($page+$range); $p++){
        if(
$p>=1&&$p<=$pages){
            if(
$p==$page){
                
$nav .= '<span style="color: #32baed">'.$p.'</span> ';
            } else {
                
$nav .= '<a class="page" id="'.$p.'">'.$p.'</a> ';
            }
        }
    }
}

echo 
$first $prev $nav $next $last;
}

?>
HTML/jQuery:
Main file
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function(){
$('#q').keyup(function(){
var q = $(this).val();
$.get('search-results.php?q='+q, function(data){
$('#results').html(data);
});
});
});
</script>
</head>
<body>
<input id="q"><br>
<div id="results"></div>
</body>
</html>
Views: 124 Likes: 1 Dislikes: 1






df
df

Wednesday, April 4, 2012 at 3:08:28 PM