From cdee5e65701330e0b740640f471228d8b9b02a91 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 16 Feb 2015 15:18:24 +0100 Subject: much better fix for #1082 --- inc/poche/Database.class.php | 12 +++++++----- inc/poche/Poche.class.php | 11 +++++------ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/inc/poche/Database.class.php b/inc/poche/Database.class.php index 210ebb74..65675afe 100755 --- a/inc/poche/Database.class.php +++ b/inc/poche/Database.class.php @@ -411,12 +411,14 @@ class Database { return $count; } - public function getRandomId($row, $user_id) { - $sql = "SELECT id FROM entries WHERE user_id=? LIMIT 1 OFFSET ? "; - $params = array($user_id, $row); - $query = $this->executeQuery($sql, $params); + public function getRandomId($user_id) { + $random = (STORAGE == 'mysql') ? 'RAND()' : 'RANDOM()'; + $sql = "SELECT id FROM entries WHERE user_id=? ORDER BY ". $random . " LIMIT 1"; + $params = array($user_id); + $query = $this->executeQuery($sql, $params); + $id = $query->fetchAll(); - return $query->fetchAll(); + return $id; } diff --git a/inc/poche/Poche.class.php b/inc/poche/Poche.class.php index 4b85d52f..481382ec 100755 --- a/inc/poche/Poche.class.php +++ b/inc/poche/Poche.class.php @@ -393,13 +393,12 @@ class Poche /* For some unknown reason I can't get displayView() to work here (it redirects to home view afterwards). So here's a dirty fix which redirects directly to URL */ case 'random': - $count = $this->store->getEntriesByViewCount($view, $this->user->getId()); - $id_query = $this->store->getRandomId(rand(1,$count)-1, $this->user->getId()); - $id = $id_query[0]; Tools::logm('get a random article'); - Tools::redirect('?view=view&id=' . $id[0]); - - //$this->displayView('view', $id); + if ($this->store->getRandomId($this->user->getId())) { + $id = $this->store->getRandomId($this->user->getId())[0]; + Tools::redirect('?view=view&id=' . $id[0]); + Tools::logm('got the article with id ' . $id[0]); + } break; default: break; -- cgit v1.2.3