# Field types
Function change_type()
a little difficult to understand, because it uses a specific syntax, which varies depending on the task. Let's try to beat these tasks for conventional groups.
The first three parameters are always the same.
Parameters
field - name of the field,
type - the desired field type
default - (Optional) default value.
params_or_attr - (Optional) array with type-parameters and/or html attributes. You can use custom html attributes in most of types.
# 1. Text fields.
This group includes the following types of fields: text, int, float, hidden.
For these types the function of the default value in the third parameter, and the maximum number of characters in the fourth parameter (excluding hidden).
$xcrud->change_type('user_code', 'int', '000', 10); // v1.5 legacy
$xcrud->change_type('user_code', 'int', '000', array('maxlength'=>10)); // v1.6
IMPORTANT
Use fields()
and pass_var()
methods to hide fields and put custom data in your table.
# 2. Multi-line text fields.
This group includes the following types of fields: textarea, texteditor.
This types no need any additional parameters.
$xcrud->change_type('user_desc', 'textarea');
# 3. Date fields.
This group includes the following types of fields: date, datetime, time, year, timestamp.
These types of fields will only accept the default value in the third parameter.
$xcrud->change_type('created', 'datetime', '2012-10-22 07:01:33');
Date range can be used with date type:
$xcrud->change_type('created', 'date', '', array('range_end'=>'end_date')); // this is start date field and it points to end date field
$xcrud->change_type('created', 'date', '', array('range_start'=>'start_date')); // this is end of range date and it points to the start date range date
This will create relation between two date fields
# 4. Lists.
This group includes the following types of fields: select, multiselect, radio, checkboxes
These types of fields are set to default in the third argument, and a list of available values, separated by commas, in the second parameter. multiselect can contain several values by default.
$xcrud->change_type('adm_email', 'select', 'email2@ex.com',
'email1@ex.com, email2@ex.com, email3@ex.com, email4@ex.com, email5@ex.com');
$xcrud->change_type('fav_color', 'multiselect', 'black, white',
'red, blue, yellow, green, black, white');
// v1.6 new syntax
$xcrud->change_type('fav_color', 'multiselect', 'black, white',
array('values' => 'red, blue, yellow, green, black, white')
);
You can show optgroups in dropdowns. Just us multi-dimensional array:
$xcrud->change_type('country', 'select', '',
array('values' => array(
'Europe' => array('UK' => 'United Kindom', 'FR' => 'France'),
'Asia' => array('RU' => 'Russia', 'CH' => 'China'))
));
Checkboxes
$xcrud->change_type('bool', 'checkboxes', '', array('values' =>
array(1 => 'Yes', 0 => 'No'))
);
Radio buttons
$xcrud->change_type('bool', 'radio', '', array('values' =>
array(1 => 'True', 0 => 'False'))
);
# 5. Password.
This group includes the following types of fields: password.
Takes the type of hashing in the third parameter (e.g. md5 or sha1 or sha256... , leave blank if you do not want to use hashing) and the maximum number of characters in the fourth. The peculiarity of this field is that it does not load the values and does not store the null value (not allowing you to change the previous one to null). This is due to the fact that passwords are usually encrypted or hashed before being stored in the database.
$xcrud->change_type('user_key', 'password', '', 32);
$xcrud->change_type('user_pass', 'password', 'sha256', 8);
// attributes example
$xcrud->change_type('user_pass', 'password', 'md5', array('maxlength'=>10,'placeholder'=>'enter password'));
# 6. Upload.
This group includes the following types of fields: file, image.
Takes the path to the upload folder (must exist and be writable) in the third parameter and configuration array in the fourth.
For all uploaded files xCRUD creates a unique name to avoid overwriting, but you can cancel the renaming. In this case, the file name will be brought to the alpha-numeric pattern.
$xcrud->change_type('attach', 'file', '', array('not_rename'=>true));
You can resize the uploaded image, and crop them at different proportions.
$xcrud->change_type('photo','image','',array('width'=>300)); // resize main image
$xcrud->change_type('photo','image','',array('width'=>300, 'height'=>300, 'crop'=>true)); // auto-crop
$xcrud->change_type('photo','image','',array('manual_crop'=>true)); // crop it as you want
You can create a preview picture for your image to be stored in that same folder, and a marker will differ at the end of the name. You can also specify the size of thumbnails, resize method and subfolder.
NOTE
'Manual crop' will crop thumbnails in same proportion as main image
You can add any number of thumbs, you must create subarray in thumbs array for each thumbnail
$xcrud->change_type('photo','image','',array(
'thumbs'=>array(
array('width'=> 50, 'marker'=>'_small'),
array('width'=> 100, 'marker'=>'_middle'),
array('width' => 150, 'folder' => 'thumbs' // using 'thumbs' subfolder
)
));
You can save your image as a binary string in the database. For this field in your database must be of type BLOB, MEDIUMBLOB or LONGBLOB. Thumbnail creation in this case is not available. If you do not know how to extract data from these fields, it is better use the normal load.
$xcrud->change_type('photo','image','',array('blob'=>true));
Uploads Parameters
Files
not_rename - [true/false] - disables auto-renaming of uploaded files
text - [string] - display custom file name
path - relative or absolute path to uploads folder
blob - [true/false] - saves image as binary string in database blob field
filename - [string] - name of downloadable file
url - [string] -real url to upload folder (optional, if you want to use real links]
Images
not_rename - [true/false] - disables auto-renaming of uploaded files
path - relative or absolute path to uploads folder
width, height - [integer] - sets dimensions for image, if both is not set image will not been resized
crop - [true/false] - image will be cropped (if not set, image will be saved with saving proportions). Both width and height required
manual_crop - [true/false] - Allows to crop image manually
ratio - [float] - cropped area ratio [uses with manual_crop]
watermark - [string] - relative or absolute path to watermark image
watermark_position - [array] - array with two elements (left,top), sets watermark offsets in persents (example: array(95,5) - right top corner)
blob - [true/false] - saves image as binary string in database blob field. Thumbnails creation is not available.
grid_thumb - [int] - number of thumb, which will be displayed in grid. If not set, original image will be displayed.
detail_thumb - [int] - number of thumb, which will be displayed in detail view/create/edit . If not set, original image will be displayed.
url - [string] -real url to upload folder [optional, if you want to use real links]
thumbs - array of thumb arrays:
width, height, crop, watermark, watermark_position - see parent
marker - [string] - thumbnail marker [if you not set marker or folder, the main image will be replaced with thumbnail]
folder - [string] - thumbnail subfolder, relative to uploads folder (if you not set marker or folder, the main image will be replaced with thumbnail)
IMPORTANT
All paths are always relative to xcrud's folder.
# 7. Price
This group includes the following types of fields: price.
Takes default value in third parameter and array of parameters in 4th.
$xcrud->change_type('amount', 'price', '5', array('prefix'=>'$'));
Parameters
max - [integer] - maximal length of text field (create/update), default is 10
decimals - [integer] - cout of decimals, default is 2
separator - [string] - thousands separator, default is comma (list view)
prefix - [string] - prefix for list view
suffix - [string] - suffix for list view
point - [string] - decimal point for list view
# 8. Remote images
This group includes the following types of fields: remote_image.
Takes default value in 3rd parameter and link part in 4th (e.g. if your field contains only file name, not full url).
$xcrud->change_type('avatar', 'remote_image');
// or
$xcrud->change_type('avatar', 'remote_image', '', 'http://my-img-host.net/my-folder/');
// 1.6
$xcrud->change_type('avatar', 'remote_image', '', array('link'=>'http://my-img-host.net/my-folder/'));
# 9. Locations and maps
This group includes the following types of fields: point.
This automatically works with 'POINT' mysql field type. In text field type coordinates will be saved as string separated with comma. 4th parameter gets array with map settings, also you can define them in configuration file to use by default.
$xcrud->change_type('my_location','point','39.909736,-6.679687',array('text'=>'Your are here'));
Parameters
width - (integer) - width of map
height - (integer) - height of map
zoom - (integer) - map zoom
text - (string) - text in info window
search_text - (string) - placeholder for search field
search - (true/false) - show search field
coords - (true/false) - show coordinates field