adaniel         Fri Mar 23 20:34:18 2001 EDT

  Modified files:              
    /php4/pear/HTML     Select.php 
  Log:
  added comments to the HTML output, added loadQuery, added load
  
Index: php4/pear/HTML/Select.php
diff -u php4/pear/HTML/Select.php:1.2 php4/pear/HTML/Select.php:1.3
--- php4/pear/HTML/Select.php:1.2       Thu Mar 22 22:24:07 2001
+++ php4/pear/HTML/Select.php   Fri Mar 23 20:34:17 2001
@@ -16,7 +16,7 @@
 // | Authors: Adam Daniel <[EMAIL PROTECTED]>                        |
 // +----------------------------------------------------------------------+
 //
-// $Id: Select.php,v 1.2 2001/03/23 06:24:07 adaniel Exp $
+// $Id: Select.php,v 1.3 2001/03/24 04:34:17 adaniel Exp $
 
 require_once "DB.php";
 require_once "PEAR.php";
@@ -26,7 +26,7 @@
  * Class to dynamically create an HTML SELECT
  *
  * @author       Adam Daniel <[EMAIL PROTECTED]>
- * @version      1.0
+ * @version      1.1
  * @since        PHP4.04pl1
  * @access       public
  */
@@ -54,13 +54,13 @@
     /**
      * Class constructor
      *
-     * @param     string    $name       Name attribute of the SELECT
-     * @param     int       $size       Size attribute of the SELECT
-     * @param     bool      $multiple   Whether the select will allow multiple 
+     * @param     string    $name       (optional)Name attribute of the SELECT
+     * @param     int       $size       (optional) Size attribute of the SELECT
+     * @param     bool      $multiple   (optional)Whether the select will allow 
+multiple 
      *                                  selections or not
-     * @param     mixed     $attributes Either a typical HTML attribute string 
+     * @param     mixed     $attributes (optional)Either a typical HTML attribute 
+string 
      *                                  or an associative array
-     * @param     int       $tabOffset  Number of tabs to offset HTML source
+     * @param     int       $tabOffset  (optional)Number of tabs to offset HTML source
      * @since     1.0
      * @access    public
      * @return    void
@@ -86,7 +86,7 @@
      */
     function apiVersion()
     {
-        return 1.0;
+        return 1.1;
     } //end func apiVersion
 
     /**
@@ -140,9 +140,6 @@
         }
         $attributes = $this->_parseAttributes($attributes);
         $attr = array("value"=>$value);
-        if (in_array($value, $this->_values)) {
-            $attr[] = "SELECTED";
-        }
         $this->_updateAttrArray($attributes, $attr);
         $this->_options[] = array("text"=>$text, "attr"=>$attributes);
     } // end func addOption
@@ -166,11 +163,7 @@
             $this->setSelectedValues($values);
         }
         while (list($key, $value) = each($arr)) {
-            if (in_array($value, $this->_values)) {
-                $this->addOption($key, $value, true);
-            } else {
-                $this->addOption($key, $value);
-            }
+            $this->addOption($key, $value);
         }
         return true;
     } // end func loadArray
@@ -188,7 +181,7 @@
      * @return    PEAR_Error on error or true
      * @throws    PEAR_Error
      */
-    function loadDbResult(&$result, $textCol="", $valueCol="", $values=null)
+    function loadDbResult(&$result, $textCol=null, $valueCol=null, $values=null)
     {
         if (!is_object($result) || (get_class($result) != "db_result" && 
             is_subclass_of($result, "db_result"))) {
@@ -200,22 +193,77 @@
         $fetchMode = ($textCol && $valueCol) ? DB_FETCHMODE_ASSOC : 
DB_FETCHMODE_DEFAULT;
         while (is_array($row = $result->fetchRow($fetchMode)) ) {
             if ($fetchMode == DB_FETCHMODE_ASSOC) {
-                if (in_array($row[$valueCol], $this->_values)) {
-                    $this->addOption($row[$textCol], $row[$valueCol], true);
-                } else {
-                    $this->addOption($row[$textCol], $row[$valueCol]);
-                }
+                $this->addOption($row[$textCol], $row[$valueCol]);
             } else {
-                if (in_array($row[0], $this->_values)) {
-                    $this->addOption($row[0], $row[1], true);
-                } else {
-                    $this->addOption($row[0], $row[1]);
-                }
+                $this->addOption($row[0], $row[1]);
             }
         }
         return true;
     } // end func loadDbResult
+    
+    /**
+     * Queries a database and loads the options from the results
+     *
+     * @param     mixed     $conn       Either an existing DB connection or a valid 
+dsn 
+     * @param     string    $sql        SQL query string
+     * @param     string    $textCol    (optional) Name of column to display as the 
+OPTION text 
+     * @param     string    $valueCol   (optional) Name of column to use as the 
+OPTION value 
+     * @param     mixed     $values     (optional) Array or comma delimited string of 
+selected values
+     * @since     1.1
+     * @access    private
+     * @return    void
+     * @throws    
+     */
+    function loadQuery(&$conn, $sql, $textCol=null, $valueCol=null, $values=null)
+    {
+        if (is_string($conn)) {
+            $dbConn = &DB::connect($conn, true);
+            if (DB::isError($dbConn)) return $dbConn;
+        } elseif (is_subclass_of($conn, "db_common")) {
+            $dbConn = $conn;
+        } else {
+            return new PEAR_Error("Argument 1 of HTML_Select::loadQuery is not a 
+valid type");
+        }
+        $result = @$dbConn->query($sql);
+        if (DB::isError($result)) return $result;
+        return $this->loadDbResult($result, $textCol, $valueCol, $values);
+    } // end func loadQuery
 
+     // I DON'T IF THERE IS A REAL BENEFIT FROM HAVING THIS METHOD BUT I WANTED TO 
+TRY IT
+    /**
+     * Loads options from different types of data sources
+     *
+     * This method is a simulated overloaded method.  The arguments, other than the
+     * first are optional and only mean something depending on the type of the first 
+argument.
+     * If the first argument is an array then all arguments are passed in order to 
+loadArray.
+     * If the first argument is a db_result then all arguments are passed in order to 
+loadDbResult.
+     * If the first argument is a string or a DB connection then all arguments are 
+     * passed in order to loadQuery.
+     * @param     mixed     $options     Options source currently supports assoc 
+array or DB_result
+     * @param     mixed     $param1     (optional) See function detail
+     * @param     mixed     $param2     (optional) See function detail
+     * @param     mixed     $param3     (optional) See function detail
+     * @param     mixed     $param4     (optional) See function detail
+     * @since     1.1
+     * @access    public
+     * @return    PEAR_Error on error or true
+     * @throws    PEAR_Error
+     */
+    function load(&$options, $param1=null, $param2=null, $param3=null, $param4=null)
+    {
+        switch (true) {
+            case is_array($options):
+                return $this->loadArray($options, $param1);
+                break;
+            case (get_class($options) == "db_result" || is_subclass_of($options, 
+"db_result")):
+                return $this->loadDbResult($options, $param1, $param2, $param3);
+                break;
+            case (is_string($options) || is_subclass_of($options, "db_common")):
+                return $this->loadQuery($options, $param1, $param2, $param3, $param4);
+                break;
+        }
+    } // end func load
+    
     /**
      * Returns the SELECT in HTML
      *
@@ -227,16 +275,31 @@
     function toHtml()
     {
         $tabs = $this->_getTabs();
+        $name = $this->_attributes["name"];
         $strHtml =
-            "\n" .$tabs . "<SELECT" . $this->_getAttrString($this->_attributes) . 
">\n";
+            "\n" . $tabs . "<!-- BEGIN SELECT $name -->\n";
+        if ($this->_comment) {
+            $strHtml .= $tabs . "<!-- $this->_comment -->\n";
+        }
+        $strHtml .=
+            $tabs . "<SELECT" . $this->_getAttrString($this->_attributes) . ">\n" .
+            $tabs . "\t<!-- BEGIN OPTIONS $name -->\n";
         for ($counter=0; $counter < count($this->_options); $counter++) {
+            $value = $this->_options[$counter]["attr"]["value"];
+            $attrString = $this->_getAttrString($this->_options[$counter]["attr"]);
+            if (in_array($value, $this->_values)) {
+                $attrString = " SELECTED" . $attrString;
+            }
             $strHtml .=
-                $tabs . "\t<OPTION" . 
$this->_getAttrString($this->_options[$counter]["attr"]) . ">" .
+                $tabs . "\t<OPTION" . $attrString . ">" .
                 $this->_options[$counter]["text"] . "\n";
         }
-        $strHtml .= $tabs . "</SELECT>";
+        $strHtml .= 
+            $tabs . "\t<!-- END OPTIONS $name -->\n" .
+            $tabs . "</SELECT><!-- END SELECT $name -->";
         return $strHtml;
     } // end func toHtml
     
 } // end class HTML_Select
 ?>
+

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to