JTable/hit: Difference between revisions
From Joomla! Documentation
m clean up |
m preparing for archive only |
||
| Line 4: | Line 4: | ||
===Syntax=== | ===Syntax=== | ||
| Line 68: | Line 68: | ||
===Examples=== | ===Examples=== | ||
=== Code Examples === | |||
<dpl> | <dpl> | ||
noresultsheader=\n | noresultsheader=\n | ||
category=hit | category=hit | ||
category=JTable | category=JTable | ||
namespace=CodeExample | |||
category=MethodExample | category=MethodExample | ||
include=* | include=* | ||
format= ,,, | format= ,,, | ||
</dpl> | </dpl> | ||
Latest revision as of 02:09, 25 March 2017
Description
Method to increment the hits for a row if the necessary property/field exists.
Syntax
hit($pk=null)
| Parameter Name | Default Value | Description |
|---|---|---|
| $pk | null | An optional primary key value to increment. If not set the instance property value is used. |
Returns
boolean True on success.
Defined in
libraries/joomla/database/table.php
Importing
jimport( 'joomla.database.table' );
Source Body
public function hit($pk = null)
{
// If there is no hits field, just return true.
if (!property_exists($this, 'hits')) {
return true;
}
// Initialise variables.
$k = $this->_tbl_key;
$pk = (is_null($pk)) ? $this->$k : $pk;
// If no primary key is given, return false.
if ($pk === null) {
return false;
}
// Check the row in by primary key.
$this->_db->setQuery(
'UPDATE `'.$this->_tbl.'`' .
' SET `hits` = (`hits` + 1)' .
' WHERE `'.$this->_tbl_key.'` = '.$this->_db->quote($pk)
);
$this->_db->query();
// Check for a database error.
if ($this->_db->getErrorNum()) {
$this->setError($this->_db->getErrorMsg());
return false;
}
// Set table values in the object.
$this->hits++;
return true;
}
Examples
Code Examples