Advertisement
netheinz

Select box method

Jan 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.10 KB | None | 0 0
  1.    /**
  2.      * Builds a select box
  3.      * Function to build and return select box
  4.      * @param string $id
  5.      * @param array $options Structure: Array[i] { [id], [title] }
  6.      * @param int $value
  7.      * @return string html with selectbox
  8.      */
  9.     function inputSelect($id, $options, $value) {
  10.         $strHtml = "<select class=\"form-control\" id=\"" . $id . "\" name=\"" . $id . "\">\n";
  11.         foreach ($options as $option) {
  12.             /* Convert to array with numeric index */
  13.             $array = array_values($option);
  14.             /* Define if option should be selected */
  15.             $selected = ($value === $array[0]) ? "selected" : "";
  16.             /* Accumulate html string with option */
  17.             $strHtml .= "<option value=\"" . $array[0] . "\" " . $selected . ">" . $array[1] . "</option>\n";
  18.         }
  19.         $strHtml .= "</select>\n";
  20.         return $strHtml;
  21.     }
  22.  
  23.     /* Function Call */
  24.     $strSelect = "SELECT id, name FROM table1 ORDER BY name";
  25.     $arrOptions = $db->fetch_array($strSelect);
  26.     $strOutPut = inputSelect("selectbox_name", $arrOptions, $selected_id);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement