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

/* popupMenu
 * Developer: Alexandar Tzanov
 * Revised: 2013-08-28
 * Version: 1.0
function popupMenu($fileName = '', $relevantPath = '', $value = '', $name = 'popupMenu', $id = 'popupMenu', $size = 40)
    // Get working directory path.
    $workDir = getcwd();
    $dataFile = "$workDir/$relevantPath/$fileName";
    // Make sure the file exist, then read it.
    if (!empty($fileName) && file_exists($dataFile))
        $popupMenu = "';
        // Return popup menu code.
        return $popupMenu;
        // Return an input field with a value, if any.
        return "";


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


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


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.


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