CI set_value不适用于获取表单方法



我搜索了很多,但找不到问题的答案。set_value()函数在GET表单方法上不起作用,而它正在处理POST 。我正在尝试从MySQL获取行,我想为此使用 GET 方法。请问谁能帮忙?

视图:

<?=form_open('employee_registration/search_employee', ['role'=>'form','method'=>'GET']);?>
    <div class="row">
        <div class="col-sm-3">
            <div class="form-group">
                <label>R. No.<span class="required">*</span></label>
                <input class="form-control" placeholder="R. No." name="srch_r_no" id="srch_r_no" value="<?=set_value('srch_r_no');?>">
            </div>
        </div>
        <div class="col-sm-3">
            <div class="form-group">
                <label>Name<span class="required">*</span></label>
                <input class="form-control" placeholder="Name" name="srch_e_name" id="srch_e_name" value="<?=set_value('srch_e_name');?>">
            </div>
        </div>
    </div>
    <button type="submit" class="btn btn-primary" name="srch_btn">Search <span class="fa fa-search"></span></button>
<?=form_close();?>

控制器:

public function search_employee()
{
    $get = $this->input->get();
    unset($get['srch_btn']);
    $table = $this->md_emp_reg->search_employee($get);
    $this->index('view',$table);
}

型:

public function search_employee($get)
{
    $parameters = $this->filter_values($get,"search");
    $query = "CALL `sp_select_employee_details`(".$parameters.");";
    $grid = $this->table($query);
    return $grid;
}
public function filter_values($arr,$type)
{
    $arr_keys = array_keys($arr);
    $filtered_values = "";
    for ($i=0; $i < count($arr); $i++)
    {
        if ($arr[$arr_keys[$i]] == "" && $type == "search")
        {
            $arr[$arr_keys[$i]] = "";
        }
        elseif($arr[$arr_keys[$i]] == "" && $type != "search")
        {
            $arr[$arr_keys[$i]] = NULL;
        }
        $filtered_values .= $this->db->escape($arr[$arr_keys[$i]]).",";
    }
    return rtrim($filtered_values,',');
}
public function table($query)
{
    $result = $this->db->query($query);
    $r_arr = $result->result_array();
    $result_fields_name = $result->list_fields();
    $result_num_fields = $result->num_fields();
    $result_num_rows = $result->num_rows();
    $table = '<table width="100%" class="table table-condensed table-striped table-bordered table-hover" id="dataTables-example"><thead><tr>';
    for ($a=0; $a < $result_num_fields; $a++)
    { 
        $table .= "<th>".$result_fields_name[$a]."</th>";
    }
    $table .="</tr></thead><tbody>";
    for ($i=0; $i < count($r_arr) ; $i++)
    {
        $table .= "<tr>";
        for ($j=0; $j < $result_num_fields ; $j++)
        {
            $table .= "<td>".$r_arr[$i][$result_fields_name[$j]]."</td>";
        }
        $table .= "</tr>";
    }
    $table .="</tbody></table>";
    $this->db->close();
    return $table;
}

这是函数:

function set_value($field, $default = '', $html_escape = TRUE)
{
    $CI =& get_instance();
    $value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
        ? $CI->form_validation->set_value($field, $default)
        : $CI->input->post($field, FALSE);
    isset($value) OR $value = $default;
    return ($html_escape) ? html_escape($value) : $value;
}

删除form_validation逻辑,因为它对我们没有用,我们得到:

function set_value($field, $default = '', $html_escape = TRUE)
    {
        $CI =& get_instance();
        $value = $CI->input->post($field, FALSE);
        isset($value) OR $value = $default;
        return ($html_escape) ? html_escape($value) : $value;
    }

交换 post 我们可以创建自己的帮助程序set_value_get(),将其保存在帮助程序中,加载帮助程序等:

function set_value_get($field, $default = '', $html_escape = TRUE)
    {
        $CI =& get_instance();
        $value = $CI->input->get($field, FALSE);
        isset($value) OR $value = $default;
        return ($html_escape) ? html_escape($value) : $value;
    }

用法将与set_value相同。

最新更新