Page 1 of 1

Click counter

Posted: 2021-01-07 11:05
by hhansen
I want to update a counter every time a user click on a record in table view.

I have created a field "vist" and added code in the hook-file:

function annoncer_dv($selectedID, $memberInfo, &$html, &$args){
if ($selectedID){
$id=makeSafe($selectedID);
$res=sql("UPDATE annoncer SET annoncer.vist=annoncer.vist+1 WHERE annoncer.id=$id", $eo);
}

}

It is however only the last entered record it works on... Any suggestions?

Re: Click counter

Posted: 2021-01-07 13:46
by onoehring
Hi,

would you tell us, from where (when) you call your function?
Your code looks fine to me but you should make sure that it's called every time - probably check if SelectedID is set in the _init function - and if it exists, call your function.

Olaf

Re: Click counter

Posted: 2021-01-07 16:29
by hhansen
Hi,

When I am in tableview and click on a record, and I have tried to add "sleep(5);" after the update call, where I can see it is called.

I want a counter showing how many times the record has been viewed, but maybe there is another way to do this?

Re: Click counter

Posted: 2021-01-07 20:25
by pbottcher
Hi,

I would guess that your vist field is not initialized, so the sql statement fails to increase the counter.

Try

Code: Select all

function annoncer_dv($selectedID, $memberInfo, &$html, &$args){
  if ($selectedID){
    $id=makeSafe($selectedID);
    $res=sql("UPDATE annoncer SET annoncer.vist=ifnull(annoncer.vist,0)+1 WHERE annoncer.id=$id", $eo);
  }

}

Re: Click counter

Posted: 2021-01-08 04:29
by hhansen
Hi,
I have just copied your changes, and it works perfect - thanks a lot !!

Re: Click counter

Posted: 2022-02-01 07:16
by pasbonte
Hello HHANSEN

I see that you have made a click counter, can you explain me more? I'm a beginner...

I see roughly but I don't know what to do: create a VIST field and where to place the code...