gpt4 book ai didi

php - CodeIgniter set_value() 不工作?

转载 作者:可可西里 更新时间:2023-11-01 13:24:20 25 4
gpt4 key购买 nike

我正在开发一个使用 CI 验证规则的类别模块。当验证规则失败时,我希望表单具有与用户输入的值相同的值。验证规则失败后,我使用 set_value 函数加载表单中的值。但每次验证失败时,该值都会设置为空白。

这是我的代码。我已经在构造函数中设置了验证规则。

$this->form_validation->set_rules($this->validation_rules);
if ($this->input->post()) {
$db_array=array(
'CategoryName' => $this->input->post('CategoryName'),
'ParentID' => $this->input->post('ParentID'),
'Status' => $this->input->post('Status')
);
if($this->form_validation->run() != false){

if($id)
{
$result = $this->category_m->update($db_array,$id);
if(isset($result['error'])&& count($result['error']>0))
{
foreach($result['error'] as $error)
{
$this->session->set_flashdata('error',$error);
}
}
if($result['success'])
{
$action['class'] = 'success';
$action['msg'] = 'Data added successfully.';
$this->session->set_flashdata('success',$action);
}
else
{
$action['class'] = 'error';
$action['msg'] = 'Invalid values to the form!';
$this->session->set_flashdata('error',$action);
}
}
else
{
$result = $this->category_m->insert($db_array);
if(isset($result['error']) && count($result['error']>0))
{
foreach($result['error'] as $error)
{
$this->session->set_flashdata('error',$error);
}
}
if($result['success'])
{
$action['class'] = 'success';
$action['msg'] = 'Data added successfully.';
$this->session->set_flashdata('sucess',$action);
}
else
{
$action['class'] = 'error';
$action['msg'] = 'Invalid values to the form!';
$this->session->set_flashdata('error',$action);
}
}
if(isset($this->input->post['frmSubmit']))
{
redirect('category/form/'.$result['CategoryID']);
}
else
{
redirect('category/index');
}

}
else
{
$categories = $this->category_m->getCategoryName(true);
$data['categories'] = convert_to_dropdown($categories,'CategoryName','CategoryID','Select Parent Category');
$data['CategoryName'] = set_value('CategoryName');
$data['ParentID'] = set_value('ParentID');
$data['Status'] = set_value('Status');
}
}

if($id)
{
$category = $this->category_m->getCategory($id);
$data['CategoryName'] = $category->CategoryName;
$data['ParentID'] = $category->ParentID;
$data['Status'] = $category->Status;
}
else
{
$data['CategoryName'] = '';
$data['ParentID'] = '';
/*foreach($this->validation_rules as $rule){
//$data['category']->{$rule['field']} = set_value($rule['field']);
}*/
}
$categories = $this->category_m->getCategoryName(true, $id);
$data['categories'] = convert_to_dropdown($categories,'CategoryName','CategoryID','Select Parent Category');
$this->load->view('form',$data);

最佳答案

更改此代码的位置。

else 
{
$categories = $this->category_m->getCategoryName(true);
$data['categories'] = convert_to_dropdown($categories,'CategoryName','CategoryID','Select Parent Category');
$data['CategoryName'] = set_value('CategoryName');
$data['ParentID'] = set_value('ParentID');
$data['Status'] = set_value('Status');
}

作为

if($this->input->post()) 
{

$categories = $this->category_m->getCategoryName(true);
$data['categories'] = convert_to_dropdown($categories,'CategoryName','CategoryID','Select Parent Category');
$data['CategoryName'] = set_value('CategoryName');
$data['ParentID'] = set_value('ParentID');
$data['Status'] = set_value('Status');
}
else if($id)
{
$category = $this->category_m->getCategory($id);
$data['CategoryName'] = $category->CategoryName;
$data['ParentID'] = $category->ParentID;
$data['Status'] = $category->Status;
}
else
{
$data['CategoryName'] = '';
$data['ParentID'] = '';
}

一定会成功

关于php - CodeIgniter set_value() 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23557612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com