www.webdeveloper.com
Results 1 to 11 of 11

Thread: HTML Search Box, without database

  1. #1
    Join Date
    Jan 2010
    Posts
    44

    HTML Search Box, without database

    Hi guys

    I have searched on the forum for this answer and could not find one.

    I have a client, who wants a search box on the header on each page of the site.

    The site consist of several php pages, with plain content text in. The reason why its .php is because I use includes for the menu.

    The problem I have is how to code a search box that search the pages for the entered query string and then display the results on a search.php page.

    My php knowledge is bad so I don't know how to do it and I'm not gonna use a database. Is this even possible?

    Please help,
    Thanks

  2. #2
    Join Date
    Oct 2006
    Posts
    52
    For each PHP file you wish to search the contents of, you could read the file into a string using file_get_contents()
    http://php.net/manual/en/function.file-get-contents.php

    Then you can attempt to locate the search item using strrpos()
    http://php.net/manual/en/function.strrpos.php

    Hope this helps.

  3. #3
    Join Date
    Jan 2010
    Posts
    44
    Ok cool, I'll see what I can figure out.

  4. #4
    Join Date
    Jan 2009
    Posts
    3,346
    I would recommend caching the results of those multiple file reads (of course discarding words like the, and, is, a, etc and all html and none text sections). The preference would be to use a database since %like% searches are relatively easy to execute and usually sufficient.

  5. #5
    Join Date
    Jan 2010
    Posts
    44
    im lost with the php code.

  6. #6
    Join Date
    Jan 2010
    Posts
    84
    Quote Originally Posted by claytonseo View Post
    im lost with the php code.
    You should have posted this in the PHP forum, since it doesn't really have anything to do with HTML.

    I would suggest that you start learning some PHP. Try starting with the tutorials on PHP and SQL at http://w3schools.com/. I've found them to be a good jumping off point. Once you have a better grasp of PHP, it shouldn't be too difficult to create a search script.

    Alternatively you could use Google's custom search, which allows you to make use of Google's search engine that will only search your website, and you can embed a search box on your site.

  7. #7
    Join Date
    Jan 2010
    Posts
    44
    Quote Originally Posted by Jerail View Post
    You should have posted this in the PHP forum, since it doesn't really have anything to do with HTML.
    soz, the google standard search box wants you to post ads on your website, which my clients does not want, and they dont wanna pay extra for a search box.

    thanks, gonna dig into php code, deadline approaching fast

  8. #8
    Join Date
    Jan 2010
    Posts
    44
    got the solution with the help of my fellow developer. I wanna post the code, but its heavy PHP, some1 wanna move the topic to the PHP section?

  9. #9
    Join Date
    Jan 2010
    Posts
    44
    found a bug that you are not able to search for more than one word. otherwise its working fine.

  10. #10
    Join Date
    Jan 2010
    Posts
    44

    Solution

    Create a folder in your root directory that contains all the contents of each page, e.g: about.txt, contact.php etc. NO coding, just plain text content.

    search.php

    <?
    function get_param($name, $type="post", $default = "")
    {
    $returnee = "";

    if(isset($_POST[$name]) && (($type == "post") || ($type == "both")))
    $returnee = $_POST[$name];
    else if(isset($_GET[$name]) && (($type == "get") || ($type == "both")) )
    $returnee = $_GET[$name];
    else
    $returnee = $default;

    if (1 == get_magic_quotes_gpc())
    $returnee = stripslashes($returnee);

    return trim($returnee);
    }

    function get_dir($name){

    $temp = array();
    $cnt = 0;
    if($dh = opendir($name)) {
    while(($file = readdir($dh)) !== false){

    if($cnt >= 2)
    array_push($temp, $file);

    $cnt++;
    }
    $cnt = 0;
    closedir($dh);
    }

    return $temp;
    }

    function search($text_search, $dir="search_docs/"){

    $text_search = explode(" ", $text_search);

    $files = get_dir($dir);

    $buffer = array();
    foreach($files as $i => $v)
    if(is_file($dir.$v))
    array_push($buffer, array($dir.$v, file_get_contents($dir.$v)));

    $found = array();
    foreach($buffer as $i => $v)
    foreach($text_search as $ti => $tv)
    if(strpos($v[1], $tv) !== false && !in_array($v[0], $found))
    array_push($found, $v[0]);

    return $found;
    }

    $index = array();
    $index["search_docs/contact.txt"] = array("contact.php", "Contact page");
    $index["search_docs/disclaimer.txt"] = array("disclaimer.php", "Disclaimer page");
    $index["search_docs/downloads.txt"] = array("downloads.php", "Downloads page");
    $index["search_docs/estimation.txt"] = array("estimation.php", "Estimation page");
    $index["search_docs/faq.txt"] = array("faq.php", "FAQ page");
    $index["search_docs/index.txt"] = array("index.php", "Home page");
    $index["search_docs/onlinep.txt"] = array("onlinep.php", "Online Proofs page");
    $index["search_docs/quote.txt"] = array("quote.php", "Quote page");
    $index["search_docs/repro.txt"] = array("repro.php", "Repro page");
    $index["search_docs/services.txt"] = array("services.php", "Services page");

    $user_input = get_param("search", "get");

    if($user_input != "" && $user_input != "Type search text here")
    $found = search($user_input);
    ?>


    IN THE HTML

    <?
    if($user_input != "" && $user_input != "Type search text here"){
    ?>

    <span style="font-size:18px;">Search results for <span style="color:#01a4a6;">"<?=$user_input;?>"</span></span><br/><br/>
    <?
    if(count($found) == 0)
    {?>
    No results found
    <?}
    else{
    foreach($found as $i => $v)
    echo "<a href='".$index[$v][0]."'>".$index[$v][1]."</a><br/>";
    }
    }
    else{?>
    Please enter text in the search box and press enter
    <?}?>

  11. #11
    Join Date
    Oct 2011
    Posts
    1

    HTML search function

    i already create in-house HTML that only can be use at my office & cannot be view from external. my HTML page already publish. my problem, my search button not function, i not using database this HTML site only for internal.
    Kindly advise

    thx
    fairoz

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles