[eluser]brian88[/eluser]
I have a search feature that i made on my site. Every time a person searches for something it adds their search text to the database. Well with this code, sometimes it adds it once, like normal, but other times it adds their search 2-3 times to the database.
I only want to add the keyword once to the database, not 2-3 times. So sometimes this code screws up.
With this function you could actually search by using the url, example if i was search for "funny jokes"
mysite.com/search/funny-jokes
or I could just use the search form on my website to search "funny jokes". they both use this function.
controller
Code:
// $keyword comes in via URL, mysite.com/search/funny-jokes
function search($keyword = ''){
$this->load->model('search_mod');
$this->load->helper('text');
// if it comes from the url
if($keyword != ''){
if( strpos(uri_string(), '%') ){ // if spaces in url
$keyword = str_replace(' ', '-', $keyword); // remove spaces
redirect('search/'.$keyword); // redirect to itself with the correct url
}else{
$usersKey = $keyword; // users search keyword from URL
$usersKey = str_replace(array('-jokes','-joke'), '', $usersKey); // get rid of "joke"
$usersKey = str_replace('-', ' ', $usersKey); // get rid of dashes
// adds to the database.
$this->add_keyword_db( $usersKey ); // add key to DB
// this gets ran 1-3 times
}
// if it comes from form submit
}elseif( $this->input->post() ){
$r = trim($this->input->post('search')); // remove spaces
$r = url_title($r, 'dash', true); // chuck-norris-kills
redirect('search/'.$r); // redirect to this same method
}
// perfecting full text search
if(isset($usersKey)){ // if there is already a keyword, then search for it
if(strlen($usersKey) >=4){
$searchWords = $this->search_mod->getSearchWords( $usersKey ); // get full text search words like +furry +beaver*
$data['results'] = $this->search_mod->searchDB( $searchWords ); // search with our new words
$usersKey = str_replace(array('+', '*'), '', $searchWords); // remove + and * to highlight words
$data['keyHighlight'] = $usersKey;
$data['key'] = str_replace('-', ' ', $keyword); // orginal word
$data['count'] = count( $data['results'] );
}else{
$data['error'] = 'Your search needs to be longer than 4 letters.';
}
}
// get normal data
$footData['total'] = $this->main_mod->getTotalJokes();
$navData['genNav'] = $this->main_mod->getGenNav();
$navData['topicNav'] = $this->main_mod->getTopicNav();
$data['ok'] = '';
// get popular keywords
$data['keywords'] = $this->search_mod->popularKeywords();
// views
$this->load->view('main/header_view.php',$data);
$this->load->view('nav_view.php', $navData);
$this->load->view('search_view.php', $data);
$this->load->view('main/footer_view.php', $footData);
} // end add
function that gets called for adding to database
Code:
function add_keyword_db($keyword){
// get data
$data['keyword'] = $keyword;
$data['usersIP'] = $this->session->userdata('ip_address');
$data['date'] = time();
$this->main_mod->addRow('search', $data);
}
model
Code:
function addRow($table, $data){
$insert = $this->db->insert($table, $data);
return $insert;
}