From d7c28b72ed76e5c85f2a25470714fe9fbbe06536 Mon Sep 17 00:00:00 2001 From: Shikiryu Date: Tue, 4 Jan 2011 10:57:00 +0000 Subject: [PATCH] Add Statistics --- class/ShortURL.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/class/ShortURL.php b/class/ShortURL.php index d268c9a..390090d 100644 --- a/class/ShortURL.php +++ b/class/ShortURL.php @@ -16,12 +16,13 @@ class ShortURL extends XMLSQL{ if($this->pkAlreadyExists($shortName, 'url')){ return self::STATE_ALREADY_EXIST; }else{ - return $this->insert(array('url'=>$longUrl), rawurlencode($shortName))->into('url')->query(); + return $this->insert(array('url'=>$longUrl,'hit'=>'0'), rawurlencode($shortName))->into('url')->query(); } } public function findThisUrl($shortName){ if($this->pkAlreadyExists(rawurlencode($shortName), 'url')){ + $this->_incrementStatFor($shortName); return $this->select(array('url'))->from('url')->where(rawurlencode($shortName))->query(); }else{ return; @@ -32,4 +33,14 @@ class ShortURL extends XMLSQL{ return $this->select()->from('url')->query(); } + + /** + * Considering the table with $shortname already exist + */ + private function _incrementStatFor($shortName){ + $currentHit = $this->select(array('hit'))->from('url')->where(rawurlencode($shortName))->query(); + $currentHit = $currentHit[0]; + return $this->update('url')->set(array('hit'=>$currentHit+1))->where(rawurlencode($shortName))->query(); + } + } \ No newline at end of file