function form_select()

Tools by the phpBBModders team and Community contributed tools.
Forum rules
Only post tools related to modding in here, simple.
Post Reply
User avatar
tumba25
Supporter
Supporter
Posts: 1058
Joined: 24 Oct 2007, 13:10
Real name: Jari Kanerva
Location: Kokkola, Finland.
Contact:

function form_select()

Post by tumba25 »

Some of our tools uses the function form_select() that is located in our own functions.php file. That file is not displayed when looking at the tools code. So here it is.

Code: Select all

/**
 * Make a form select
 *
 * @param array $select_ary Array of options
 * @param string $default Default option
 * @param mixed $lang_prefix Language prefix, if this is false, no lang is used
 * @param string $not_assoc If this is true, no associative array is used
 * @return string Select html
 */
function form_select($select_ary, $default = '', $lang_prefix = '', $not_assoc = false, $prefix_is_key = false, $lang_strtoupper = true)
{
   // only accept arrays, no empty ones
   if (!is_array($select_ary) || !sizeof($select_ary))
   {
      return '';
   }

   // If selected isn't in the array, use first entry
   if ($not_assoc && !in_array($default, $select_ary, true))
   {
      $default = $select_ary[0];
   }
   else if (!$not_assoc && !isset($select_ary[$default]))
   {
      // neat trick from php.net
      $default = array_shift(array_values($select_ary));
   }

   if ($lang_prefix !== false)
   {
      global $user;

      if ($prefix_is_key)
      {
         $lang = &$user->lang[$lang_prefix];
         $lang_prefix = '';
      }
      else
      {
         $lang = &$user->lang;
      }
   }

   $select_options = '';
   foreach ($select_ary as $key => $value)
   {
      if ($not_assoc)
      {
         $key = $value;
      }

      if ($lang_prefix !== false)
      {
         $value_key = $lang_strtoupper ? strtoupper($lang_prefix . $value) : $lang_prefix . $value;
         $value = !empty($lang[$value_key]) ? $lang[$value_key] : $value;
      }

      $select_options .= '<option value="' . $key . '"' . (($key == $default) ? ' selected="selected"' : '') . '>' . $value . '</option>';
   }
   return $select_options;
}
Dakin Quelia
New member
New member
Posts: 49
Joined: 12 Nov 2008, 01:26
Real name: Daniel
Location: Belgique

Re: function form_select()

Post by Dakin Quelia »

Thank you very much, tumba25. ;)
Post Reply