Jump to content

Facebook like Search Engine


cary1234
Go to solution Solved by fastsol,

Recommended Posts

Greetings, as what the title says I want to add search function to my website that looks like facebook (even if you dont press ENTER the result can be seen already and it searches for every letter combination ex. if I type 123 the result would be cary1234)

I already tried searching to search in google the results are as follows

1. http://www.9lessons.info/2009/06/autosuggestion-with-jquery-ajax-and-php.html

I try to copy-paste the code, but they arent working. :(
I also dont understand the code.




2. http://www.codeforest.net/simple-search-with-php-jquery-and-mysql

 

This link is much better I think, it doesnt give me exact facebook searching but the codes has comments so I can understand. Problem is I'm stuck in this code.

 

<?php
//if we got something through $_POST
if (isset($_POST['search'])) {
    // here you would normally include some database connection
    include('db.php');
    $db = new db();
    // never trust what user wrote! We must ALWAYS sanitize user input
    $word = mysql_real_escape_string($_POST['search']);
    $word = htmlentities($word);
    // build your search query to the database
    $sql = "SELECT title, url FROM pages WHERE content LIKE '%" . $word . "%' ORDER BY title LIMIT 10";
    // get results
    $row = $db->select_list($sql);
    if(count($row)) {
        $end_result = '';
        foreach($row as $r) {
            $result         = $r['title'];
            // we will use this to bold the search word in result
            $bold           = '<span class="found">' . $word . '</span>';    
            $end_result     .= '<li>' . str_ireplace($word, $bold, $result) . '</li>';            
        }
        echo $end_result;
    } else {
        echo '<li>No results found</li>';
    }
}
?>




I tried to fix the code but I'm failed, the error is Fatal Error: Call to undefined method::mysqli_select_list() in ....... line 14

<?php
	//if we got something through $_POST
	if (isset($_POST['search'])) {
    // here you would normally include some database connection
    include('db_Connect.php');
	$db = mysqli_connect($db_Host, $db_User, $db_Pass, $db_Name);
    // never trust what user wrote! We must ALWAYS sanitize user input
    $word = mysqli_real_escape_string($db, $_POST['search']);
    $word = htmlentities($word);
    // build your search query to the database
    $sql = "SELECT fname FROM pages WHERE fname LIKE '%" . $word . "%' ORDER BY title LIMIT 10";
    // get results
    
    $row = $db->select_list($sql);
    if(count($row)) {
        $end_result = '';
        foreach($row as $r) {
            $result         = $r['title'];
            // we will use this to bold the search word in result
            $bold           = '<span class="found">' . $word . '</span>';    
            $end_result     .= '<li>' . str_ireplace($word, $bold, $result) . '</li>';            
        }
        echo $end_result;
    } else {
        echo '<li>No results found</li>';
    }
}
?>

I want to change it to mysqli, but I dont know. Please help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.