# Main Methods
The documents listed in the left navigation provide details about how to build apps using xCRUD's various APIs. This guide lists the main methods to run xCRUD.
# get_instance()
Returns an xCRUD object. This is a static method and can be called anywhere, indefinitely.
Parameter | Type | Default | Description |
---|---|---|---|
$name | string | false | Assign an identifier to the instance being created. |
$xcrud = Xcrud::get_instance();
# Multiple Instances
You can create multiple instances of xCRUD on one page (for example, to work with multiple tables):
$xcrud1 = Xcrud::get_instance();
$xcrud2 = Xcrud::get_instance();
Next, for each instance you can use different methods.
TIP
If you want to get the same instance in different functions/files (for example, in controller and view), you must specify the $name
. Calling an instance by name you will get the same instance. See Below
// controller.php
$xcrud = Xcrud::get_instance('MyInstance');
$xcrud->table('users');
// view.php
$xcrud = Xcrud::get_instance('MyInstance');
$echo xcrud->render();
# table()
Sets the table name from which data will be loaded. This method is mandatory.
Parameter | Type | Default | Description |
---|---|---|---|
$table_name | string | The name of the table. |
``''``$xcrud->table('my_table');
You can also load a table from another database.
IMPORTANT
xCRUD needs at least one primary or unique field in your table. If not, you will get an error. The best practice is to use PRIMARY AUTO_INCREMENT column in every table. This was done to prevent losing of your data. xCRUD can't work with complex (combined) primary index.
# connection()
Creates a connection for the current instance of xCRUD.
Parameter | Type | Default | Description |
---|---|---|---|
$user | string | Database user username. | |
$pass | string | '' | Database user password. |
$table | string | '' | Table name. |
$host | string | 'localhost' | Database host. |
$encode | string | 'utf-8' | Connection character encoding. |
$xcrud2->connection('username','password','remote_table','localhost','utf-8');
NOTE
This method is rewriting connection settings from configuration file for current instance
# language()
Sets interface language.
Parameter | Type | Default | Description |
---|---|---|---|
$lang | string | 'en' | Language code. |
$xcrud->language('en'); // will load en.ini lang file from languages folder
TIP
It's better to set the language in the configuration file. Use this method only if you really need dynamic language switching.
# set_lang()
Allows you to replace existing language translator in the current instance.
Parameter | Type | Default | Description |
---|---|---|---|
$var | string | '' | |
$translate | string | '' |
NOTE
The best way is to customize the lang file. Do not create new variables with set_lang()
, put them in the language file.
# render()
Displays your table data. While this method is not called, xCRUD takes no action, the database is not connected, saving system resources, especially critical for large data.
Parameter | Type | Default | Description |
---|---|---|---|
$task | string | false | Render xCRUD in create, edit or view screens. |
$task can be: 'create' , 'edit' , 'view | |||
$primary | mixed | false | Primary key value of table row to be displayed when $task is set to 'edit' or 'view' . |
$out = $xcrud->render();
// some code...
echo $out;
// or
echo $xcrud->render();
// or
echo $xcrud;
xCRUD by default renders the grid (list) screen. You can render xCRUD straight into create, edit or view (details) screen.
$xcrud->render('create'); // add entry screen
$xcrud->render('edit', 12); // edit entry screen, '12' - primary key
$xcrud->render('view', 85); // view entry screen, '85' - primary key
TIP
Remember that the render()
method must be called last, it just returns the data for output, and all the methods declared after (for the current instance) will not be applied.