Added 'random' as an order_by() option in Active Record.
diff --git a/system/database/DB_active_rec.php b/system/database/DB_active_rec.php
index cb134ea..e8059ab 100644
--- a/system/database/DB_active_rec.php
+++ b/system/database/DB_active_rec.php
@@ -450,7 +450,12 @@
*/
function order_by($orderby, $direction = '')
{
- if (trim($direction) != '')
+ if (strtolower($direction) == 'random')
+ {
+ $orderby = ''; // Random results want or don't need a field name
+ $direction = $this->_random_keyword;
+ }
+ elseif (trim($direction) != '')
{
$direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' '.$direction : ' ASC';
}
@@ -458,6 +463,7 @@
$this->ar_orderby[] = $orderby.$direction;
return $this;
}
+
// --------------------------------------------------------------------
/**
@@ -469,6 +475,7 @@
{
return $this->order_by($orderby, $direction);
}
+
// --------------------------------------------------------------------
/**
@@ -583,7 +590,7 @@
$this->from($table);
}
- $sql = $this->_compile_select($this->count_string);
+ $sql = $this->_compile_select($this->_count_string);
$query = $this->query($sql);
$this->_reset_select();
diff --git a/system/database/drivers/mssql/mssql_driver.php b/system/database/drivers/mssql/mssql_driver.php
index 051dd5b..8961045 100644
--- a/system/database/drivers/mssql/mssql_driver.php
+++ b/system/database/drivers/mssql/mssql_driver.php
@@ -35,7 +35,7 @@
* database engines, so this string appears in each driver and is
* used for the count_all() and count_all_results() functions.
*/
- var $count_string = "SELECT COUNT(*) AS numrows ";
+ var $_count_string = "SELECT COUNT(*) AS numrows ";
/**
* Non-persistent database connection
@@ -280,7 +280,7 @@
if ($table == '')
return '0';
- $query = $this->query($this->count_string . "FROM ".$this->dbprefix.$table);
+ $query = $this->query($this->_count_string . "FROM ".$this->dbprefix.$table);
if ($query->num_rows() == 0)
return '0';
diff --git a/system/database/drivers/mysql/mysql_driver.php b/system/database/drivers/mysql/mysql_driver.php
index 69a238d..2d91c59 100644
--- a/system/database/drivers/mysql/mysql_driver.php
+++ b/system/database/drivers/mysql/mysql_driver.php
@@ -31,18 +31,19 @@
class CI_DB_mysql_driver extends CI_DB {
/**
- * The syntax to count rows is slightly different across different
- * database engines, so this string appears in each driver and is
- * used for the count_all() and count_all_results() functions.
- */
- var $count_string = "SELECT COUNT(*) AS numrows ";
-
- /**
* Whether to use the MySQL "delete hack" which allows the number
* of affected rows to be shown. Uses a preg_replace when enabled,
* adding a bit more processing to all queries.
*/
var $delete_hack = TRUE;
+
+ /**
+ * The syntax to count rows is slightly different across different
+ * database engines, so this string appears in each driver and is
+ * used for the count_all() and count_all_results() functions.
+ */
+ var $_count_string = "SELECT COUNT(*) AS numrows ";
+ var $_random_keyword = ' RAND()'; // database specific random keyword
/**
* Non-persistent database connection
@@ -296,7 +297,7 @@
if ($table == '')
return '0';
- $query = $this->query($this->count_string . "FROM `".$this->dbprefix.$table."`");
+ $query = $this->query($this->_count_string . "FROM `".$this->dbprefix.$table."`");
if ($query->num_rows() == 0)
return '0';
diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php
index 30a256e..0991170 100644
--- a/system/database/drivers/mysqli/mysqli_driver.php
+++ b/system/database/drivers/mysqli/mysqli_driver.php
@@ -35,8 +35,9 @@
* database engines, so this string appears in each driver and is
* used for the count_all() and count_all_results() functions.
*/
- var $count_string = "SELECT COUNT(*) AS numrows ";
-
+ var $_count_string = "SELECT COUNT(*) AS numrows ";
+ var $_random_keyword = ' RAND()'; // database specific random keyword
+
/**
* Whether to use the MySQL "delete hack" which allows the number
* of affected rows to be shown. Uses a preg_replace when enabled,
@@ -289,7 +290,7 @@
if ($table == '')
return '0';
- $query = $this->query($this->count_string . "FROM `".$this->dbprefix.$table."`");
+ $query = $this->query($this->_count_string . "FROM `".$this->dbprefix.$table."`");
if ($query->num_rows() == 0)
return '0';
diff --git a/system/database/drivers/oci8/oci8_driver.php b/system/database/drivers/oci8/oci8_driver.php
index 0cd04cc..e6bcefe 100644
--- a/system/database/drivers/oci8/oci8_driver.php
+++ b/system/database/drivers/oci8/oci8_driver.php
@@ -48,7 +48,7 @@
* database engines, so this string appears in each driver and is
* used for the count_all() and count_all_results() functions.
*/
- var $count_string = "SELECT COUNT(1) AS numrows ";
+ var $_count_string = "SELECT COUNT(1) AS numrows ";
// Set "auto commit" by default
var $_commit = OCI_COMMIT_ON_SUCCESS;
@@ -396,7 +396,7 @@
if ($table == '')
return '0';
- $query = $this->query($this->count_string . "FROM ".$table);
+ $query = $this->query($this->_count_string . "FROM ".$table);
if ($query == FALSE)
{
diff --git a/system/database/drivers/odbc/odbc_driver.php b/system/database/drivers/odbc/odbc_driver.php
index ef26ea1..5e16762 100644
--- a/system/database/drivers/odbc/odbc_driver.php
+++ b/system/database/drivers/odbc/odbc_driver.php
@@ -35,7 +35,8 @@
* database engines, so this string appears in each driver and is
* used for the count_all() and count_all_results() functions.
*/
- var $count_string = "SELECT COUNT(*) AS numrows ";
+ var $_count_string = "SELECT COUNT(*) AS numrows ";
+ var $_random_keyword = ' RND('.time().')'; // database specific random keyword
/**
* Non-persistent database connection
@@ -258,7 +259,7 @@
if ($table == '')
return '0';
- $query = $this->query($this->count_string . "FROM `".$this->dbprefix.$table."`");
+ $query = $this->query($this->_count_string . "FROM `".$this->dbprefix.$table."`");
if ($query->num_rows() == 0)
return '0';
diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php
index beaa793..e54f9cc 100644
--- a/system/database/drivers/postgre/postgre_driver.php
+++ b/system/database/drivers/postgre/postgre_driver.php
@@ -35,7 +35,8 @@
* database engines, so this string appears in each driver and is
* used for the count_all() and count_all_results() functions.
*/
- var $count_string = "SELECT COUNT(*) AS numrows ";
+ var $_count_string = "SELECT COUNT(*) AS numrows ";
+ var $_random_keyword = ' RANDOM()'; // database specific random keyword
/**
* Non-persistent database connection
@@ -285,8 +286,8 @@
if ($table == '')
return '0';
- $query = $this->query($this->count_string .'FROM "'.$this->dbprefix.$table.'"');
-// original query before count_string was used. Kept for reference
+ $query = $this->query($this->_count_string .'FROM "'.$this->dbprefix.$table.'"');
+// original query before _count_string was used. Kept for reference
// $query = $this->query('SELECT COUNT(*) AS numrows FROM "'.$this->dbprefix.$table.'"');
if ($query->num_rows() == 0)
diff --git a/system/database/drivers/sqlite/sqlite_driver.php b/system/database/drivers/sqlite/sqlite_driver.php
index 5f86b8e..aa6738c 100644
--- a/system/database/drivers/sqlite/sqlite_driver.php
+++ b/system/database/drivers/sqlite/sqlite_driver.php
@@ -37,7 +37,8 @@
* database engines, so this string appears in each driver and is
* used for the count_all() and count_all_results() functions.
*/
- var $count_string = "SELECT COUNT(*) AS numrows ";
+ var $_count_string = "SELECT COUNT(*) AS numrows ";
+ var $_random_keyword = ' Random()'; // database specific random keyword
/**
* Non-persistent database connection
@@ -281,7 +282,7 @@
if ($table == '')
return '0';
- $query = $this->query($this->count_string . "FROM `".$this->dbprefix.$table."`");
+ $query = $this->query($this->_count_string . "FROM `".$this->dbprefix.$table."`");
if ($query->num_rows() == 0)
return '0';