diff --git a/examples/all.php b/examples/all.php index 81a24b1..7f848f9 100644 --- a/examples/all.php +++ b/examples/all.php @@ -44,6 +44,11 @@ ->groupby('column_name') ->run(); +//random +$db->from('table_name') + ->rand() + ->run(); + // limit $db->from('table_name') ->limit(0, 20) diff --git a/src/BasicDB.php b/src/BasicDB.php index 238a290..8a859e8 100644 --- a/src/BasicDB.php +++ b/src/BasicDB.php @@ -49,6 +49,13 @@ class BasicDB extends \PDO * @var * */ + private $rand; + /** + * OrderBy Rand() + * + * @var + * + */ private $orderBy; /** * GroupBy Value @@ -201,6 +208,20 @@ public function join($targetTable, $joinSql, $joinType = 'inner') $this->join[] = ' ' . strtoupper($joinType) . ' JOIN ' . $targetTable . ' ON ' . sprintf($joinSql, $targetTable, $this->tableName); return $this; } + + /** + * Defines random operation in sql query + * + * @param + * $limit + * Default + * 5 + * @return $this + */ + public function rand($limit = 5){ + $this->rand = ' ORDER BY rand() LIMIT ' .$limit; + return $this; + } /** * Defines -orderby- operation in sql query @@ -276,6 +297,10 @@ public function generateQuery() $this->sql .= $this->orderBy; $this->orderBy = null; } + if ($this->rand) { + $this->sql .= $this->rand; + $this->orderBy = null; + } if ($this->limit) { $this->sql .= $this->limit; $this->limit = null;