weetat wrote:
Hi all ,
I have a SELECT for country in the php file . I would like to load
the country and city belong to the country dynamically from the database.
Anyone have ideas how to do it ?
below is a function that generates a select tag based on the
criteria you give it, the first argument, $items, you give it is the most
important - it should contain the data used to populate the list where by each
key in the array is the value of an OPTION and the corresponding value in
the array is the display text of corresponding OPTION.
to create the $items array you would perform a query on your database and
loop the results, e.g. (untested query code)
$res = mysql_query('SELECT id, name FROM foo ORDER BY name');
if ($res mysql_numrows($res)) {
while ($row = mysql_fetch_assoc($res)) {
$items[ $row['id'] ] = $row['name']
}
}
here is the function (linewrapping in the email may have screwed the code layout
a bit) - this function is quite old and by no means perfect (Richard Lynch gave
good
argument as to the possible 'cons' of such a fucntion, but nonetheless here it
is):
/** selectTag()
* generate an html select list using the given $items as options.
*
* @param array $items - list of items to display as options
* @param array $selected - value of selected item in $items
* @param string $selectname - value of select tag's name attribute
* @param numeric $addnone- add a 'blank' option to the top of the list
(different types depending on value passed)
* @param string $onchange - value of select tag's onchange attribute
* @param string $selectid - value of select tag's id attribute
* @param boolean $readonly - whether the select tag is readsonly or not
* @param boolean $option_class_arr - optional CSS class names for
individual option tags
* @param string $selectHTMLattribs - passthru HTML (attributes for the
select tag)
*
* @return string
*/
function selectTag($items, $selected = '', $selectname = '', $addnone = 0,
$onchange = '', $selectid = '', $readonly =
0, $option_class_arr = array(), $selectHTMLattribs = '')
{
// must have an array of items
if (!is_array($items)) {
return '';
}
// determine the value of the selected item
$selected = strtolower(trim($selected));
// list of options we will generate
$opts = array();
// add item with value 0 ?
$add_none_value = 0;
switch ( $addnone ) {
case '1': // item display = 'none'
$zero_id_item = getStr('None');
break;
case '3': // item display = '0'
$zero_id_item = getStr('zero_digit');
break;
case '2': // item display = 'all'
case '4': // item display = 'all digit' e.g. 'all 3'
$zero_id_item = getStr('All');
break;
default:
if(!empty($addnone) $addnone != '0') {
if(is_array($addnone)) {
list($add_none_value, $zero_id_item) = @each($addnone);
} else {
// here we can drop in a custom 'blank' item:
$zero_id_item = $addnone;
}
}
break;
}
if (isset($zero_id_item)) {
$thisselected = ($selected '') ? '' : ' selected=selected';
// FIX THE NEXT LINE - BUT NOT IMPORTANT
$class_var = (false
isset($option_class_arr[IAJ_OPTION_ZERO_ITEM_CLASS_VALUE] )) ? ' class=' .
$option_class_arr[IAJ_OPTION_ZERO_ITEM_CLASS_VALUE] . '' : '';
$opts[] = 'option value='.htmlentities($add_none_value,
ENT_QUOTES).'
'.$thisselected.$class_var.''.$zero_id_item.'/option';
}
$thisselected = '';
foreach ($items as $value = $displayname) {
$thisselected = ($selected '' $selected == strtolower($value)) ? '
selected=selected' : '';
$class_var= (isset($option_class_arr[$value])) ? ' class=' .
$option_class_arr[$value] . ' ' : '';
$opts[] = 'option value='.htmlentities($value,
ENT_QUOTES).''.$thisselected.$class_var.''.$displayname.'/option';
}
// various select tag attribute values
$onchange = (($onchange = trim($onchange)) '')
? onchange=\$onchange\
: ''
;
$id = ($selectid = strval($selectid))
? id=\{$selectid}\
: ''
;
$readonly = ($readonly == 1)
? ' readonly=readonly'
: ''
;
return select name=\{$selectname}\
{$selectHTMLattribs}{$id}{$onchange}{$readonly}\n.
join(\n\t, $opts).
\n/select;
}
Thanks
- weetat
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php