Php 使用代码插入器进行服务器端验证

Php 使用代码插入器进行服务器端验证,php,codeigniter,validation,post,insert,Php,Codeigniter,Validation,Post,Insert,我是新来的编码员。这与我过去的编码经验非常不同,我现在想做的是将数据发布到服务器并进行检查。由于输入框是动态生成的,这使得我的添加功能非常混乱 首先,对于每个数据,它必须有1个标题和1个内容,并随机编号(或无)一个链接组(链接文本和链接),例如链接文本1、链接1、链接文本2、链接2……等等 我想用更优雅的方式来做这件事 我现在做的是在模型中有2个方法,1个用于标题和内容,在感兴趣之后,返回最后一个id,根据这个id附加所有其他链接组 public function add() { //i

我是新来的编码员。这与我过去的编码经验非常不同,我现在想做的是将数据发布到服务器并进行检查。由于输入框是动态生成的,这使得我的添加功能非常混乱

首先,对于每个数据,它必须有1个标题和1个内容,并随机编号(或无)一个链接组(链接文本和链接),例如链接文本1、链接1、链接文本2、链接2……等等

我想用更优雅的方式来做这件事

我现在做的是在模型中有2个方法,1个用于标题和内容,在感兴趣之后,返回最后一个id,根据这个id附加所有其他链接组

public function add()
{
    //if save button was clicked, get the data sent via post
    if ($this->input->server('REQUEST_METHOD') === 'POST')
    {
        //form validation
        $this->form_validation->set_rules('title', '消息標題', 'required');
        $this->form_validation->set_rules('content', '消息內容', 'required');
        foreach ($this->input->post() as $key => $value) {
            if (strstr($key,"linkText") !== False) {
                $this->form_validation->set_rules($key, '連結標題', 'required');
            }
            else if (strstr($key,"link") !== False) {
                 $this->form_validation->set_rules($key, '連結地址', 'required');
            }
        }
        $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');

        //if the form has passed through the validation
        if ($this->form_validation->run())
        {
            $data_to_store = array(
                'title' => $this->input->post('title'),
                'content' => $this->input->post('content')
            );

            //if the insert has returned true then we show the flash message
            $lastID = $this->news_model->store_news($data_to_store);

            if($lastID !== False){
                foreach ($this->input->post() as $key => $value) {
                    if (strstr($key,"linkText") !== False) {
                        $linkText = $key;
                    }else if (strstr($key,"link") !== False) {
                        $link = $key;
                    }

                    if (isset($linkText) && isset($link)) {
                        $data_to_store = array(
                        'title' => $this->input->post($linkText),
                        'url' => $this->input->post($link),
                        'news_id' => $lastID
                        );

                        if($this->news_model->store_news_link($data_to_store)){
                            $data['flash_message'] = TRUE;
                        } else {
                            $data['flash_message'] = FALSE; 
                        }
                    }
                }
            }else{
                $data['flash_message'] = FALSE; 
            }
        }else{
                $data['flash_message'] = FALSE; 
        }
    }   
    //load the view
    $data['main_content'] = 'admin/news/add';
    $this->load->view('includes/template', $data);
}    
公共函数添加()
{
//如果单击了“保存”按钮,则获取通过post发送的数据
如果($this->input->server('REQUEST\u METHOD')=='POST')
{
//表单验证
$this->form\u validation->set\u rules('title','消息標題', '必需的“);
$this->form\u validation->set\u rules('内容','消息內容', '必需的“);
foreach($this->input->post()作为$key=>$value){
if(strstr($key,“linkText”)!==False){
$this->form\u validation->set\u rules($key,'連結標題', '必需的“);
}
else if(strstr($key,“link”)!==False){
$this->form\u validation->set\u rules($key,'連結地址', '必需的“);
}
}
$this->form_validation->set_error_分隔符(“×”,“”);
//如果表单已通过验证
如果($this->form\u validation->run())
{
$data\u to\u store=数组(
'title'=>this->input->post('title'),
'content'=>this->input->post('content')
);
//如果insert返回true,则显示flash消息
$lastID=$this->news\u model->store\u news($data\u to\u store);
如果($lastID!==False){
foreach($this->input->post()作为$key=>$value){
if(strstr($key,“linkText”)!==False){
$linkText=$key;
}else if(strstr($key,“link”)!==False){
$link=$key;
}
if(isset($linkText)&&isset($link)){
$data\u to\u store=数组(
'title'=>this->input->post($linkText),
'url'=>this->input->post($link),
'news\u id'=>$lastID
);
如果($this->news\u model->store\u news\u link($data\u to\u store)){
$data['flash_message']=TRUE;
}否则{
$data['flash_message']=FALSE;
}
}
}
}否则{
$data['flash_message']=FALSE;
}
}否则{
$data['flash_message']=FALSE;
}
}   
//加载视图
$data['main_content']='admin/news/add';
$this->load->view('includes/template',$data);
}    
它循环了2次,并重复了许多代码,有没有更优雅的方法来实现这一点?谢谢您的帮助。

您可以删除

if ($this->input->server('REQUEST_METHOD') === 'POST')
因为

$this->form_validation->run()
她正在照顾它

也请去掉很多

$data['flash_message'] = FALSE;
只需将该行放在函数的开头,然后在需要时将其更改为true。把它复制粘贴到代码上是没有意义的

我不确定我是否理解这个问题,但我认为您正在寻找像这样的输入数组

<input name="myarray[]" ... />


CodeIgniter可以验证这些,请参见

谢谢提醒。更新