db connection must exist, database must be selected.
<?
#
# this class generates form element <select> as drop-down list
# where human readable options are in list and correct id is
# placed in hidden id field ...
#
# input parameters are: query string, name of the form,
# select element name, id element name & tabindex.
# query string must return result where at least row0 = id and row1 = name.
#
# usage init class and call it with:
# $classref->make_js($querys, $formname, $elementname, $hiddenname, $tabindex);
# example:
# $wact13_jsh->make_js("select id, nam from kb order by nam", "act13", "kli", "klid", 1);
#
# use it at your own risk, code is free, enjoy :)
# Tarmo Randel
# [email protected]
#
class js_create {
var $qr, $qnr;
# inner function, do not access directly
function gen_js ($form, $selname, $idname)
{
?>
<script language="javascript">
<!--
function doSet<? echo sprintf("%s", $idname); ?>Value() {
<?
if ($this->qnr > 0)
{
for ($i=1; $i <= $this->qnr; $i++) # this loop generates javascript
{
mysql_data_seek ($this->qr, $i-1);
$fr = mysql_fetch_row ($this->qr);
if ($i > 1) { echo "else ";}
?>
if (document.<? echo sprintf("%s", $form); ?>.<? echo sprintf("%s", $selname);
?>.options[<? echo sprintf("%s", $i-1); ?>].selected)
{
document.<? echo sprintf("%s", $form); ?>.<? echo sprintf("%s", $idname);
?>.value = "<? echo sprintf("%s", $fr[0]); ?>";
}
<?
}
}
?>
return true;
}
// -->
</script>
<?
}
# inner function, do not access directly
function gen_sel ($selname, $idname, $tabidx)
{
?>
<select name="<? echo sprintf("%s", $selname); ?>"
onChange="doSet<? echo sprintf("%s", $idname); ?>Value()"
tabindex="<? echo sprintf("%s", $tabidx); ?>">
<?
if ($this->qnr > 0)
{
for ($i=1; $i <= $this->qnr; $i++) # this loop makes select element
{
mysql_data_seek ($this->qr, $i-1);
$fr = mysql_fetch_row ($this->qr);
# I needed to limit element length ... so i set it to 21 (and that was fine for me ...)
if (strlen($fr[1]) > 21)
{
$s2shw = substr($fr[1], 0, 21);
} else
$s2shw = $fr[1];
echo sprintf("<option>%sn", $s2shw);
}
}
?>
</select>
<input type="hidden" name="<? echo sprintf("%s", $idname); ?>"
value="<?
if ($this->qnr > 0)
{
mysql_data_seek ($this->qr, 0);
$fr = mysql_fetch_row ($this->qr);
echo sprintf("%s", $fr[0]);
} ?>" size="5" maxlength="5">
<?
}
# main function. use this :)
function make_js ($qs, $form, $selname, $idname, $tabidx)
{
$this->qr = mysql_query($qs);
$this->qnr = mysql_numrows($this->qr);
$this->gen_js ($form, $selname, $idname);
$this->gen_sel ($selname, $idname, $tabidx);
}
# end of class
}
?>