Detail view hit counter page reload problem

mahmoodee

Member
Hello
I add field element called 'hitcount' to my list .
I create a PHP form submission script and select 'onBeforeLoad' for 'Process script'
Code:
if (!$formModel->isEditable()) {
  $rowid = $formModel->getRowId();
  if (!empty($rowid)) {
    $db = FabrikWorker::getDbo();
    $query = $db->getQuery(true);
    $query->update('yourtable)->set('hitcount = COALESCE(hitcount,0) + 1')->where('id = ' . $rowid);
    $db->setQuery($query);
    $db->execute();
  }
}
the code work fine.
my problem : If the user reload the page , the hitcount value will be increase by 1, so the user can increase number of visits 100 , if he reload the page 100 times.
Could you give me hints How do I prevent the user from that ,so that is calculated by a single visit to the user no matter how he reload the page .
thank you
 
You'd have to maintain another table, called something like user_hits, which records hits on form / row / user ...

So create your 'user_hits' list, with id, form_id, row_id, user_id (the last three can all be simple field elements, set to integer)

(don't really need form if you are only doing this for one form, but makes it extendable to other forms)

Code:
if (!$formModel->isEditable()) {
  $rowid = $formModel->getRowId();
  if (!empty($rowid)) {
    $db = FabrikWorker::getDbo();
    $query = $db->getQuery(true);
    // check to see if there's a matching row for form, row and user in user_hits
    $user = JFactory::getUser();
    $userid = $user->get('id');
    $formid = $formModel->getId();
    $query->select('id')->from('user_hits')->where('form_id = ' . $db->quote($formid))->where('row_id = ' . $db->quote($rowid))->where('user_id = ' . $db->quote($userid));
    $db->setQuery($query);
    $hasHit = $db->loadResult();
    // if no hit for this user, record it
    if (empty($hasHit)) {
      // first inc the hit counter in the form's table
      $query->clear()->update('yourtable)->set('hitcount = COALESCE(hitcount,0) + 1')->where('id = ' . $rowid);
      $db->setQuery($query);
      $db->execute();
      // now insert the user hit for this form, row and user
      $query->clear()->insert('user_hits')->set('form_id = '  . $db->quote($formid))->set('row_id = ' . $db->quote($rowid))->set('user_id = ' . $db->quote($userid));
      $db->setQuery($query);
      $db->execute();
    }
  }
}

Obviously replace 123 with you actual numeric formid.

-- hugh
 
Last edited:
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top