> From: Nathan Cook [mailto:[EMAIL PROTECTED]] > Sent: Thursday, July 25, 2002 12:34 PM > Subject: [PHP] New way to make select boxes auto select > > > You may already be doing it like this, but I think I found a > new way to > make select boxes auto-select (what data they put in) a lot > easier. All you > have to do is put a variable in each select tag that is equal > to the value > of the select option i.e.: <option value=teacher $teacher> -- > then all you > have to do is base the variable on that <select > name=interest> $$interest = > "selected"; quick and easy with out having to loop through an > if elseif > statement. Let me know if you like that method or have any > objections.
I use these functions which do something similar. The first function accepts an array of names for the option, and returns a string of all of the <options> but not the <select> tags. The ones to be selected have a value of 'selected'. The second function accepts a code table, of say states or provinces, and a value that is to be selected, and returns the <options>. Both are only for single selected values, but could be easily extended. Usage: // ------------------------------------------------------------------------------------------- // buildSelect -- return a Select box named $selectName based on key value array $selectArray //<select name="states"> // <?php // $arr = array('MD'=>'selected','DC'=>'','VA'=>''); // echo buildSelect($arr); // ?> // </select> // ------------------------------------------------------------------------------------------- function buildSelect($selectArray) { $str = ''; $count = count($selectArray); for ($i=0;$i<$count;++$i) { list($key,$selected) = each($selectArray[$i]); $selectValue = htmlspecialchars($key); $str .= "<option value=\"$selectValue\" $selected>$key</option>\n"; } return($str); } // ------------------------------------------------------------------------------------------- // setupSelect -- Read table $table from database $db, load all values of $field, and build // a select box from this list named $selName // <select name="states"> // <?php // echo setupSelect($db,'states','stateAbbr','stateName','MD'); // ?> // </select> // ------------------------------------------------------------------------------------------- function setupSelect($db,$table,$field,$orderBy,$value) { $sql = "SELECT " . $field . " from " . $table; if (empty($orderBy)) { $orderBy = $field; } $sql .= " order by " . $orderBy; $result = mysql_query($sql,$db) or die(log_mysql_error($sql,__FILE__,__LINE__)); while ($row = mysql_fetch_array($result)) { $columnData = $row[$field]; if (!empty($columnData)) { $selected = ($columnData == $value ? "selected" : ""); $selArray[] = array($columnData => $selected); } } if (count($selArray) != 0) { return(buildSelect($selArray)); } } // ------------------------------------------------------------------------- // logs to syslog define the constant DISPLAY_MYSQL_ERRORS as 1 // if you want to see errors in browser too // usage: $result = mysql_query($sql,$db) // or die(log_mysql_error($sql,__FILE__,__LINE__)); // ------------------------------------------------------------------------- function log_mysql_error($sql='',$file='',$line=0) { $msg="{$_SERVER['PHP_SELF']}: (FILE=$file) (LINE=$line)\n"; $msg .= "MySQL Says: " . mysql_error() . "\n"; if (!empty($sql)) { $msg .= "SQL Statement was: $sql"; } error_log($msg,0); if (1 == DISPLAY_MYSQL_ERRORS) { $msg=nl2br($msg); echo "<br>$msg<br>\n"; } exit; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php