Welcome Guest, Not a member yet? Register   Sign In
row gets added 2-3 times to database
#1

[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;
}
#2

[eluser]Beginers[/eluser]
you need to created another function that will test if the keywords he/she searches is in the database already like...

Code:
$query = $this->db->query("SELECT * FROM table_name WHERE table_name.keywords REGEXP'^$searchKey$'");
or
$query = $this->db->query("SELECT * FROM table_name WHERE table_name.keywords LIKE'%$searchKey%'");


if($query->num_rows()>0):
      //function dosave();
else:
     //no function keep silent
endif;

just like that..
#3

[eluser]brian88[/eluser]
I want to see the same keywords in my database so i know what users are searching for. So if there are 100 searches for "horses" then I know thats a popular keyword. So I dont think your code will work

I want 1 search to only insert 1 row. Right now 1 search inserts 1-3 rows of the same keyword.

I think it has something to do with me redirecting the current page to the same page its already on.
#4

[eluser]LinkFox[/eluser]
My guess is your insert code is being called 3 times. Put in a echo into the head of the function and see if this is the case. Put one on the controller and one in the function, then you can find out whats being called 3 times.
#5

[eluser]brian88[/eluser]
i put an echo in front of the insert and it is only being echoed once.

Ive ran several test and i cant find out why sometimes it adds the keyword once and other several times... my database is showing its even adding the same keyword by different ip address's.




Theme © iAndrew 2016 - Forum software by © MyBB