]> git.mjollnir.org Git - moodle.git/commitdiff
Extended get_records so it can deal with LIMITs now
authormoodler <moodler>
Thu, 31 Jul 2003 12:18:22 +0000 (12:18 +0000)
committermoodler <moodler>
Thu, 31 Jul 2003 12:18:22 +0000 (12:18 +0000)
lib/datalib.php

index cd7917dc6bf264c185c4766e23a00e5e40b38230..41a770a0ddd48de25cbcb71ab6781b2ee63c5af3 100644 (file)
@@ -358,11 +358,12 @@ function get_record_select($table, $select="", $fields="*") {
 }
 
 
-function get_records($table, $field="", $value="", $sort="", $fields="*") {
+function get_records($table, $field="", $value="", $sort="", $fields="*", $limitfrom="", $limitnum="") {
 /// Get a number of records as an array of objects
 /// Can optionally be sorted eg "time ASC" or "time DESC"
 /// If "fields" is specified, only those fields are returned
 /// The "key" is the first column returned, eg usually "id"
+/// limitfrom and limitnum must both be specified or not at all
 
     global $CFG;
 
@@ -372,11 +373,26 @@ function get_records($table, $field="", $value="", $sort="", $fields="*") {
         $select = "";
     }
 
+    if ($limitfrom) {
+        switch ($CFG->dbtype) {
+            case "mysql":
+                 $limit = "LIMIT $limitfrom,$limitnum";
+                 break;
+            case "postgres7":
+                 $limit = "LIMIT $limitnum OFFSET $limitfrom";
+                 break;
+            default: 
+                 $limit = "LIMIT $limitnum,$limitfrom";
+        }
+    } else {
+        $limit = "";
+    }
+
     if ($sort) {
         $sort = "ORDER BY $sort";
     }
 
-    return get_records_sql("SELECT $fields FROM $CFG->prefix$table $select $sort");
+    return get_records_sql("SELECT $fields FROM $CFG->prefix$table $select $sort $limit");
 }
 
 function get_records_select($table, $select="", $sort="", $fields="*") {