PHP HowTo - Dynamic Selects using a Function

Many sites have alot of different select fields in their forms. In order to streamline my coding efforts I have consolidated and am using a function to satisfy all my drop-down needs:

    function MakeSelectList($strSQL, $strParameters, $blnShowBlank)    {        $arrData = GetArrayQuery($strSQL);?>                    <select <?= $strParameters ?>>                        <option value=""></option>                        <option value="<?= $arrData[$i][0] ?>"><?= $arrData[$i][1] ?></option>                    </select>The GetArrayQuerry() function is another streamlined function I use to return data as a data array from the database. This way I only deal with arrays and am removing my code from having to deal directly with the database:
    function GetArrayQuery($strSQL)    {        $i = 0;        $objRS = $db->query($strSQL);        CheckError($objRS);        while ($objRS->fetchInto($arrRow)) {$arrData[$i++] = $arrRow;}

        return $arrData;    }

Let me know if you have more elegant ways of doing this! I’m always open to new ideas. :)

Leave a Reply