PEAR logo

HTML_QuickForm_advmultiselect : The Definitive Guide



Sort usage

Figure 9.4. Auto-arrange feature usage

Auto-arrange feature usage

In this example, here are our goals :

  • add the user-end sort buttons (up, down).
  • add the ability to sort lists in alphabetic order or reverse (by programming).

Example 9.4. How to sort the select boxes

  1. <?php
  2. /**
  3. * Custom advMultiSelect HTML_QuickForm element
  4. * that allows to manage sort of select boxes.
  5. *
  6. * @version    $Id: qfams_custom_5.php,v 1.4 2007/01/05 15:35:33 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_custom_5.php
  12. *             qfams_custom_5 source code
  13. * @link       http://www.laurent-laville.org/img/qfams/screenshot/custom5.png
  14. *             screenshot (Image PNG, 609x318 pixels) 9.94 Kb
  15. */
  16.  
  17. require_once 'HTML/QuickForm.php';
  18. require_once 'HTML/QuickForm/advmultiselect.php';
  19.  
  20. $form = new HTML_QuickForm('amsCustom5');
  21. $form->removeAttribute('name');        // XHTML compliance
  22.  
  23. $fruit_array = array(
  24.     'apple'     =>  'Apple',
  25.     'orange'    =>  'Orange',
  26.     'pear'      =>  'Pear',
  27.     'banana'    =>  'Banana',
  28.     'cherry'    =>  'Cherry',
  29.     'kiwi'      =>  'Kiwi',
  30.     'lemon'     =>  'Lemon',
  31.     'lime'      =>  'Lime',
  32.     'tangerine' =>  'Tangerine',
  33. );
  34.  
  35. // decides either both select list will have their elements be arranged or not
  36. if (isset($_POST['autoArrange'])) {
  37.     if ($_POST['autoArrange'] == 'A') {
  38.         $sort = SORT_ASC;
  39.     } elseif ($_POST['autoArrange'] == 'D') {
  40.         $sort = SORT_DESC;
  41.     } else {
  42.         $sort = false;
  43.     }
  44. } else {
  45.     $sort = false;
  46. }
  47.  
  48. // rendering with QF renderer engine and template system
  49. $form->addElement('header', null,
  50.                   'For demo purpose only: must be validate to be active');
  51. $arrange[] =& $form->createElement('radio', null, null, 'Auto arrange asc.''A');
  52. $arrange[] =& $form->createElement('radio', null, null, 'Auto arrange desc.', 'D');
  53. $arrange[] =& $form->createElement('radio', null, null, 'No auto arrange',    'N');
  54. $form->addGroup($arrange, 'autoArrange', 'Sort list:');
  55. $form->setDefaults(array('autoArrange' => 'N'));
  56.  
  57. $validate =& $form->addElement('submit', null, 'Validate');
  58.  
  59. $form->addElement('header', null, 'Advanced Multiple Select: custom layout ');
  60.  
  61. $ams =& $form->addElement('advmultiselect', 'fruit', null, $fruit_array,
  62.                            array('class' => 'pool', 'style' => 'width:200px;'),
  63.                            $sort
  64. );
  65. $ams->setLabel(array('Fruit:', 'Available', 'Selected'));
  66.  
  67. $ams->setButtonAttributes('add'     , 'class=inputCommand');
  68. $ams->setButtonAttributes('remove'  , 'class=inputCommand');
  69. $ams->setButtonAttributes('moveup'  , 'class=inputCommand');
  70. $ams->setButtonAttributes('movedown', 'class=inputCommand');
  71.  
  72. // template for a dual multi-select element shape
  73. $template = '
  74. <table{class}>
  75. <!-- BEGIN label_2 --><tr><th>{label_2}</th><!-- END label_2 -->
  76. <!-- BEGIN label_3 --><th>&nbsp;</th><th>{label_3}</th></tr><!-- END label_3 -->
  77. <tr>
  78.   <td>{unselected}</td>
  79.   <td align="center">
  80.     {add}<br />{remove}<br /><br />{moveup}<br />{movedown}<br />
  81.   </td>
  82.   <td>{selected}</td>
  83. </tr>
  84. </table>
  85. ';
  86. $ams->setElementTemplate($template);
  87.  
  88. if ($_SERVER['REQUEST_METHOD'] == 'GET') {
  89.     // fruit default values already selected without any end-user actions
  90.     $form->setDefaults(array('fruit' => array('kiwi','lime')));
  91.  
  92. } elseif (isset($_POST['fruit'])) {
  93.     // fruit end-user selection
  94.     $form->setDefaults(array('fruit' => $_POST['fruit']));
  95. }
  96.  
  97. $buttons[] =& $form->createElement('submit', null, 'Submit');
  98. $buttons[] =& $form->createElement('reset'null, 'Reset');
  99. $form->addGroup($buttons);
  100. ?>
  101. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  102.     "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  103. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  104. <head>
  105. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  106. <title>HTML_QuickForm::advMultiSelect custom example 5</title>
  107. <style type="text/css">
  108. <!--
  109. body {
  110.   background-color: #FFF;
  111.   font-family: Verdana, Arial, helvetica;
  112.   font-size: 10pt;
  113. }
  114.  
  115. table.pool {
  116.   border: 0;
  117.   background-color: #339900;
  118. }
  119. table.pool td {
  120.   padding-left: 1em;
  121. }
  122. table.pool th {
  123.   font-size: 80%;
  124.   font-style: italic;
  125.   text-align: center;
  126. }
  127. table.pool select {
  128.   color: white;
  129.   background-color: #006600;
  130. }
  131.  
  132. .inputCommand {
  133.   width: 60px;
  134. }
  135.  -->
  136. </style>
  137. <?php echo $ams->getElementJs(false); ?>
  138. </head>
  139. <body>
  140. <?php
  141. if ($form->validate()) {
  142.     $clean = $form->getSubmitValues();
  143.  
  144.     echo '<pre>';
  145.     print_r($clean);
  146.     echo '</pre>';
  147. }
  148. $form->display();
  149. ?>
  150. </body>
  151. </html>
HTML_QuickForm_advmultiselect : The Definitive Guide v 1.4.0 : 9 Juin 2007