Re: [phplib] How can I populate a select box from a db query? From: Davor Cengija (dcengija <email protected>)
Date: 11/15/00

"White, Bob" <rcwhite <email protected>> writes:

> Hi All,
> How can I populate a select box from an oracle db query?
> Any help would be greatly appreciated.
> Bob White

        I have 2 functions (extending form class) which might help
        you:

class custom_form extends form {
        
        var $database_class = "DB_custom";
        var $db;
        
        function GetDB() {
                if(!is_object($this->db)) {
                        $class = $this->database_class;
                        $this->db = new $class;
                }
                return $this->db;
        }

        /*
         * Put an array and build a select list
         * array(array(id, title))
         */
        function DropMenu($array, $name, $size = 1, $txt = "Choose one", $defvalue = 0, $extrahtml = '') {
        $selectone = array("label" => "$txt", "value" => $defvalue);
        $options = array($selectone);

                $count = count($array);
                for($i = 0; $i < $count; $i++) {
                        $tmp = $array[$i];
                        $a = false;
                        while(list($arraykey, $arrayvalue) = each($tmp)) {
                                if($a == false) {
                                        $value = $arrayvalue;
                                        $a = true;
                                }
                                else {
                                        $label = $arrayvalue;
                                }
                        }
                        $options[] = array("label"=>$label, "value"=>$value);
                }

        $this->add_element(array(
                    "type" => "select",
                    "name" => $name,
                    "options" => $options,
                    "extrahtml" => $extrahtml,
                    "size" => $size));
        }
        
    /*
     * Query a database and put the results into a select list. You'll
       need a query which returns 2 values per row, ID and VALUE
       like
        $qry = "SELECT id, value FROM table";
     */
    function DropMenuLoad($qry, $name, $size = 1, $txt = "Choose one", $defvalue = 0, $extrahtml = '') {
       
                   $this->db = $this->GetDB();

                $this->db->query($qry);

        $selectone = array("label" => "$txt", "value" => $defvalue);
        $options = array($selectone);

        while ($this->db->next_record()) {
            $value = $this->db->Record[0];
            $label = $this->db->Record[1];

            $temparray = array("label" => $label, "value" => $value);
            $options[] = $temparray;
        }
        $this->add_element(array(
                    "type" => "select",
                    "name" => $name,
                    "options" => $options,
                    "extrahtml" => $extrahtml,
                    "size" => $size));

    } /* Drop Menu Load*/

        Now you have an form element $name, which can be displayed as
        
$f->show_element($name)

-- 
Besplatno kopiranje Linux distribucija i softwarea
[Novi Mandrake, RedHat, Debian, ...]
http://www.inet.hr/~dcengija/

--------------------------------------------------------------------- To unsubscribe, e-mail: phplib-unsubscribe <email protected> For additional commands, e-mail: phplib-help <email protected>