大家好
很久以前,一開始台灣一些公司在製作網站的時候
並沒有想到使用資料庫,甚至老闆還會問你
"為什麼一定要用資料庫? 有伺服器不就可以了嗎? 我問別人也是伺服器就可以了啊?"
這種有理說不清的情況
 
所以以前有些網站我就是單純使用靜態頁面搜尋來製作搜尋功能
主要就是分享語法給大家
順便打上註解
算是新手教學吧
 
其實這種靜態頁面的搜尋方式也不是說很不好
如果只是幾百個產品 html 頁面
其實搜尋也是很快
只是如何比對關鍵資料與提取要顯示的資訊才是比較大的問題
不過我這邊主要是呈現 搜尋資料夾以及讀取檔案
給大家參考囉
 
題外話:  有時候想一想以前我寫的程式也真的是很糟糕,不過沒辦法,人總是慢慢成長學習嘛
 
 
<?php
//取得從 GET 或是 POST 的關鍵字
$Keyword = $_REQUEST["Keyword"];
//準備查看有哪些資料檔案需要搜尋
$go_file = array();
//要排除的
$no_load = array(
    'css',
    'e-catalog',
    'images',
    'pdf',
    'Scripts',
    'Templates',
    '..',
    '.'
);
//打開現在資料夾
if ($handle = opendir('.'))
{
    //開始一層一層查看
    while (false !== ($entry = readdir($handle)))
    {
        //不是排除資料夾 則 進去看看
        if (!in_array($entry, $no_load) && is_dir($entry) && $file_dir = opendir($entry))
        {
            while (false !== ($file_name = readdir($file_dir)))
            {
                if ($file_name != '..' && $file_name != '.')
                {
                    //紀錄要查看檔案
                    $go_file[] = $entry . '/' . $file_name;
                }
            }
        }
    }
    closedir($handle);
}
//有找到要搜尋的檔案 與 關鍵字
if (count($go_file) > 0 && $Keyword != "")
{
    echo $KD;
    $total = 0;
    $html = '';
    foreach ($go_file as $file)
    {
        if (is_file($file))
        {
            //取得檔案內容
            $file_contents = file_get_contents($file);
            if (strpos(''.(strtolower($file_contents)) , $Keyword))
            {
                //有找到字  做一些處理...
                //有找到字  做一些處理...
                //有找到字  做一些處理...
            }
        }
    }
}
?>
 
有問題也可以交流喔
感恩