ListCreate

From Superk

Jump to: navigation, search
  /**
   * sqlTools::listCreate - Creates a listbox with given input query.
   *
   * Creates a form listbox listing the values provided.  Values are provided
   * to the method via a DB query and passed as an array.  In addition to the
   * name of the <select> element ($name) and the DB query array ($db), there
   * are two additional optional elements, $all and $selected.  If $all is
   * passed the string 'all' to the method, a special item will be prepended
   * to the listbox that will pass the value 'all' which can be used for all-
   * inclusive queries of the specified recordset.  $selected is an optional
   * variable that can accept the currently selected item from the previous
   * request.
   *
   * NOTE: Element 0 (the first element) of the DB Query passed to populate the
   * listbox will ALWAYS be used as the option values.  Subsequent elements in
   * the $db array will comprise the visible items separated by a space.
   *
   * EXAMPLE:
   * <?php $sql = new sqlTools; ?>
   * <form name="newform" action="thispage.php" method="get">
   *   <?php $sql->listCreate('mylist', $queryres, 'all', $_GET['mylist'], 'onchange="document.thisForm.submit()"'); ?>
   *   <input type="submit" name="submit" value="Submit" />
   * </form>
   *
   * @param string $name Name to be given the listbox element
   * @param array $db Query results to populate the listbox with
   * @param string $all (Optional) Will add a 'Show All' selection if 'all' is passed to this variable
   * @param string $selected (Optional) Pass the current selection from previous query to mark the item selected
   * @param string $html (Optional) Miscellaneous HTML string to pass to < select >
   * @returns string HTML < select > (listbox) form element fully populated.
   * @author
   */
  function listCreate($name, $db, $all, $selected, $html)
  {
    echo '<select name="'.$name.'"';
    if(!is_null($html)) {
      echo $html;
    }
    echo '>'."\n";
    if((!is_null($all))&&($all == 'all')) {
      echo '<option value="all">Show All</option>'."\n";
    }
    if(is_array($db[0])) {
      if(count($db) > 1) {
        foreach($db as $item) {
          echo '<option value="'.$item[0].'" ';
          if((isset($selected))&&($selected == $item[0])) {
            echo 'selected="selected" ';
          }
          echo '>';
          if(count($item) > 1) {
            for($i=1;$i<count($item);$i++) {
              echo $item[$i].' ';
            }
          } else {
            echo $item[0];
          }
          echo '</option>'."\n";
        }
      }
    } else {
      foreach($db as $item) {
        echo '<option value="'.$item.'" ';
        if((isset($selected))&&($selected == $item)) {
          echo 'selected="selected" ';
        }
        echo '>'.$item.'</option>'."\n";
      }
    }
    echo '</select>'."\n";
  }
Personal tools