6
6
* @author phptricks Team - Mohammad Anzawi
7
7
* @author_uri https://phptricks.org
8
8
* @uri https://github.com/anzawi/php-database-class
9
- * @version 3.1 .0
9
+ * @version 3.2 .0
10
10
* @licence MIT -> https://opensource.org/licenses/MIT
11
11
* @package PHPtricks\Database
12
12
*/
13
13
14
- namespace PHPtricks \Database ;
15
14
16
15
// include config() function file
17
16
include __DIR__ . "/config_function.php " ;
@@ -62,8 +61,9 @@ class Database implements \IteratorAggregate, \ArrayAccess
62
61
/**
63
62
* @var $_newValues null to save new value to use save() method
64
63
*/
65
- $ _newValues = null ;
66
-
64
+ $ _newValues = null ,
65
+
66
+ $ _ordering = false ;
67
67
68
68
protected
69
69
/**
@@ -264,6 +264,7 @@ public static function connect()
264
264
*/
265
265
public function query ($ sql , $ params = [])
266
266
{
267
+ echo $ sql ;
267
268
$ this ->_query = "" ;
268
269
$ this ->_where = "WHERE " ;
269
270
// 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()
423
424
* @param array $fields fields we need to select
424
425
* @return Database result of select as Database object
425
426
*/
426
- public function select ($ fields = ['* ' ])
427
+ public function select ($ fields = ['* ' ], $ last = false )
427
428
{
429
+ if ($ fields === true )
430
+ {
431
+ $ fields = ['* ' ];
432
+ $ last = true ;
433
+ }
428
434
if ($ fields != ['* ' ] && !is_null ($ this ->_idColumn ))
429
435
{
430
436
if (!in_array ($ this ->_idColumn , $ fields ))
@@ -433,10 +439,23 @@ public function select($fields = ['*'])
433
439
}
434
440
}
435
441
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
+
438
456
439
457
$ this ->_query = $ sql ;
458
+ $ this ->_ordering = false ;
440
459
441
460
return $ this ->collection ([
442
461
'results ' => $ this ->query ($ sql )->results (),
@@ -636,6 +655,13 @@ public function notIn($field, $values = [])
636
655
return $ this ;
637
656
}
638
657
658
+ public function orderBy ($ colName , $ type = 'ASC ' )
659
+ {
660
+ $ this ->_query .= " ORDER BY {$ colName } {$ type }" ;
661
+ $ this ->_ordering = true ;
662
+ return $ this ;
663
+ }
664
+
639
665
/**
640
666
* get first row from query results
641
667
* @return Database
@@ -679,10 +705,17 @@ public function firstRecord()
679
705
* @param int $to
680
706
* @return $this
681
707
*/
682
- public function limit ($ from = 0 , $ to = 15 )
708
+ public function limit ($ from = 0 , $ to = 0 )
683
709
{
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
+
686
719
return $ this ;
687
720
}
688
721
@@ -734,15 +767,6 @@ public function results()
734
767
}
735
768
736
769
737
- /**
738
- * Show last query
739
- * @return string
740
- */
741
- public function showMeQuery ()
742
- {
743
- return $ this ->_sql ;
744
- }
745
-
746
770
/**
747
771
*
748
772
* New In V.2.1.0
@@ -766,8 +790,14 @@ public function showMeQuery()
766
790
* now add to url this string query (?page=2 or 3 or 4 .. etc)
767
791
* see (link() method to know how to generate navigation automatically)
768
792
*/
769
- public function paginate ($ recordsCount = 0 )
793
+ public function paginate ($ recordsCount = 0 , $ last = false )
770
794
{
795
+ if ($ recordsCount === true )
796
+ {
797
+ $ last = true ;
798
+ $ recordsCount = 0 ;
799
+ }
800
+
771
801
if ($ recordsCount === 0 )
772
802
$ recordsCount = config ("pagination.records_per_page " );
773
803
@@ -787,7 +817,7 @@ public function paginate($recordsCount = 0)
787
817
$ this ->_colsCount = ceil (count ($ this ->select ()->results ()) / $ recordsCount );
788
818
789
819
// return query results
790
- return $ this ->limit ($ startFrom , $ recordsCount )->select ();
820
+ return $ this ->limit ($ startFrom , $ recordsCount )->select ([ ' * ' ], $ last );
791
821
}
792
822
793
823
/**
@@ -1350,11 +1380,25 @@ public function first()
1350
1380
return isset ($ this ->_results [0 ]) ? $ this ->_results [0 ] : null ;
1351
1381
}
1352
1382
1353
- public function last ()
1383
+ public function last ($ count = 0 )
1354
1384
{
1355
1385
$ reverse = array_reverse ($ this ->results ());
1356
1386
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 ;
1358
1402
}
1359
1403
1360
1404
public function each (callable $ callback )
0 commit comments