php代码在两个选定年份之间插入所有年份

时间:2016-08-18 08:47:13

标签: php cakephp

我正在使用cakephp ..

我的表单中有 fromyear ToYear 字段

我想如果我选择年= 2013年和年年= 2016年

然后在数据库中插入3行,如下例所示。

 fromyear   
     2013 
     2014
     2015
     2016    

        like wise...

        please help me to get above scenario..  

我试过下面的代码...但是这是从所有行中插入相同的...但我想在选定年份之间插入所有年份。

public function addVin(){
        $this->layout = "backend";
        if($this->request->is('post')){       
            $this->loadModel('VinStore');
            $from = $this->request->data['VinStore']['FromYear'];
            $to = $this->request->data['VinStore']['ToYear'];
            if((!empty($from)) && (!empty($to))) { 
                $count = $from - $to;
                for ($i=0; $i<=$count; $i++) {  
                    $this->VinStore->create();                
                    $this->VinStore->save($this->request->data);
                }
                $this->Session->setFlash("Vin Data has been added");
                $this->redirect('/admins/addVin'); 
            }       

        }

    }

1 个答案:

答案 0 :(得分:0)

public function addVin(){
    $this->layout = "backend";
    if($this->request->is('post')){      
        $this->loadModel('VinStore');       
            $from = $this->request->data['VinStore']['MDL_YR'];
            $to = $this->request->data['VinStore']['MDL_YR_To'];
            $count = $to - $from; 
            for ($i=0; $i<=$count; $i++) {  
                $year = $from++;
                $this->request->data['VinStore']['MDL_YR'] = $year;
                $this->VinStore->create();                
                $this->VinStore->save($this->request->data);
                $to--;
            }
            $this->Session->setFlash("Vin Data has been added");
            $this->redirect('/admins/addVin'); 



    }