From 8cae5831a936688889df86c91ce2f4c766c790f7 Mon Sep 17 00:00:00 2001 From: shish Date: Mon, 19 May 2008 03:17:31 +0000 Subject: [PATCH] random image function, at last git-svn-id: file:///home/shish/svn/shimmie2/trunk@863 7f39781d-f577-437e-ae19-be835c7a54ca --- core/database.class.php | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/core/database.class.php b/core/database.class.php index 9450eb63..2c2aeabb 100644 --- a/core/database.class.php +++ b/core/database.class.php @@ -123,19 +123,23 @@ class Database { } // }}} // misc {{{ - public function count_pages($tags=array()) { - global $config; - $images_per_page = $config->get_int('index_width') * $config->get_int('index_height'); + public function count_images($tags=array()) { if(count($tags) == 0) { - return ceil($this->db->GetOne("SELECT COUNT(*) FROM images") / $images_per_page); + return $this->db->GetOne("SELECT COUNT(*) FROM images"); } else { $querylet = $this->build_search_querylet($tags); $result = $this->execute($querylet->sql, $querylet->variables); - return ceil($result->RecordCount() / $images_per_page); + return $result->RecordCount(); } } + public function count_pages($tags=array()) { + global $config; + $images_per_page = $config->get_int('index_width') * $config->get_int('index_height'); + return ceil($thi->count_images($tags) / $images_per_page); + } + public function execute($query, $args=array()) { $result = $this->db->Execute($query, $args); if($result === False) { @@ -395,6 +399,14 @@ class Database { return ($row ? new Image($row) : null); } + public function get_random_image($tags=array()) { + $max = $this->count_images($tags); + $rand = mt_rand(0, $max); + $set = $this->get_images($rand, 1, $tags); + if(count($set) > 0) return $set[0]; + else return null; + } + public function get_image_by_hash($hash) { $image = null; $row = $this->db->GetRow("{$this->get_images} WHERE hash=?", array($hash));