コード例 #1
0
ファイル: widgets.php プロジェクト: horrabin/opendb
/**
	@param $name
	@param $lookup_results	This can be a MySQL $results reference or an PHP associative
							array.  Even if you index your array numerically, the 'value'
							and index still get set inside this function.
	@param $display_mask
			This mask can be anything.  Any %??????% will be interpreted as a database
			column name and will be selected from there as appropriate.
	@param $lookup_results	Database results mysql reference
	@param $size			Size of SELECT object.  Specify a size of 'NA' to stop the 
	 						<select ...> and </select> tags being generated.  This will allow
	 						things like empty options to be included, while still taking
	 						advantage of the generation of database/array options.
	@param $value			Checked Ind value.  If the value param is found in the current
							'value' column, that record will be SELECTED.
	@param $value_column	Specifies the value column, or 'value' as a default.
	@param $checked_ind 	Specifies the checked_ind column to select a default record, or
							'' as default, which will mean the first record will be selected
							by the browser when building the generated select.
	@param $include_ind_func
							If defined, will call the function with the current lookup array
 						    as argument.  If the function returns TRUE, the record will be
							included, otherwise it will be skipped.
	
	However the $lookup_results DO have to include a 'value' column for this
	process to work, as this will be used for the value of each select option.

	You can however set the $value_column to a value indicating a column name to
	be used instead of 'value'
*/
function custom_select($name, $lookup_results, $display_mask, $size = 1, $value = NULL, $value_column = 'value', $include_ind_func = NULL, $checked_ind = '', $onchange_event = '', $disabled = FALSE, $id = NULL)
{
    // allows function to be called with an array of args, instead of individual arguments.
    if (is_array($name)) {
        extract($name);
    }
    if ($size !== 'NA') {
        if (is_numeric($size) && $size > 1) {
            $var = "\n<select " . ($id != NULL ? "id=\"{$id}\"" : "") . " name=\"" . $name . "[]\" size=\"{$size}\" onchange=\"{$onchange_event}\"" . ($disabled ? ' DISABLED' : '') . " MULTIPLE>";
        } else {
            $var = "\n<select " . ($id != NULL ? "id=\"{$id}\"" : "") . " name=\"{$name}\" onchange=\"{$onchange_event}\"" . ($disabled ? ' DISABLED' : '') . ">";
        }
    } else {
        $var = '';
    }
    $lookup_results = fetch_results_array($lookup_results);
    reset($lookup_results);
    $empty_display_mask = expand_display_mask($display_mask, NULL, '%');
    $value_found = FALSE;
    while ($lookup_r = each($lookup_results)) {
        // Check if this record should be included in list of values.
        if (!function_exists($include_ind_func) || $include_ind_func($lookup_r)) {
            $lookup_value = get_array_variable_value($lookup_r, $value_column);
            $display = expand_display_mask($display_mask, $lookup_r, '%');
            // if all variables were replaced with nothing, then assume empty option
            if (strlen(strval($lookup_value)) == 0 && $display == $empty_display_mask) {
                $display = '';
            }
            if (is_array($value)) {
                if (in_array($lookup_value, $value) !== FALSE) {
                    $var .= "\n<option value=\"" . $lookup_value . "\" SELECTED>{$display}";
                } else {
                    $var .= "\n<option value=\"" . $lookup_value . "\">{$display}";
                }
            } else {
                if (!$value_found && $value == NULL && $lookup_r[$checked_ind] == 'Y') {
                    $var .= "\n<option value=\"" . $lookup_value . "\" SELECTED>{$display}";
                } else {
                    if (strcasecmp(trim($value), strval($lookup_value)) === 0) {
                        $value_found = TRUE;
                        $var .= "\n<option value=\"" . $lookup_value . "\" SELECTED>{$display}";
                    } else {
                        $var .= "\n<option value=\"" . $lookup_value . "\">{$display}";
                    }
                }
            }
        }
    }
    if ($size !== 'NA') {
        $var .= "\n</select>";
    }
    return $var;
}
コード例 #2
0
ファイル: inputfields.php プロジェクト: horrabin/opendb
function process_lookup_results($lookup_results, $value)
{
    if (is_array($value) && count($value) > 0) {
        $values_r = $value;
    } else {
        if (!is_array($value) && $value !== NULL) {
            // if a single string value, convert to single element array.
            $values_r[] = $value;
        } else {
            // is_empty_array!
            $values_r = NULL;
        }
    }
    $lookup_rs = fetch_results_array($lookup_results);
    $value_found = FALSE;
    while (list(, $lookup_r) = each($lookup_rs)) {
        if (is_array($values_r) && ($lookup_key = array_search2($lookup_r['value'], $values_r, TRUE)) !== FALSE) {
            $value_found = TRUE;
            break;
        }
    }
    $lookup_val_rs = array();
    reset($lookup_rs);
    while (list(, $lookup_r) = each($lookup_rs)) {
        if ($value_found) {
            $lookup_r['checked_ind'] = 'N';
        }
        if (is_array($values_r) && ($lookup_key = array_search2($lookup_r['value'], $values_r, TRUE)) !== FALSE) {
            $lookup_r['checked_ind'] = 'Y';
            // Remove the matched element
            array_splice($values_r, $lookup_key, 1);
        }
        $lookup_val_rs[] = $lookup_r;
    }
    if (is_array($values_r)) {
        // Add the value to the list of options and select it.
        reset($values_r);
        while (list(, $value) = each($values_r)) {
            if (strlen($value) > 0) {
                $lookup_val_rs[] = array(value => $value, checked_ind => 'Y');
            }
        }
    }
    return $lookup_val_rs;
}