select options, generated dropdown options combobox control dynamic database sql select multiple="multiple" checked check multiple options using ctrl control
selectDynamic
A single or multiple select with values coming from a database. This control also handles tree-like structures (like a multi-level category structure for an online shop).
You can also use a database query to pre-select values for a
multiple select like in this example:
For such a select you would create an SQL query that fetches all the
possible values from a database (these are 'WAV', 'MP3', 'AVI', 'MPEG'),
and another SQL query that specifies which options are to be selected
('WAV' and 'AVI').
- Live examples:
- Definition:
single dynamic select
<?php $config = Array( 'id' => Array( 'type' => 'select', 'displayname' => 'This is a dynamic select', 'sql' => 'SELECT id, name FROM writers', 'values' => Array( 0 => 'choose nothing', 1 => 'value 1', 2 => 'value 2' ), 'value' => '0', 'html' => 'multiple="multiple"', // an XHTML compatible way of writing the "multiple" attribute // if you want a tree-select (check the tree-select // example!), you should use these // settings: 'treeid' => 'id', 'treestart' => '0', 'treeparent' => 'parentid', // in such a case you should use %s in the 'sql' // parameter as the placeholder of the WHERE clause // of the query (the WHERE clause is automatically // created based on the tree-settings, eg. // 'parentid = current_treeid' ). // example: // 'sql' => 'SELECT id, name FROM table // WHERE active = 1 AND %s // if you omit this template, a complete // WHERE clause will be automatically // appended to the value of the 'sql' setting. ) ); ?>multiple dynamic select
It's very important to note that the element name must be in the form
name[]for multiple selects! This way you'll receive the selected values in an array (this is a PHP feature). Feel free to use the tree-settings even for multiple selects.<?php $config = Array( 'booksofawriter[]' => Array( 'type' => 'selectDynamic', 'displayname' => 'This is a dynamic multiple select', 'html' => 'multiple="multiple"', // an XHTML compatible way of the multiple parameter // first field is the option value, second is the displayed // option text, field names do not count 'sql' => 'SELECT id, title FROM books', 'values' => Array( 0 => 'choose nothing', ), 'valuesql' => "SELECT bookid FROM booksofawriter WHERE writerid = %s", 'value' => '0' | Array( 1,2 ) ) ); ?> - Possible validators:
- 'required'
- 'custom'
