Re: Help in reading DB info into a pulldown menu

From: Date: Thu, 02 Nov 2000 17:35:35 +0000
Subject: Re: Help in reading DB info into a pulldown menu
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-4149@lists.php.net to get a copy of this message
> Can someone point me to sample code in which > info extracted from a database (in my case mySQL) > is read into the pulldown menu options in HTML? > Something like this should do the trick. This works with objects but could easily be changed to work with other values. Call it like this: echo drawFormSelect('items', $item_obj, 'select id, name from items order by name', '-- Select an Item --'); /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// // FUNCTION NAME: // drawFormSelect // // PURPOSE: // Return a HTML <SELECT> menu based on the input. It // accepts // // INPUT: // $name => The value in <SELECT NAME=$name> // $obj => The object we are working with when we call // this function. We look for a property in this // object named $name and use that as the // default item that is selected. // $sql => The SQL query that will be execute to populate // the menu. We use the first item selected as our // key so this most likely needs to be an id field // in the db. // $default_text => The default text that is displayed at the // top of the list. This can be selected to undefine // the variable and is shown if nothing in $obj // matches. // // function drawFormSelect($name, $obj, $sql, $default_text) { global $DB; $result = $DB->execute($sql); $r = '<SELECT NAME="' . $name . '">'; $r .= ($default_text ? ('<OPTION VALUE="">' . $default_text . '</OPTION>') : ''); while (!$result->EOF) { /* for a property named $name in the object that was passed */ /* in ($obj). We want to use the id number of the $obj->$name */ /* as the item that is selected. */ if(is_object($obj)) { $selected = ($obj->$name == $result->fields[0]) ? 'SELECTED' : ''; } $result->nextRow(); } $r .= "</SELECT>"; return $r; } // // END FUNCTION drawFormSelect /////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

« previous php.db (#4149) next »