htmlselect-class

Gesponsorde koppelingen

PHP script bestanden

  1. htmlselect-class

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/*
  $Id: class.select.php,v 1.0 2006/07/07 22:10:38 hpdl Exp $

  Author: Kees Schepers  
  More Info: http://www.phphulp.nl/php/scripts/4/692
  Released under the GNU General Public License
*/

interface htmlElementInterface {
      public function getHTML();
      public function getDomObject();
}

/**
* Class to create a select box and to set some property's
**/

class select implements htmlElementInterface {
      private $aAttributes;
      private $objDomDocument;
      private $aData;
      private $objSelect;
      public function __Construct($p_aData) {            
            /**
            * Check and build the data array
            **/

            $this->aData = array();
            $this->setData($p_aData);
            
            /**
            * Start the DOM document for creating HTML
            **/

            $this->objDomDocument = new DOMDocument('1.0');
      }
      
      public function __Set($key,$value) {
            switch($key) {
                  case
'id' :
                        $this->aAttributes['id'] = $value;
                        break;
                  case
'name' :
                        $this->aAttributes['name'] = $value;
                        break;
                  case
'onchange' :
                        $this->aAttributes['onchange'] = $value;
                        break;
                  case
'class' :
                        $this->aAttributes['class'] = $value;
                        break;
                  case
'style' :
                        $this->aAttributes['style'] = $value;
                        break;
                  default :

                        throw new exception('select::__Set() -> Trying to set an option which not exists! ('.$key.')');
                        break;                        
            }
      }

      /**
      * function which builds the correct data array which will be internaly used.
      * @param array $p_aData array with elements for the select list
      **/

      private function setData($p_aData) {
            if( is_array($p_aData) ) {
                  foreach($p_aData AS $element) {
                        if( is_array( $element ) ) {
                              if( count( $element ) > 1 )  {
                                    /**
                                    * Array has a value and a text node. The first index is the value second the                                    
                                    * text node. If more keys given, ignore them.
                                    **/

                                    $this->aData[] = array('value' => $element[0],'text' => $element[1]);
                              }
else {
                                    throw new exception('select::setData() -> Wrong data given as parameter! (expects an array with at least more then one index)');
                              }
                        }
else {
                              /**
                              * Text and value are the same
                              **/

                              $this->aData[] = array('value' => $element, 'text' => $element);
                        }
                  }
            }
else {
                  throw new exception('select::setData() -> Data given as parameter should be an array!');
            }
      }

      /**
      * function which creates the select element, and calls the attribute(s)-creator-function
      **/

      private function setSelectElement() {
            $this->objSelect = $this->objDomDocument->createElement('select');
            $this->objSelect = $this->objDomDocument->appendChild($this->objSelect);
            $this->setAttributes();
      }

      /**
      * function which adds attributes to the select element.
      **/

      private function setAttributes() {
            if( is_array($this->aAttributes) ) {
                  foreach($this->aAttributes AS $attributeName=>$attributeValue) {
                        $this->objSelect->setAttribute($attributeName,$attributeValue);
                  }
            }
      }

      /**
      * This function loops the array and creates the select box options
      **/

      private function setSelectOptions() {
            if( is_array($this->aData) ) {
                  /**
                  * Loop all array options
                  **/

                  foreach($this->aData AS $aElement) {
                        $objOption = $this->objDomDocument->createElement('option');
                        $objOption = $this->objSelect->appendChild($objOption);
                        $objOption->setAttribute('value',$aElement['value']);
      
                        $objOptionText = $this->objDomDocument->createTextNode($aElement['text']);
                        $objOptionText = $objOption->appendChild($objOptionText);                        
                  }
            }
else {
                  throw new exception('select::getSelectedOption() -> No data given in object!');
            }
      }

      /**
      * Calls all his members to actually create his select box
      **/

      private function createSelectBox() {
            $this->setSelectElement();
            $this->setSelectOptions();
      }

      /**
      * Get the whole object with a tree construction to add this object to others
      **/

      public function getDomObject() {
            $this->createSelectBox();
            return $this->objDomDocument;
      }

      /**
      * Function which builds the select box and gives you back the HTML
      **/

      public function getHTML() {
            $this->createSelectBox();
            return $this->objDomDocument->saveHTML();
      }
}

?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.