Basic 2

Overview

This example show how to populate the two select fields with options coming from a database rather than basic array.
Basic render options are used.

Screenshot

sample screenshot

Demonstration

Give it a try

Dependencies

This example requires mandatory resources :

Explains step by step

Step 1

Suppose we have to retrieve information from a database, and have a simple table for holding user info.


This SQL statement creates a table with a content usable under the default database scheme using MySQL:
CREATE TABLE user (
   userid VARCHAR(5) NOT NULL,
   gid INT NOT NULL,
   affect INT NOT NULL,
   lastname VARCHAR(50)NOT NULL,
   firstname VARCHAR(50) NOT NULL,
   PRIMARY KEY (userid)
);

INSERT INTO user VALUES ('MJ001', 1, 0, 'Martin', 'Jansen');
INSERT INTO user VALUES ('BG001', 1, 1, 'Greg', 'Beaver');
INSERT INTO user VALUES ('CD001', 1, 0, 'Daniel', 'Convissor');
INSERT INTO user VALUES ('LL001', 2, 1, 'Laurent', 'Laville');

Step 2

At line 20 we will get DSN data informations: $user, $pass, $host, $db required to connect to the database (line 26).

Our first query that will get all users of group #1 to fill the AVAILABLE list (on left side) is built on lines 29-30.

While our second query that will get identities of user already affected in group #1, and fill the SELECTED list (on right side) is built on line 33.

Caution: line 63 is empty (null) deliberatly, because we will use the QF/select load feature at line 69.

At line 66 we decided to used the default template (null), but without javascript code embedded (false), because we will use a common handler at line 97.

Step 3

To beautify a bit render of the dual multi-select element, we have added two headers, one for each select box (left and right) on line 62, and enlarge width (double size) from default 100 pixels on line line 64.

pretty easy !

Source Code

PHP code
  1. <?php
  2. /**
  3.  * Basic advMultiSelect HTML_QuickForm element without any customization.
  4.  * Load options from a database.
  5.  *
  6.  * @version    $Id: qfams_basic_2.php,v 1.4 2009/01/29 11:22:26 farell Exp $
  7.  * @author     Laurent Laville <pear@laurent-laville.org>
  8.  * @package    HTML_QuickForm_advmultiselect
  9.  * @subpackage Examples
  10.  * @access     public
  11.  * @example    examples/qfams_basic_2.php
  12.  *             qfams_basic_2 source code
  13.  * @link       http://www.laurent-laville.org/img/qfams/screenshot/basic2.png
  14.  *             screenshot (Image PNG, 605x283 pixels) 5.17 Kb
  15.  */
  16.  
  17. require_once 'DB.php';
  18. require_once 'HTML/QuickForm.php';
  19. require_once 'HTML/QuickForm/advmultiselect.php';
  20. require_once 'dsn_qfams_basic2.inc';  // dsn data: $user, $pass, @$host, $db
  21.  
  22. PEAR::setErrorHandling(PEAR_ERROR_DIE);
  23.  
  24. $dsn = "mysql://$user:$pass@$host/$db";
  25.  
  26. $db = DB::connect($dsn);
  27.  
  28. // query to get all users of group #1 (available users list)
  29. $queryAll = 'SELECT userid, CONCAT(lastname, " ", firstname) AS useridentity '
  30.           . 'FROM user WHERE gid = 1';
  31.  
  32. // query to get all users affected of group #1 (selected users list)
  33. $querySel = 'SELECT userid FROM user WHERE gid = 1 AND affect = 1';
  34.  
  35. // execute query to get ident of users affected
  36. $affected_user =& $db->getCol($querySel);
  37.  
  38.  
  39. $form = new HTML_QuickForm('amsBasic2');
  40. $form->removeAttribute('name');  // XHTML compliance
  41.  
  42. // same as default element template but wihtout the label (in first td cell)
  43. $withoutLabel = <<<_HTML
  44. <tr valign="top">
  45.     <td align="right">
  46.         &nbsp;
  47.     </td>
  48.     <td align="left">
  49.         <!-- BEGIN error --><span style="color: #ff0000;">{error}</span><br /><!-- END error -->{element}
  50.     </td>
  51. </tr>
  52. _HTML;
  53.  
  54. // more XHTML compliant
  55. // replace default element template with label, because submit button have no label
  56. $renderer =& $form->defaultRenderer();
  57. $renderer->setElementTemplate($withoutLabel, 'send');
  58.  
  59. $form->addElement('header', null, 'Advanced Multiple Select: default layout ');
  60.  
  61. $ams =& $form->addElement('advmultiselect', 'user',
  62.     array('Users:', 'Available', 'Affected'),         // labels
  63.     null,                                             // datas
  64.     array('style' => 'width:200px;')                  // custom layout
  65. );
  66. $ams->setElementTemplate(null, false);
  67.  
  68. // load QFAMS values (unselected and selected)
  69. $ams->load($db, $queryAll, 'useridentity', 'userid', $affected_user);
  70.  
  71. $form->addElement('submit', 'send', 'Send');
  72.  
  73. // close database connection
  74. $db->disconnect();
  75. ?>
  76. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  77.     "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  78. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  79. <head>
  80. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  81. <title>HTML_QuickForm::advMultiSelect basic example 2</title>
  82. <style type="text/css">
  83. <!--
  84. body {
  85.   background-color: #FFF;
  86.   font-family: Verdana, Arial, helvetica;
  87.   font-size: 10pt;
  88. }
  89. pre.sql {
  90.   border: 1px solid silver;
  91.   background-color: lightyellow;
  92.   color: black;
  93.   padding: 1em;
  94. }
  95.  -->
  96. </style>
  97. <?php echo $ams->getElementJs(false); ?>
  98. </head>
  99. <body>
  100. <?php
  101. if ($form->validate()) {
  102.     $clean = $form->getSubmitValues();
  103.  
  104.     echo '<pre>';
  105.     print_r($clean);
  106.     echo '</pre>';
  107. } else {
  108.     $form->display();
  109. }
  110. ?>
  111.  
  112. <pre class="sql">
  113. CREATE TABLE user (
  114.    userid VARCHAR(5) NOT NULL,
  115.    gid INT NOT NULL,
  116.    affect INT NOT NULL,
  117.    lastname VARCHAR(50)NOT NULL,
  118.    firstname VARCHAR(50) NOT NULL,
  119.    PRIMARY KEY (userid)
  120. );
  121.  
  122. INSERT INTO user VALUES ('MJ001', 1, 0, 'Martin', 'Jansen');
  123. INSERT INTO user VALUES ('BG001', 1, 1, 'Greg', 'Beaver');
  124. INSERT INTO user VALUES ('CD001', 1, 0, 'Daniel', 'Convissor');
  125. INSERT INTO user VALUES ('LL001', 2, 1, 'Laurent', 'Laville');
  126. </pre>
  127.  
  128. </body>
  129. </html>
generated by Generic Syntax Highlighter - GeSHi