1. Go to this page and download the library: Download claytonkreisel/wpdb-tools library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
claytonkreisel / wpdb-tools example snippets
use WPDBTools\CustomTable\Table;
class Your_Table_Name extends Table{
/*
This version number is stored in the wp_options table and tells
WordPress when to check for updated structure. Be sure to pass a
string with proper versioning xx.xx.xx.
*/
public function version(){
return "0.0.1";
}
/*
Return a name for the table. This will be the name of the table
after the WordPress prefix is added. IE "test_name" becomes a table
with the name "wp_test_name" in the WordPress database.
*/
public function name(){
return 'your_table_name';
}
/*
Return an associative array of columns that defines the table structure in
the database. The formatting for this is extremely important and it will
rray you wish to define as the primary key for your database table.
This will typically be the first column (IE "id").
*/
public function primary_key(){
return "id";
}
}
/*
Initiates the new table and performs a check on the database in order to update
structure if needed.
*/
$your_new_table = new Your_Table_Name();
/*
This method inserts one new row into the database.
@params $data(array)[
'varchar_column' => 'Short Value',
'boolean_column' => true,
'text_column' => '',
'longtext_column' => 'This is a considerable amount of text that will go into the database',
'datetime_column' => '2021-07-01 10:30:00'
//Not all columns are
/*
This method inserts multiple new rows into the database.
@params $data(array)[' => 30,
'varchar_column' => 'Short Value',
'boolean_column' => true,
'text_column' => '',
'longtext_column' => 'This is a considerable amount of text that will go into the database',
'datetime_column' => '2021-07-01 10:30:00'
],
[
'int_column' => 55,
'varchar_column' => 'Some other value',
'boolean_column' => false,
'text_column' => 'This one has some text here',
'longtext_column' => '',
'datetime_column' => '2021-07-04 16:30:00'
]
]);
/* Simply select all rows in the table */
$your_new_table->select_all();
//Output -> SELECT * FROM your_new_table;
//Returns-> All rows from the table
/* Simple select with string for WHERE clause */
$your_new_table->select([
'id' => 20
]);
//Output -> SELECT * FROM your_new_table WHERE `id` = 20;
//Returns -> Row with an ID of 20
/* Simple select with multiple WHERE clauses with AND operator */
$your_new_table->select([
'varchar_column' => 'Short Value',
'boolean_column' => true,
]);
//Output -> SELECT * FROM your_new_table WHERE `varchar_column` = "Short Value" AND `boolean_column` = 1;
//Return -> Any row that has a varchar_column of "Short Value" and boolean_column of true
/* Simple select with multiple WHERE clauses with OR operator */
$your_new_table->select_or([
'varchar_column' => 'Short Value',
'boolean_column' => true,
]);
//Output -> SELECT * FROM your_new_table WHERE `varchar_column` = "Short Value" OR `boolean_column` = 1;
//Return -> Any row that has a varchar_column of "Short Value" or boolean_column of true
/* Complex AND select using operators other than = (this works with select_or as well) */
/*
Provide an array of arrays that will build the WHERE clause of your query
@params key(where the datetime_column is less than January 1, 2020 at noon and where the longtext_column contains the string "some text" anywhere in the cell and where text_column begins with "another text" regarless of what comes after it in the cell.
/* Simply select all with ORDER clause */
$your_new_table->select_all([
'orderby' => 'varchar_column',
'order' => 'ASC',
]);
//Output -> SELECT * FROM your_new_table ORDER BY `varchar_column` ASC;
//Return -> Returns all rows sorted by the varchar_column in ASC (1-9,A-Z) order.
/* Simple select with multiple ORDER clauses */
$where = [
'boolean_column' => false
];
$order = [
[
'orderby' => 'datetime_column',
'order' => 'DESC'
],
[
'orderby' => 'varchar_column',
'order' => 'ASC',
'is_numeric' => true
],
];
$your_new_table->select($where, $order);
//Output -> SELECT * FROM your_new_table WHERE `boolean_column` = 0 ORDER BY `datetime_column` DESC, ABS(`varchar_column`) ASC;
//Return -> Returns all rows where the boolean_column is set to false and then sorted by the datetime_column in DESC (newest - oldest) order first followed by varchar_column in ASC (1-9,A-Z) order.
/* Change the type of return you get from an associative array to a keyed object */
$your_new_table->select_all(false, OBJECT_K);
//Output -> SELECT * FROM your_new_table;
//Return -> Returns every row in the table formatted as an object with the column names as keys
/* Delete single row based on id */
$your_new_table->delete([
'id' => 1
]);
//Output -> DELETE FROM your_new_table WHERE id = 1;
//Result -> Deletes the row with the id of 1 from the table.
/* Delete rows using multiple definitions glued with AND */
$your_new_table->delete([
'boolean_column' => true,
'varchar_column' => 'Some text'
]);
//Output -> DELETE FROM your_new_table WHERE `boolean_column` = 1 AND `varchar_column` => "Some text";
//Result -> Deletes all rows where the boolean_column is true and the varchar_column is "Some text".
/* Delete rows using multiple definitions glued with AND and other operaters for comparison */
/*
Provide an array of arrays that will build the WHERE clause of your query
@params key(ue" => [1,10],
"compare" => "BETWEEN"
],
[
"key" => "varchar_column",
"value" => ['text', 3, 'some other text'],
"compare" => "NOT IN"
]
]);
//Output -> DELETE FROM your_new_table WHERE `boolean_column` = 1 AND `varchar_column` != "Some text" OR `longtext_column` LIKE "%look for this%";
//Result -> Deletes all rows where the boolean_column is true and the varchar_column does not "Some text" and the longtext_column contains the string "look for this".
/* Delete rows based on where cause passed in string */
$your_new_table->delete("`id` > 50 OR (`id` <= 50 AND `varchar_column` = 'Some Text')");
//Output -> DELETE FROM your_new_table WHERE `id` > 50 OR (`id` <= 50 AND `varchar_column` = 'Some Text');
//Result -> Deletes a row with an id greater than 50 or with an id of 50 or less if the varchar_column is equal to "Some text".
/* In the rare even you want to deletes all rows in the table */
//WARNING THIS WILL DELETE ALL DATA IN THE TABLE
$your_new_table->delete_all();
/* Simple update using the row's ID */
$your_new_table->update(
[
'longtext_column' => 'This is a line of new text to go into the cell',
'datetime_column' => '2021-01-01 12:00:00'
],
[
'id' => 35
]
);
//Output-> UPDATE your_new_table SET `longtext_column` = 'This is a line of new text to go into the cell', `datetime_column` = '2021-01-01 12:00:00' WHERE `id` = 35;
//Result-> Updates the row with an ID of 35 and changes two values in that row. The longtext_column and the datetime_column are both changed with their respective new values. NOTE: you can change as many or as few row values as you would like.
/* Multiple WHERE clause matches */
$your_new_table->update(
[
'longtext_column' => 'This is a line of new text to go into the cell',
'datetime_column' => '2021-01-01 12:00:00'
],
[
'varchar_column' => "Some value",
'boolean_column' => true,
'int_column' => 100
]
);
//Output-> UPDATE your_new_table SET `longtext_column` = 'This is a line of new text to go into the cell', `datetime_column` = '2021-01-01 12:00:00' WHERE `varchar_column` = "Some value" AND `boolean_column` = 1 AND `int_column` = 100;
//Result-> Updates the rows where the varchar_column is equal to "Some value", the boolean_column is true and the int_column is equal to 100. Change two values in that row. The longtext_column and the datetime_column are both changed with their respective new values. NOTE: you can change as many or as few row values as you would like.
/* Build a more complex WHERE clause for the update that includes custom operators and comparisons */
/*
Provide an array of arrays that will build the WHERE clause of your query
@params key(row values as you would like.
/* Simple update using a string for the WHERE clause */
$your_new_table->update(
[
'longtext_column' => 'This is a line of new text to go into the cell',
'datetime_column' => '2021-01-01 12:00:00'
],
"`id` > 35 OR id < 20"
);
//Output-> UPDATE your_new_table SET `longtext_column` = 'This is a line of new text to go into the cell', `datetime_column` = '2021-01-01 12:00:00' WHERE `id` > 35 OR `id` < 20;
//Result-> Updates the row with an ID of 35 and changes two values in that row. The longtext_column and the datetime_column are both changed with their respective new values. NOTE: you can change as many or as few row values as you would like.
/* ORIGINAL STRUCTURE */
public function columns(){
return [
'id' => 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT',
'int_column' => 'int(11) UNSIGNED NOT NULL DEFAULT 0',
'varchar_column' => 'varchar(15) NOT NULL',
'varchar_column_2' => 'varchar(30) NOT NULL DEFAULT "Default Stuff"',
'boolean_column' => 'boolean NOT NULL DEFAULT 0',
'text_column' => 'text NOT NULL',
'longtext_column' => 'longtext NOT NULL',
'datetime_column' => 'datetime NOT NULL DEFAULT "2000-01-01 12:00:00"'
]
}
/* ADD A COLUMN */
//To add a boolean column to your table called "boolean_column_2" where the default is set to true simply change your columns method to the following.
public function columns(){
return [
'id' => 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT',
'int_column' => 'int(11) UNSIGNED NOT NULL DEFAULT 0',
'varchar_column' => 'varchar(15) NOT NULL',
'varchar_column_2' => 'varchar(30) NOT NULL DEFAULT "Default Stuff"',
'boolean_column' => 'boolean NOT NULL DEFAULT 0',
'text_column' => 'text NOT NULL',
'longtext_column' => 'longtext NOT NULL',
'datetime_column' => 'datetime NOT NULL DEFAULT "2000-01-01 12:00:00"',
'boolean_column_2' => 'boolean NOT NULL DEFAULT 1'
]
}
/* CHANGE AN EXISTING COLUMN */
//In order to change the boolean_column to have a default of true and to change the length of varchar_column to 25 simply change your columns method to the following.
public function columns(){
return [
'id' => 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT',
'int_column' => 'int(11) UNSIGNED NOT NULL DEFAULT 0',
'varchar_column' => 'varchar(25) NOT NULL',
'varchar_column_2' => 'varchar(30) NOT NULL DEFAULT "Default Stuff"',
'boolean_column' => 'boolean NOT NULL DEFAULT 1',
'text_column' => 'text NOT NULL',
'longtext_column' => 'longtext NOT NULL',
'datetime_column' => 'datetime NOT NULL DEFAULT "2000-01-01 12:00:00"'
]
}
//Drops the longtext_column column from the table
$your_new_table->remove_column("longtext_column");
//After this please remove the longtext_column from your columns method in the Table child class you created earlier.
autoload.php
$data
$data
$data
which serves as the
which serves as the
which serves as the
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.