Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
PHP-MySQLi-Database-Class
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
PHP-MySQLi-Database-Class
Commits
e5937d76
Commit
e5937d76
authored
Feb 10, 2015
by
Alexander Butenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Order by field support
parent
06f6a372
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
1 deletion
+23
-1
MysqliDb.php
MysqliDb.php
+8
-1
readme.md
readme.md
+7
-0
tests.php
tests.php
+8
-0
No files found.
MysqliDb.php
View file @
e5937d76
...
...
@@ -446,7 +446,7 @@ class MysqliDb
*
* @return MysqliDb
*/
public
function
orderBy
(
$orderByField
,
$orderbyDirection
=
"DESC"
)
public
function
orderBy
(
$orderByField
,
$orderbyDirection
=
"DESC"
,
$customFields
=
null
)
{
$allowedDirection
=
Array
(
"ASC"
,
"DESC"
);
$orderbyDirection
=
strtoupper
(
trim
(
$orderbyDirection
));
...
...
@@ -455,6 +455,13 @@ class MysqliDb
if
(
empty
(
$orderbyDirection
)
||
!
in_array
(
$orderbyDirection
,
$allowedDirection
))
die
(
'Wrong order direction: '
.
$orderbyDirection
);
if
(
is_array
(
$customFields
))
{
foreach
(
$customFields
as
$key
=>
$value
)
$customFields
[
$key
]
=
preg_replace
(
"/[^-a-z0-9\.\(\),_]+/i"
,
''
,
$value
);
$orderByField
=
'FIELD ('
.
$orderByField
.
', "'
.
implode
(
'","'
,
$customFields
)
.
'")'
;
}
$this
->
_orderBy
[
$orderByField
]
=
$orderbyDirection
;
return
$this
;
}
...
...
readme.md
View file @
e5937d76
...
...
@@ -266,6 +266,13 @@ $results = $db->get('users');
// Gives: SELECT * FROM users ORDER BY id ASC,login DESC, RAND ();
```
order by values example:
```
php
$db
->
orderBy
(
'userGroup'
,
'ASC'
,
array
(
'superuser'
,
'admin'
,
'users'
));
$db
->
get
(
'users'
);
// Gives: SELECT * FROM users ORDER BY FIELD (userGroup, 'superuser', 'admin', 'users') ASC;
```
### Grouping method
```
php
$db
->
groupBy
(
"name"
);
...
...
tests.php
View file @
e5937d76
...
...
@@ -149,6 +149,14 @@ if ($db->count != 3) {
exit
;
}
// order by field
$db
->
orderBy
(
"login"
,
"asc"
,
Array
(
"user3"
,
"user2"
,
"user1"
));
$login
=
$db
->
getValue
(
"users"
,
"login"
);
if
(
$login
!=
"user3"
)
{
echo
"order by field test failed"
;
exit
;
}
$db
->
where
(
"active"
,
true
);
$users
=
$db
->
get
(
"users"
);
if
(
$db
->
count
!=
1
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment