<?php

function cmb_dinamis1($name, $id_combo, $opsi_name, $table, $field, $pk, $selected)
{
    $ci = get_instance();
    $cmb = "<select name='$name' id='$id_combo' class='default-select2 form-control' style='width : 100%;' required>";
    $cmb .= "<option value=''>-Pilih $opsi_name-</option>";
    $data = $ci->db->get($table)->result();
    foreach ($data as $d) {
        $cmb .= "<option value='" . $d->$pk . "'";
        $cmb .= $selected == $d->$pk ? " selected='selected'" : '';
        $cmb .= ">" .  $d->$field . "</option>";
    }
    $cmb .= "</select>";
    return $cmb;
}

function cmb_dinamis2($name, $id_combo, $opsi_name, $table, $field, $field2, $pk, $selected)
{
    $ci = get_instance();
    $cmb = "<select name='$name' id='$id_combo' class='form-control'>";
    $cmb .= "<option value=''>-Pilih $opsi_name-</option>";
    $data = $ci->db->get($table)->result();
    foreach ($data as $d) {
        $cmb .= "<option value='" . $d->$pk . "'";
        $cmb .= $selected == $d->$pk ? " selected='selected'" : '';
        $cmb .= ">" .  $d->$field . " , " .  $d->$field2 . "</option>";
    }
    $cmb .= "</select>";
    return $cmb;
}

function cmb_dinamis3($name, $id_combo, $opsi_name, $table, $field, $pk, $selected)
{
    $ci = get_instance();
    $cmb = "<select name='$name' id='$id_combo' class='default-select2 form-control' style='width : 100%;'>";
    $cmb .= "<option value=''>-Pilih $opsi_name-</option>";
    $data = $ci->db->get($table)->result();
    foreach ($data as $d) {
        $cmb .= "<option value='" . $d->$pk . "'";
        $cmb .= $selected == $d->$pk ? " selected='selected'" : '';
        $cmb .= ">" .  $d->$field . "</option>";
    }
    $cmb .= "</select>";
    return $cmb;
}


function cmb_enum($name, $table, $field, $selected)
{
    $ci = get_instance();
    $query = "SHOW COLUMNS FROM " . $table . " LIKE '$field'";
    $row = $ci->db->query("SHOW COLUMNS FROM " . $table . " LIKE '$field'")->row()->Type;
    $regex = "/'(.*?)'/";
    preg_match_all($regex, $row, $enum_array);
    $enum_fields = $enum_array[1];

    $cmb = "<select name='$name' id='$name' class='default-select2 form-control' style='width : 100%;'>";
    $cmb .= "<option value=''>-Pilih-</option>";
    foreach ($enum_fields as $key => $value) {
        $enums[$value] = $value;
        $cmb .= "<option value='" . $value . "'";
        $cmb .= $selected == $value ? " selected='selected'" : '';
        $cmb .= ">" .  $value . "</option>";
    }
    $cmb .= "</select>";
    return $cmb;
}
