Titan Fusion

Alexandar Tzanov's Personal Blog

Function: popupMenu


The popupMenu PHP function will generate a popup menu from a text file. If the file cannot be read or found the function will return a regular text field. The purpose of this function is to generate a menu with a long list of options. Another benefit of this function is to reuse it in different views, but to only have to maintain the list of item in one place – the text file.

Source Code

  1. /* popupMenu
  2.  * Developer: Alexandar Tzanov
  3.  * Revised: 2013-08-28
  4.  * Version: 1.0
  5.  */
  6.  
  7. function popupMenu($fileName = '', $relevantPath = '', $value = '', $name = 'popupMenu', $id = 'popupMenu', $size = 40)
  8. {
  9.     // Get working directory path.
  10.     $workDir = getcwd();
  11.     $dataFile = "$workDir/$relevantPath/$fileName";
  12.  
  13.     // Make sure the file exist, then read it.
  14.     if (!empty($fileName) && file_exists($dataFile))
  15.     {
  16.         $popupMenu = "<select name=\"$name\" id=\"$id\">\n<option></option>\n";
  17.         $fileData = file($dataFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  18.  
  19.         // Loop through file data array and build string.
  20.         foreach ($fileData as $menuItem)
  21.         {
  22.             $menuItemSelected = ($value == trim($menuItem)) ? 'selected="yes"' : '';
  23.             $popupMenu .= "<option value=\"$menuItem\" $menuItemSelected>$menuItem</option>\n";
  24.         }
  25.  
  26.         $popupMenu .= '</select>';
  27.  
  28.         // Return popup menu code.
  29.         return $popupMenu;
  30.     }
  31.     else
  32.     {
  33.         // Return an input field with a value, if any.
  34.         return "<input name=\"$name\" size=\"$size\" id=\"$id\" value=\"$value\">";
  35.     }
  36. }

Usage

string popupMenu( string $fileName[,string $relevantPath[, string $value[, string $name = popupMenu[, string $id = popupMenu[, int $size = 40]]]]])

Parameters

Name Definition Default Value
$fileName The name of the text file with the items. One option item per line. N/A
$relevantPath The local path to the file starting from the product’s work directory. The work directory is automatically detected. N/A
$value The value to match agains to preselect a on option. N/A
$name The name attribute of the select/input HTML element. popupMenu
$id The id attribute of the select/input HTML element. popupMenu
$size The size of the input text field. 40

Return

The popupMenu function will return one of two possible form field.

  • A popup menu, i.e. a select with options, if a readable text file with a name and at location specified by the developer is found.
  • A text field, if the file specified by the developer is either not readable or cannot be found.

Examples

// Example 1 - Generate the options menu.
echo popupMenu('list-of-items.txt', 'bin');
 
// Example 2 - Generate the options menu with a preselected option.
print popupMenu('itemslist.txt', '', 'Apple');

Revision History

Date Version Changes
2013-08-28 01.00.00 Initial release.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.