kunal
kunal

Reputation: 4248

how to update data only once in a day?

I am facing One problem. I am using Cakephp2.0 Framework. I want that searchcount will be updated once in a day. For example:-

Here is my code:

Starting serachcount = 0;
if it access today(26dec 2016) means n number of times its count will 
be **serachcount** = 1;

If it is not  access by tomorrow (27dec 2016) its count should not be 
 updated its still **serachcount** = 2;
And this process carries on

Can anyone help me here is my function

Here is my code what I have tried

public function visits($id=null){
        $this->loadModel('User');
        if($this->request->is('post')){
            $data= $this->request->data;
            $id = $data['id'];
            $userdata = $this->User->find('first',array(
                'conditions'=>array('User.id'=>$id),
                'fields'=>array('id','searchcount')
                )
            );
            $this->User->id = $id;
            $searchcount = $userdata['searchcount'];
            $searchcount = $searchcount+1;
            $this->User->saveField('searchcount',$searchcount);
            echo $searchcount; die;
        }
    }

Upvotes: 0

Views: 96

Answers (1)

Manohar Khadka
Manohar Khadka

Reputation: 2195

You just need simple logic as mentioned by @Ayaou:

1. add field in your table eg. count_date 
2. do not forget to update count_date when searchcount get updated
3. make your visits function something like this:

if($this->request->is('post')){
 $data= $this->request->data;
 $id = $data['id'];
 $userdata = $this->User->find('first',array(
   'conditions'=>array('User.id'=>$id),
   'fields'=>array('id','searchcount','count_date')
            )
        );
  $this->User->id = $id;
  $searchcount = $userdata['searchcount'];
  $countDate = $userdata['count_date'];
  $dateToday = date('Y-m-d');
  if($countDate != $dateToday) {
     $dataToUpdate["count_date"] = $dateToday;
     $dataToUpdate["searchcount"] = $searchcount+1;
     $this->User->save($dataToUpdate);
   } else {
     // already updated today
   }
 }

Upvotes: 1

Related Questions