]> git.nbdom.net Git - nb.git/commitdiff
dbq
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 23 Dec 2016 10:27:11 +0000 (10:27 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 23 Dec 2016 10:27:11 +0000 (10:27 +0000)
lib/php/db.php
lib/php/db/table.php

index 94088410bea8cc83d5afbf6a0a0fc16aafafe4fc..2f12ed4cbf989ac6b1d4ccf6e7a80477ec3f8a1d 100644 (file)
@@ -121,7 +121,8 @@ class Db extends nb {
   public $extras;
   public $format = 'json';
   public $formats = [ 'table','div','csv','xml','json','yaml','sh','sql' ];
-  public $limits = ['10','20','50','100','500','1000'];
+  public $limits = ['20','50','100','500','1000'];
+  public $limit;
 
   # Classes
   public $out;
@@ -171,6 +172,21 @@ class Db extends nb {
     # Pdo
     $this->connect_init();
 
+    # Params
+    /*
+    if (empty($this->limit) and !empty($this->limits)) {
+      $this->limit = $this->limits[0];
+    }
+    if (empty($this->limit)) {
+      if ($this->p('paged')) {
+        $this->limit = ($this->limit * $this->p('paged')).','.$this->limit;
+      } elseif ($this->limit=$this->p('limit')) {
+      } else {
+        $this->limit = $this->limits[0];
+      }
+    }
+    */
+
     # Extras must disapear - NB 29.03.16
     if (!empty($this->extras)) self::bye($this->extras);
     return true;
@@ -560,9 +576,9 @@ class Db extends nb {
     }
 
     // Handle limit
-    if ($this->p('limit')) {
+    if ($this->limit) {
 
-      $limit = (int)$this->p('limit');
+      $limit = (int)$this->limit;
       $i = empty($head) ? 1 : 0;
 
       foreach ($rows as $k=>$v) {
index 3c153d15f629d917fba59921fa90d5f07de3e103..ddbd44fdf5f246714a5a1220c9295f36cf64553c 100644 (file)
@@ -42,7 +42,8 @@ Class Table extends nb {
   public $show_url_sort = true;
   public $show_header = true;
   public static $params = [ 'db', 'table', 'limit', 'debug', 'action'
-    , 'page', 'paged' # wordpress
+    # wordpress
+    , 'page', 'paged'
   ];
 
   function __construct($name,$opt=[]) {
@@ -94,6 +95,13 @@ Class Table extends nb {
     // Add others
     foreach ($opt as $k => $v) { $this->$k = $v; }
 
+    if (empty($this->db()->limit)) {
+      if ($this->p('paged')) {
+        $this->db()->limit = ($this->db()->limit * $this->p('paged')).','.$this->db()->limit;
+      } elseif ($this->db()->limit=$this->p('limit')) {
+      }
+    }
+
     # NB 22.12.16: TODEL 
     $this->show_header =
       (bool)$this->p('header',$this->show_header)
@@ -500,7 +508,6 @@ Class Table extends nb {
 
     }
 
-# NB 03.04.16     if ($this->p('db')) $params['db'] = $this->p('db');
     $flat = [];
     foreach ($params as $k=>$v) { $flat[] = $k.'='.urlencode($v); }
     return $flat ? '?'. join('&amp;',$flat) : ''; 
@@ -753,9 +760,6 @@ Class Table extends nb {
 
     if (isset($this->orderby)) self::pdef('orderby',$this->orderby);
     if (self::p('order')) self::pset('orderby',self::p('orderby').' '.self::p('order')); # from Wordpress
-    if (self::p('paged')) {
-      self::pset( 'limit', (self::p('limit') * self::p('paged')).','.self::p('limit') );
-    }
 
     //
     // Select
@@ -785,8 +789,7 @@ Class Table extends nb {
 
       if ($this->p('orderby')) $sql .= ' ORDER BY '.$this->p('orderby');
 
-      if ($this->p('limit')) {
-        $limit = $this->p('limit');
+      if ($limit = $this->db()->limit) {
         $sql .= ' LIMIT '.$limit;
       } else {
         $limit = '';
@@ -797,7 +800,6 @@ Class Table extends nb {
     //
     // Get results
     //
-# NB 28.03.16     $this->sql = $sql;
     $this->debug(preg_replace('/[\r\n]+[ ]*/',' ',$sql),1);
 # NB 03.09.16     $this->debug(preg_replace('/(,|FROM|WHERE|HAVING|GROUP|ORDER)/i',"\n\\1",$sql),1);
     return [$sql,$where,$limit,$select_count];
@@ -885,6 +887,9 @@ Class Table extends nb {
   }
 
   public function rows(&$opt=[],$opt_by_val=null) {
+    if (empty($this->db()->limit)) {
+      $this->db()->limit = $this->db()->limits[0];
+    }
 
     #
     # Run query