From 1a2183e553b7fd79d70481a219cebc9c182b0f92 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Fri, 23 Dec 2016 10:27:11 +0000 Subject: [PATCH] dbq --- lib/php/db.php | 22 +++++++++++++++++++--- lib/php/db/table.php | 21 +++++++++++++-------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/lib/php/db.php b/lib/php/db.php index 94088410..2f12ed4c 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -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) { diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 3c153d15..ddbd44fd 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -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('&',$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 -- 2.47.3