Perhaps my function may help you...
Use it like this:
<td class="label<?php if ($submit_new) echo ' required_txt'; ?>"><label
for="problem_category">Problem Category:</label></td>
<td><?php select_box_array(1, 'problem_category',
get_array('problem_category'), true, false, $_REQUEST['problem_category']);
?></td>
Or with the double_up_uh_uh() function to save redundant array key/values:
<td class="label"><label for="unit_opened">Unit opened for
investigation:</label></td>
<td><?php select_box_array(1, 'unit_opened', double_up_uh_uh(array('Yes',
'No')), true, false, $_REQUEST['unit_opened']); ?></td>
/**
* Dynamically generates a select box from a $key => $value array.
*
* the array can be generated on the fly or stored.
* the key is the OPTION value and the $value is the name shown in the box.
* if there is only a key, then that is used as the value too.
* very useful for boolean type listboxes too.
* also useful to save DB queries for supporting tables.
*
* Tooltips do NOT work in IE6. sorry. blame Microsoft for not following W3
standards...
*
* @access public
* @return mixed integer # of elements in select box or string of
actual select box if $echo == true.
* @param integer $size usually 1, but the select box can be any
height.
* @param string $name the NAME='$name' parameter of a SELECT tag.
* @param array $listArray The actual key => value formatted array.
* @param boolean $blank add the extra 'empty' <OPTION VALUE=''>.
* @param boolean $auto onChange will cause a form submit if true.
* @param string $MatchToThis sometimes it is useful to match $name to
something like $_GET['name'] instead. it is array safe too!
* @param string $extratags Any extra CLASS='' or MULTIPLE or whatever
to put in the <SELECT ...> tag.
* @param boolean $indent When using an array of arrays, the sub arrays
will be grouped using <OPTGROUP>. If you want to disable the indent when
$size is greater than 1, change this to false.
* @param boolean $echo Output the selectbox in place (true) or return
it as a string
* @see select_box_sql()
* @author Daevid Vincent [dae...@]
*/
function select_box_array($size=1, $name, $listArray, $blank = false, $auto
= false, $MatchToThis = false, $extratags = false, $indent = true, $echo =
true)
{
global $$name;
$items = 0;
if (intval($size) < 1) $size = 1;
if ($MatchToThis === false) $MatchToThis = $$name;
$out = '';
$out .= "\n<select size=\"".$size."\" name=\"".$name."\"
id=\"".$name."\"";
if ($auto) $out .= ' onChange="this.form.submit(); return true;"';
if ($extratags) $out .= " ".$extratags;
//if ($size > 1) $out .= ' onmouseover="selectBoxTitle(this);"';
$out .= ">\n";
if (count($listArray) > 0)
{
if( ! is_bool( $indent ) || ( ! $indent && ( $size == 1 ) )
) $indent = true;
if ($blank && is_bool($blank) ) { $out .= "\n<option
value=\"\"></option>\n"; }
elseif ($blank && is_string($blank)) { $out .= "\n<option
value=\"\" title=\"" . $blank . "\" ".((selected_if_in_array($MatchToThis,
'') || '' === $MatchToThis) ? 'selected' : '').">".$blank."</option>\n"; }
foreach($listArray as $key => $val)
{
// Check for selectbox sub-headings.
if( is_array($val) )
{
$out .= ( $indent ?
"\t<optgroup LABEL=\"" .
stripslashes( $key ) . "\">\n" :
"\t<option value=\"\"
CLASS=\"selectbox-ghosted\" disabled=\"disabled\">--- " . trim(
preg_replace( '/^-*(.*?)-*$/', '\\1', stripslashes( $key ) ) ) . "
---</option>\n");
foreach( $val as $subkey => $subval )
{
$out .= "\t\t<option value=\"" .
$subkey . "\" title=\"" . stripslashes( $subval ? $subval : $subkey ) .
"\"";
if
(selected_if_in_array($MatchToThis, $subkey) || $subkey == $MatchToThis)
$out .= ' selected="selected"';
if ($size > 1) $out .= '
onmouseover="this.title = this.text;"';
$out .= '>';
$out .= stripslashes( $subval ?
$subval : $subkey );
$out .= "</option>\n";
}
$out .= (($indent) ? "\t</optgroup>\n" :
'');
}
elseif( 0 == strncmp( $val, "---", 3 ) )
{
$out .= "<option value=\"\" disabled
class='selectbox-ghosted'>".stripslashes($val)."</option>\n";
}
else
{
$out .= "\t<option value=\"".$key."\"
title=\"" . stripslashes( (($val)?$val:$key) ) . "\"";
if (selected_if_in_array($MatchToThis, $key)
|| $key == $MatchToThis) $out .= " selected";
//if ($size > 1) $out .= "
title=\"".stripslashes($val)."\"";
if ($size > 1) $out .= '
onmouseover="this.title = this.text;"';
$out .= '>';
$out .= stripslashes( $val ? $val : $key );
$out .= "</option>\n";
}
} //foreach
} //if count(listArray)
$out .= "</select>\n";
if ($echo)
{
echo $out;
return count($listArray);
}
else return $out;
}
/**
* @return mixed 'selected' or false
* @param array $haystack the array that contains the selected items
* @param string $needle the item in the $haystack we're looking for.
* @author Daevid Vincent [dae...@]
*/
function selected_if_in_array(&$haystack, $needle)
{
if ( !is_array($haystack) ) return false;
return ( in_array($needle, $haystack) ) ? 'selected' : false;
}
/**
* Take an array of unique values and make the keys the same as the value.
* Useful for select boxes for example.
*
* @access public
* @return mixed
* @param array $myArray
* @author Daevid Vincent [dae...@]
*/
function double_up_uh_uh($myArray)
{
foreach($myArray as $k => $v)
$tmp[$v] = $v;
return $tmp;
}
/**
* Returns the first non-empty value in the array,
* Works much like mySQL's function,
* except this is an array passed in rather than a list of parameters.
*
* @access public
* @return mixed
* @param array $myArray
* @author Daevid Vincent [dae...@]
*/
function coalesce($myArray)
{
foreach($myArray as $a) if ($a) return $a;
}
> -----Original Message-----
> From: Don Wieland [mailto:[email protected]]
> Sent: Monday, November 09, 2009 2:59 PM
> To: [email protected]
> Subject: [PHP] Build Categories based on an static Array
>
> I am trying to build a SELECT MENU (with categories). I cant seem to
> get the syntax correct. Little help:
>
> // Get Areas values for MENU
> $AreasList = "<select name=\"Area_id\"><option
> value=''></option>";
>
> $queryR = array(0 => 'Domestic','International','Special');
> while($rowR = $queryR->fetch_assoc()) {
>
> $AreasList .= "<optgroup label=\"{$rowR['Region_Name']}\">";
> $Area_Type = $rowR['Area_Type'];
>
>
> $query2 = "SELECT Area_id,Area FROM Areas WHERE
> `Area_Type`='$Area_Type' ORDER BY Area";
> $results2 = $db->query($query2) or die("No Area found.");
>
> while($row2 = $results2->fetch_assoc()) {
> $AreasList .= "<option
> value='{$row2['Area_id']}'>{$row2['Area']}</option>";
> }
> $AreasList .= "</optgroup>";
> }
>
> $AreasList .= "</select>";
>
>
>
> Don Wieland
> D W D a t a C o n c e p t s
> ~~~~~~~~~~~~~~~~~~~~~~~~~
> [email protected]
> Direct Line - (949) 305-2771
>
> Integrated data solutions to fit your business needs.
>
> Need assistance in dialing in your FileMaker solution? Check out our
> Developer Support Plan at:
> http://www.dwdataconcepts.com/DevSup.html
>
> Appointment 1.0v9 - Powerful Appointment Scheduling for
> FileMaker Pro
> 9 or higher
> http://www.appointment10.com
>
> For a quick overview -
> http://www.appointment10.com/Appt10_Promo/Overview.html
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php