Ejemplo n.º 1
0
function table_keys_to_where_condition(&$row_data, $db_table_config, $use_table_name = FALSE)
{
    if (!is_array($db_table_config)) {
        die(__FUNCTION__ . ": need an array to work on \$db_table_config");
    }
    $table_keys_array = \k1lib\sql\get_db_table_keys($db_table_config);
    if (empty($table_keys_array)) {
        die(__FUNCTION__ . ": The is no PRI on the \$db_table_config");
    }
    $key_values = array();
    foreach ($table_keys_array as $column_name => $noused) {
        if (isset($row_data[$column_name])) {
            $key_values[$column_name] = $row_data[$column_name];
        }
    }
    $first_value = TRUE;
    $where_condition = "";
    foreach ($key_values as $key => $value) {
        //        if ($db_table_config[$key]['type'])
        if (!$first_value) {
            $where_condition .= " AND ";
        }
        if ($use_table_name) {
            $where_condition .= "{$db_table_config[$key]['table']}.{$key} = '{$value}'";
        } else {
            $where_condition .= "{$key} = '{$value}'";
        }
        $first_value = FALSE;
    }
    return $where_condition;
}
Ejemplo n.º 2
0
function get_labels_from_table($db, $table_name)
{
    \k1lib\sql\db_check_object_type($db, __FUNCTION__);
    if (!is_string($table_name) || empty($table_name)) {
        die(__FUNCTION__ . " \$table_name should be an non empty string");
    }
    $table_config_array = \k1lib\sql\get_db_table_config($db, $table_name);
    $label_field = \k1lib\sql\get_db_table_label_fields($table_config_array);
    $table_keys_array = \k1lib\sql\get_db_table_keys($table_config_array);
    if (!empty($table_keys_array)) {
        $table_config_array = array_flip($table_keys_array);
    }
    if (count($table_keys_array) === 1) {
        $key_filed = key($table_keys_array);
        $labels_sql = "SELECT {$key_filed} as value, {$label_field} as label FROM {$table_name}";
        $labels_data = \k1lib\sql\sql_query($db, $labels_sql);
        if (!empty($labels_data) && count($labels_data) > 0) {
            $label_array = array();
            foreach ($labels_data as $row) {
                $label_array[$row['value']] = $row['label'];
            }
            return $label_array;
        } else {
            return FALSE;
        }
    } else {
        return FALSE;
    }
}