Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4b4a8e4

Browse files
refactor: Updated many code blocks. Also were fixed some issues due insert, update and delete
1 parent 5a60fc5 commit 4b4a8e4

File tree

3 files changed

+160
-13
lines changed

3 files changed

+160
-13
lines changed

‎README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,111 @@
11
# PHP Database Class
2+
23
PHP Class that handles database Objects and Requests.
4+
5+
It's the result of quite some time of studying the PDO class and the new PHP 7.4 version, along with OO in PHP.
6+
7+
Let's go for the methods.
8+
9+
## addConnection
10+
11+
Method to define a database connection
12+
13+
## useConnection
14+
15+
This function will set which connection the class should use at the moment
16+
17+
## getTotalRequests
18+
19+
It will return the total amount number of requests that were done to the database
20+
21+
## performance
22+
23+
It will display a HTML comment on the page, displaying the total amount of time in unix the requisitions had taken
24+
25+
## fetch
26+
27+
Will fetch a row. If $simple then only one row and finishes.
28+
29+
## fetchAll
30+
31+
Will give all results
32+
33+
## count
34+
35+
Will give the number of rows inside the object
36+
37+
## empty
38+
39+
If the object is empty, returns true
40+
41+
## query
42+
43+
Performns a query
44+
45+
## pagedQuery
46+
47+
Breaks the number of results to be shown in a smallest number
48+
49+
## setLanguage
50+
51+
Define which language the class should work (default: english)
52+
53+
## setPaginationWords
54+
55+
Defines the words inside the pagination HTML when the page method is evoked
56+
57+
## getCurrentPage
58+
59+
Will return the current page according to URL
60+
61+
## page
62+
63+
Will give the pagination HTML
64+
65+
## date
66+
67+
Will give server date
68+
69+
## datetime
70+
71+
Will give server datetime
72+
73+
## setCollation
74+
75+
Will generate a SQL to change whole database collation. Returns a string, and can apply it too.
76+
77+
## setFriendlyURL
78+
79+
Will stablish the FriendlyURL instance
80+
81+
## prepare
82+
83+
Will prepare a SQL
84+
85+
## set
86+
87+
Will bind value to a prepared SQL
88+
89+
## formatMonney
90+
91+
Will make the value a monetary one
92+
93+
## insert
94+
95+
Will insert $data inside $table. If $additional is given, will transform values
96+
97+
## id
98+
99+
Returns last inserted id
100+
101+
## update
102+
103+
Updates $data in $table using given $rules. $additional is supported too.
104+
105+
## delete
106+
107+
Will delete whole table if $rules is omitted
108+
109+
## URLNormalize
110+
111+
Makes given string url friendly

‎db.class.php

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/**
44
* PHP DB Class
55
* 2021年01月07日 -> Class created
6-
* 2021年03月12日 -> Added rel="canoical|prev|next" to <a> pagination tags
6+
* 2021年03月12日 -> Added rel="canonical|prev|next" to <a> pagination tags
77
* 2021年03月12日 -> Added a lot of comments
8+
* 2021年06月05日 -> Added transformWith private method for insert and update methods using the $additional array. Added formatMonney public method to work with monetary values. Changed getPageNow to getCurrentPage
89
*
9-
* MUST FIX: delete(), $rules inside insert, define $functions on update and insert
1010
*
1111
* MUST CREATE: search method, wordlist method, similarwords method, searchSimilarWord method
1212
*/
@@ -54,7 +54,7 @@ class db
5454
);
5555

5656
/** Will return a database instance. It also sets manny vars about the connection, if isn't yet defined */
57-
public static function getInstance()
57+
private static function getInstance()
5858
{
5959
if (self::$dbInit == null) {
6060
self::$dbInit = microtime();
@@ -220,7 +220,7 @@ public static function pagedQuery($mixed, $limit, $page = false, $words = false)
220220
$object->extra['limit'] = $limit;
221221
self::setPaginationWords($object, $words);
222222
if ($page == false) {
223-
$page = self::getPageNow();
223+
$page = self::getCurrentPage();
224224
}
225225
$newObject = self::query($mixed . " LIMIT " . (($limit * $page) - $limit) . ", " . $limit);
226226
return $newObject;
@@ -229,7 +229,7 @@ public static function pagedQuery($mixed, $limit, $page = false, $words = false)
229229
$totalRows = $mixed->extra['totalEntries'];
230230
$object = new dbObject($mixed->getInstance(), array("limit" => $limit, 'totalEntries' => $totalRows));
231231
self::setPaginationWords($object, $words);
232-
$page = self::getPageNow();
232+
$page = self::getCurrentPage();
233233
if ($totalRows > $limit) {
234234
$newObject = self::query($mixed->getInstance()->queryString . " LIMIT " . (($limit * $page) - $limit) . ", " . $limit);
235235
return $newObject;
@@ -244,7 +244,7 @@ public static function pagedQuery($mixed, $limit, $page = false, $words = false)
244244
self::updateTotalRequests();
245245
$object = new dbObject($mixed, array("limit" => $limit));
246246
self::setPaginationWords($object, $words);
247-
$page = self::getPageNow();
247+
$page = self::getCurrentPage();
248248
if ($totalRows > $limit) {
249249
$newObject = self::query($mixed->queryString . " LIMIT " . (($limit * $page) - $limit) . ", " . $limit);
250250
return $newObject;
@@ -283,7 +283,7 @@ public static function setPaginationWords($object, $words = false)
283283
}
284284

285285
/** It retrieves the current page */
286-
private static function getPageNow($object = false)
286+
private static function getCurrentPage($object = false)
287287
{
288288
if (!$object) {
289289
$object = self::$pageObject;
@@ -327,7 +327,7 @@ public static function page($echo = true, $class = "")
327327
}
328328
$words = self::$pageObject->extra['words'];
329329
$totalPages = ceil($totalRows / $limit);
330-
$pageNow = self::getPageNow(self::$pageObject);
330+
$pageNow = self::getCurrentPage(self::$pageObject);
331331
if ($class) {
332332
$words['class'] . "" . $class . "";
333333
}
@@ -530,19 +530,47 @@ public static function execute($sql)
530530
return self::getInstance()->exec($sql);
531531
}
532532

533-
/** WORK IN PROGRESS - It will insert $data on a specific $table. The $rules are optional */
534-
public static function insert($data, $table, $rules = array())
533+
/** It will apply the additional steps before the real method applyment */
534+
private static function transformWith(&$data, $additional)
535+
{
536+
if (isset($additional['function'])) {
537+
foreach ($additional['function'] as $fieldKey => $function) {
538+
if (isset($data[$fieldKey])) {
539+
$data[$fieldKey] = call_user_func($function, $data[$fieldKey]);
540+
}
541+
}
542+
}
543+
}
544+
545+
/** This function is responsible to make the given value a decimal value for monetary operations */
546+
/** It looks like the best option to save monney on a database is to use the DECIMAL 19,4 */
547+
public static function formatMonney($value)
548+
{
549+
$source = array('.', ',');
550+
$replace = array('', '.');
551+
$value = str_replace($source, $replace, $value);
552+
return $value;
553+
}
554+
555+
556+
/** It will insert $data on a specific $table. The $rules are optional */
557+
public static function insert($data, $table, $additional = array())
535558
{
536559
self::fixDataCollumns($data, $table, $newData);
537-
$sql = "INSERT INTO $table (" . implode(", ", array_keys($newData)) . ") VALUES (:" . implode(", :", array_keys($newData)) . ");";
560+
$array_keys = array_keys($newData);
561+
$sql = "INSERT INTO $table (" . implode(", ", $array_keys) . ") VALUES (:" . implode(", :", $array_keys) . ");";
538562
$object = self::getInstance();
539563
$stmnt = self::prepare($object, $sql);
564+
if (!empty($additional)) {
565+
self::transformWith($newData, $additional);
566+
}
540567
foreach ($newData as $key => $value) {
541568
self::set($stmnt, $key, $value);
542569
}
543570
if ($stmnt->execute()) {
544571
self::updateTotalRequests();
545572
self::$id = $object->lastInsertId();
573+
unset($stmnt, $object);
546574
return true;
547575
} else {
548576
return false;
@@ -556,7 +584,7 @@ public static function id()
556584
}
557585

558586
/** It will update a row on a specific $table with new $data. The row must attend to the $rules */
559-
public static function update($data, $table, $rules = array())
587+
public static function update($data, $table, $rules = array(), $additional = array())
560588
{
561589
self::fixDataCollumns($data, $table, $newData, "update");
562590
$collumns = array();
@@ -572,6 +600,9 @@ public static function update($data, $table, $rules = array())
572600
$sql = "UPDATE $table SET " . implode(", ", $collumns) . (!empty($rules) ? " WHERE " . implode(" AND ", $newRules) : "") . ";";
573601
$object = self::getInstance();
574602
$stmnt = self::prepare($object, $sql);
603+
if (!empty($additional)) {
604+
self::transformWith($newData, $additional);
605+
}
575606
foreach ($newData as $key => $value) {
576607
self::set($stmnt, $key, $value);
577608
}
@@ -588,7 +619,7 @@ public static function update($data, $table, $rules = array())
588619
}
589620
}
590621

591-
/** WORK IN PROGRESS - It will delete a record - or all - on a specific $table */
622+
/** It will delete a record - or all - on a specific $table */
592623
public static function delete($table, $rules = array())
593624
{
594625
if (!empty($rules)) {

‎index.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
include("db.class.php");
4+
5+
db::addConnection('default', array("HOST" => "localhost", "USER" => "root", "PASSWORD" => "", "NAME" => "test"));
6+
7+
db::insert(array("monney" => "1,00.50"), "monney");

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /