66 * @author phptricks Team - Mohammad Anzawi
77 * @author_uri https://phptricks.org
88 * @uri https://github.com/anzawi/php-database-class
9- * @version 3.1 .0
9+ * @version 3.2 .0
1010 * @licence MIT -> https://opensource.org/licenses/MIT
1111 * @package PHPtricks\Database
1212 */
1313
14- namespace PHPtricks \Database ;
1514
1615// include config() function file
1716include __DIR__ . "/config_function.php " ;
@@ -62,8 +61,9 @@ class Database implements \IteratorAggregate, \ArrayAccess
6261 /**
6362 * @var $_newValues null to save new value to use save() method
6463 */
65- $ _newValues = null ;
66-
64+ $ _newValues = null ,
65+
66+ $ _ordering = false ;
6767
6868 protected
6969 /**
@@ -264,6 +264,7 @@ public static function connect()
264264 */
265265 public function query ($ sql , $ params = [])
266266 {
267+ echo $ sql ;
267268 $ this ->_query = "" ;
268269 $ this ->_where = "WHERE " ;
269270 // set _error. true to that if they can not be false for this function to work properly, this function makes the
@@ -423,8 +424,13 @@ public function save()
423424 * @param array $fields fields we need to select
424425 * @return Database result of select as Database object
425426 */
426- public function select ($ fields = ['* ' ])
427+ public function select ($ fields = ['* ' ], $ last = false )
427428 {
429+ if ($ fields === true )
430+ {
431+ $ fields = ['* ' ];
432+ $ last = true ;
433+ }
428434 if ($ fields != ['* ' ] && !is_null ($ this ->_idColumn ))
429435 {
430436 if (!in_array ($ this ->_idColumn , $ fields ))
@@ -433,10 +439,23 @@ public function select($fields = ['*'])
433439 }
434440 }
435441
436- $ sql = "SELECT " . implode (', ' , $ fields )
437- . " FROM {$ this ->_table } {$ this ->_query }" ;
442+ if (!$ last )
443+ $ sql = "SELECT " . implode (', ' , $ fields )
444+ . " FROM {$ this ->_table } {$ this ->_query }" ;
445+ else
446+ {
447+ //$this->_query .= ($this->_ordering == false ? " ORDER BY {$this->_idColumn} DESC" : '');
448+ $ sql = "SELECT * FROM (
449+ SELECT " . implode (', ' , $ fields ) . "
450+ FROM {$ this ->_table }
451+
452+ {$ this ->_query }
453+ ) sub ORDER by id ASC " ;
454+ }
455+
438456
439457 $ this ->_query = $ sql ;
458+ $ this ->_ordering = false ;
440459
441460 return $ this ->collection ([
442461 'results ' => $ this ->query ($ sql )->results (),
@@ -636,6 +655,13 @@ public function notIn($field, $values = [])
636655 return $ this ;
637656 }
638657
658+ public function orderBy ($ colName , $ type = 'ASC ' )
659+ {
660+ $ this ->_query .= " ORDER BY {$ colName } {$ type }" ;
661+ $ this ->_ordering = true ;
662+ return $ this ;
663+ }
664+
639665 /**
640666 * get first row from query results
641667 * @return Database
@@ -679,10 +705,17 @@ public function firstRecord()
679705 * @param int $to
680706 * @return $this
681707 */
682- public function limit ($ from = 0 , $ to = 15 )
708+ public function limit ($ from = 0 , $ to = 0 )
683709 {
684- if (is_integer ($ from ) && is_integer ($ to ))
685- $ this ->_query .= " LIMIT {$ from }, {$ to }" ;
710+ if (!$ to )
711+ {
712+ $ this ->_query .= " LIMIT {$ from }" ;
713+ }
714+ else
715+ {
716+ $ this ->_query .= " LIMIT {$ from }, {$ to }" ;
717+ }
718+
686719 return $ this ;
687720 }
688721
@@ -734,15 +767,6 @@ public function results()
734767 }
735768
736769
737- /**
738- * Show last query
739- * @return string
740- */
741- public function showMeQuery ()
742- {
743- return $ this ->_sql ;
744- }
745-
746770 /**
747771 *
748772 * New In V.2.1.0
@@ -766,8 +790,14 @@ public function showMeQuery()
766790 * now add to url this string query (?page=2 or 3 or 4 .. etc)
767791 * see (link() method to know how to generate navigation automatically)
768792 */
769- public function paginate ($ recordsCount = 0 )
793+ public function paginate ($ recordsCount = 0 , $ last = false )
770794 {
795+ if ($ recordsCount === true )
796+ {
797+ $ last = true ;
798+ $ recordsCount = 0 ;
799+ }
800+
771801 if ($ recordsCount === 0 )
772802 $ recordsCount = config ("pagination.records_per_page " );
773803
@@ -787,7 +817,7 @@ public function paginate($recordsCount = 0)
787817 $ this ->_colsCount = ceil (count ($ this ->select ()->results ()) / $ recordsCount );
788818
789819 // return query results
790- return $ this ->limit ($ startFrom , $ recordsCount )->select ();
820+ return $ this ->limit ($ startFrom , $ recordsCount )->select ([ ' * ' ], $ last );
791821 }
792822
793823 /**
@@ -1350,11 +1380,25 @@ public function first()
13501380 return isset ($ this ->_results [0 ]) ? $ this ->_results [0 ] : null ;
13511381 }
13521382
1353- public function last ()
1383+ public function last ($ count = 0 )
13541384 {
13551385 $ reverse = array_reverse ($ this ->results ());
13561386
1357- return isset ($ reverse [0 ]) ? $ reverse [0 ] : null ;
1387+ if (!$ count )
1388+ {
1389+ return isset ($ reverse [0 ]) ? $ reverse [0 ] : null ;
1390+ }
1391+
1392+ $ lastRecords = [];
1393+ $ j = 0 ;
1394+
1395+ for ($ i = 0 ; $ i < $ count ; $ i ++)
1396+ {
1397+ $ lastRecords [$ j ] = $ reverse [$ i ];
1398+ $ j ++;
1399+ }
1400+
1401+ return $ lastRecords ;
13581402 }
13591403
13601404 public function each (callable $ callback )
0 commit comments