CodeIgniter Forums
search application help - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: search application help (/showthread.php?tid=24111)



search application help - El Forum - 10-31-2009

[eluser]Unknown[/eluser]
Hi all,

I'm at the conceptual stage of creating a small search application.

The most basic functionality is to pull data from a MSSQL database holding information about 200 members in two tables. Search parameter can be anything like 'first_name', 'last_name', 'position' etc.


I would like to know from the experienced Codeigniters whether it is an overkill to use Codeigniter for this?


Thanks


search application help - El Forum - 10-31-2009

[eluser]Krzemo[/eluser]
Such app is just few lines of non MVC, procedural code and I guess everything beyond that is an overkill.
If you need to have it done asap and dont know CI - dont bother learning it. On the other hand it might be perfect start with CI.
CI is supposed to help you code apps - you have to find out for yourself if using makes sense or not...

Regs
K.


search application help - El Forum - 10-31-2009

[eluser]FutureKing[/eluser]
[quote author="codan" date="1257010826"]Hi all,

I'm at the conceptual stage of creating a small search application.

The most basic functionality is to pull data from a MSSQL database holding information about 200 members in two tables. Search parameter can be anything like 'first_name', 'last_name', 'position' etc.


I would like to know from the experienced Codeigniters whether it is an overkill to use Codeigniter for this?


Thanks[/quote]

Use SQL "LIKE" keyword. No Need to use CI.


search application help - El Forum - 10-31-2009

[eluser]Unknown[/eluser]
Hey Cshamoh and FutureKing

Thanks for your suggestions. I'll do it without CI now and and in CI later to learn the CI way.

Thanks again


search application help - El Forum - 10-31-2009

[eluser]skunkbad[/eluser]
Code:
class Search extends Controller {
    private $keyword = null;
    private $error = null;
    private $local = 0;
    private $min_chars = 3;
    private $max_chars = 30;
    private $boundary = '<!-- SEARCH BOUNDARY -->';
    private $message_A= "Your search results for:";
    private $message_B = "Invalid Search <span style='font-style:italic; font-size:80%;'>(too short or too long)</span>";
    private $message_C = "The value that you are searching for is either less than 3 characters, or more than 30 characters. Please try to search again with a search value between 3 and 30 characters in length.";
    private $message_D = "No Search Value";
    private $message_E = "The search program can't search for an empty value. Try typing something in to the search form before pressing the \"Search\" button.";
    private $message_F = "There were no instances of&nbsp;";
    private $message_G = "&nbsp;found on any of the pages of this site";
    private $no_title = "Untitled";
    private $limit_extracts = "10";
    private $contents;

    public function index()
    {
        $this->keyword = $this->input->post('keyword');
        $this->searchHeader();
        $this->searchFiles();

        // insert page specific data into template
        $data = array(
            'title' => 'Brian\'s Web Design - Search',
            'robots' => 'noindex,nofollow',
            'style_sheet_names' => array(
                                        'css/style.css',
                                        'css/page_specific/search/screen.css'
            ),
            'media_targets' => array(
                                    'screen',
                                    'screen'
            ),
            'javascripts' => array(
                                    'js/target_blank.js'
            ),
            'slogan' => 'Great Web Design - Quality You Can Trust',
            'content' => $this->load->view('search', array('search_results' => $this->contents) , TRUE)
        );
        $this->load->view('template_content', $data );
    }

    private function searchHeader() {
        $this->searchError();
        if ($this->error!=1&&isset;($this->keyword)){
            $this->contents = "<h2>$this->message_A <i>".htmlentities(stripslashes($this->keyword))."</i></h2>\n";
        }
    }

    private function searchError() {
        if (isset($this->keyword)){
            if(strlen($this->keyword)<$this->min_chars||strlen($this->keyword)>$this->max_chars) {
                $this->contents =  "<h2>$this->message_B</h2>";
                $this->contents .=  "<p>$this->message_C</p>";
                $this->error = 1;
            }
        }else{
            $this->contents =  "<h2>$this->message_D</h2>";
            $this->contents .=  "<p>$this->message_E</p>";
            $this->error = 1;
        }
    }

    private function searchFiles() {
        if (isset($this->keyword)&&$this->error!=1){
            $pageList = file(site_url('sitemap/txt', 'http'), FILE_IGNORE_NEW_LINES);
            foreach($pageList as $page){
                $fgcdata = file_get_contents($page);
                $chopped = explode($this->boundary,$fgcdata);
                static $count_hits = null;
                if (count($chopped) >= 3){
                    $text = null;
                    for($i=0;$i<=count($chopped)-1;$i++){
                        if ($i%2){
                            $text .= $chopped[$i];
                        }
                    }
                    $text = strip_tags($text);
                    $keyword_html = htmlentities($this->keyword);
                    $do=stristr($text, $this->keyword)||stristr($text, $keyword_html);
                    if($do)    {
                        $count_hits++;
                        if(preg_match_all("=&lt;title[^&gt;]*>(.*)&lt;/title&gt;=siU", $fgcdata, $title)) {
                            if(!$title[1][0])
                                $link_title=$no_title;
                            else
                                $link_title=$title[1][0];
                        }
                        else {
                            $link_title=$no_title;
                        }
                        ($page == $pageList[0]) ? $page = base_url() : $page = $page;
                        $this->contents .=  "<p class=\"itemP\">\n\t<a >$count_hits.  $link_title</a>\n\t<br />\n\t";
                        $this->keyword = preg_quote($this->keyword);
                        $this->keyword = str_replace("/","\/","$this->keyword");
                        $keyword_html = preg_quote($keyword_html);
                        $keyword_html = str_replace("/","\/","$keyword_html");
                        $this->contents .=  "<span class=\"extract\">";
                        if(preg_match_all("/((\s\S*){0,3})($this->keyword|$keyword_html)((\s?\S*){0,3})/i", $text, $match, PREG_SET_ORDER)); {
                            if(!$this->limit_extracts)
                                $number=count($match);
                            else
                                $number=$this->limit_extracts;
                                for ($h=0;$h<$number;$h++) {
                                    if (!empty($match[$h][3]))
                                        $this->contents .= sprintf("<i><b>..</b> %s<b>%s</b>%s <b>..</b></i>", $match[$h][1], $match[$h][3], $match[$h][4]);
                                    }
                        }
                        $this->contents .=  "</span></p>\n";
                        flush();
                    }
                }
            }
            if ($count_hits == null){
                $this->contents .=  "<p>$this->message_F<b>" . htmlentities($this->keyword) . "</b>$this->message_G.</p>";
            }
        }
    }

}



search application help - El Forum - 10-31-2009

[eluser]skunkbad[/eluser]
I couldn't explain the above code in the post because of char limit. I use this to search files on my site. No DB required. Comments tell the code what to include in the search. It is not fast, so other methods are probably better, but it is an example of another way to search.