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 another database query to pre-select values for a selectDynamic element:


For such a select:

  1. create an SQL query using the sql setting that fetches all the possible values from a database, for example: Array( 1 => 'WAV', 2 => 'MP3', 3 => 'AVI', 4 => 'MPEG' ),
  2. and create another SQL query using the valuesql setting that specifies which options are to be selected -
    Array( 1, 3 ) in this example.

single dynamic select

<?php

$config 
= Array(

  
'id' => Array(
    
'type'           => 'selectDynamic',
    
'displayname'    => 'This is a dynamic select',
    
'sql'            => 'SELECT id, name FROM writers',
    
'values'         =>
      Array( 
        
=> 'choose nothing'
        
=> 'value 1'
        
=> 'value 2'
      
),
    
'value'          => '0',

    
// 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.

    
'levelprefix' => '&nbsp;&nbsp;',
       
// a string prefix for option texts
       // to create the tree visually.
       // At the root level it's used 0 times, 
       //        level 1: once, 
       //        level n: n times.

    
'html'           => 'class="inputfieldstyle"',
    
'help'           => 'validation failed for this element',
    
'rowlayout'      => '...%element% %prefix% %postfix% etc...',
    
'prefix'         => 'string to display before element',
    
'postfix'        => 'string to display after element',
    
'readonly'       => 0,
    
'display'        => 0,
    
'htmlid'         => 'name1',

  ),

);

?>

multiple dynamic select

The differences between a single and a multiple select are:

  • the name of the multiple select element must have trailing [] (square brackets).
  • the multiple select must have the following string injected in its 'html' setting: multiple="multiple"
  • you can use the 'valuesql' setting to pre-select values
  • in the Clonefish constructor you can pass an id parameter to the class. In 'valuesql' you can reuse this value as a %s placeholder. It makes easier to create modification forms without injecting the ID manually into the SQL query.

All the settings of a single dynamic select can still be used (including the tree format). These settings are not repeated in the following configuration definition.

<?php 

$config 
= Array(

  
'booksofawriter[]' => Array(
    
'type'           => 'selectDynamic',
    
'displayname'    => 'This is a dynamic multiple select',

    
// an XHTML compatible way of the multiple parameter
    
'html'           => 'multiple="multiple"',

    
// 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( => 'choose nothing' ),
    
'valuesql'       => 
      
"SELECT bookid
       FROM booksofawriter
       WHERE writerid = %s"
,
    
'value'          => '0' | Array( 1)
  )

);

?>

Live examples:

Validators to use together with this element:

form validation for developers!

clonefish is a proven, mature form generator class that helps PHP developers to create, validate and process secure, flexible and easy-to-maintain forms
learn more
Bookmark and Share