skip to Main Content

I have this site http://mxcounters.com/traffictack/

Users come to site register and logged in. After login they search their domain for some seo reviews. Now all I want to store the each user search into database separately. Then I will show the searched domain by each user to their account page.

I have tried some last activity method to store in database but it is not as much useful please help me on this:

       $time_since = now() - $this->session->userdata('last_activity');
       $interval = 300;
       // Do nothing if last activity is recent
       if ($time_since < $interval) return;
       // Update database
       $updated = $this->db
       ->set('last_activity', now())
       ->where('id', $user_id)
       ->update('users');

2

Answers


  1. If you want to insert each search you can use:

    First create a table in which the records are stored.

    CREATE TABLE `usersearchs` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `user_id` VARCHAR(50) NOT NULL DEFAULT '0',
    `search` VARCHAR(50) NOT NULL DEFAULT '0',
    `time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    INDEX `id` (`id`)
    )
    ENGINE=InnoDB
    ;
    

    In this table the timestamp is automaticly added to the entries.

    then use a insert query on each search:

    INSERT INTO usersearch (user_id, search) VALUES ($user_id, $search_string);
    

    If you only want to update the last_activity column:

    Alter your table that after every table update the last_activity is updated and use a second column e.g. named searchString to contain the last searched item.

    ALTER TABLE `users`
    CHANGE COLUMN `last_activity` `last_activity` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    ADD COLUMN `searchString` VARCHAR(50) NOT NULL AFTER;
    

    Then use a query on each search:

    UPDATE users SET searchString = `$searchstring` WHERE id = $userID
    

    Please use right escaping when using databases (PDO prepared statements are a good option)

    Login or Signup to reply.
  2. Try this

    In conteroller

    $userId = $this->session->userdata('user_id');
    if(empty($userId)){
        redirect('login'); # this for user not logged
    }
    else{
        $search  = $this->input->post('domain');
    
        $resut = $this->method_name->serach_domain($userId, $search);
        if(!empty($resut)){
    
            #have data. if user come here set another methods for Insert data
            $this->model_name->insert_search_result(($userId, $search);
    
        }
        else{
    
            # nodata
            echo "No result found"
        }
    
    }
    

    In Model

    function insert_search_result(($userId, $search){
    
        $data = array(
           'user_id' => $userID ,
           'search' => $search,
           'timestamp' => date('Y-m-d H:i:s')
        );
    
        $this->db->insert('mytable', $data);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search