如何在codeigniter中发布由循环创建的表单值

时间:2016-05-23 14:38:28

标签: php codeigniter

如何从使用循环创建的表单中获取帖子值

这是我的控制器

    $valuepost=$this->input->post('value');

if($valuepost==1){
    $val1=array('NoOfinput' => $this->input->post('val1input'),'NoOfbox' => $this->input->post('val1box'),'boxvalue' => null);
    $val=array($val1);
    } 
    elseif($valuepost==2){
    $val1=array('NoOfinput' => $this->input->post('val1input'),'NoOfbox' => $this->input->post('val1box'),'boxvalue' => null);
    $val2=array('NoOfinput' => $this->input->post('val2input'),'NoOfbox' => $this->input->post('val2box'),'boxvalue' => null);
    $val=array($val1,$val2);
    }   
        elseif($valuepost==3){
    $val1=array('NoOfinput' => $this->input->post('val1input'),'NoOfbox' => $this->input->post('val1box'),'boxvalue' => null);
    $val2=array('NoOfinput' => $this->input->post('val2input'),'NoOfbox' => $this->input->post('val2box'),'boxvalue' => null);
    $val3=array('NoOfinput' => $this->input->post('val3input'),'NoOfbox' => $this->input->post('val3box'),'boxvalue' => null);

    $val=array($val1,$val2,$val3);
    }

    elseif($valuepost==4){
    $val1=array('NoOfinput' => $this->input->post('val1input'),'NoOfbox' => $this->input->post('val1box'),'boxvalue' => null);
    $val2=array('NoOfinput' => $this->input->post('val2input'),'NoOfbox' => $this->input->post('val2box'),'boxvalue' => null);
    $val3=array('NoOfinput' => $this->input->post('val3input'),'NoOfbox' => $this->input->post('val3box'),'boxvalue' => null);
    $val4=array('NoOfinput' => $this->input->post('val4input'),'NoOfbox' => $this->input->post('val4box'),'boxvalue' => null);

    $val=array($val1,$val2,$val3,$val4);
    }

 echo val;

如何使用循环或foreach简化此操作,请帮助解决所需的解决方案

1 个答案:

答案 0 :(得分:0)

使用(int)将post值转换为int。

-Pandroid.injected.signing.store.file= path to your keystore 
-Pandroid.injected.signing.store.password=yourstorepassword
-Pandroid.injected.signing.key.alias=yourkeyalias
-Pandroid.injected.signing.key.password=yourkeypassword

如果$ valuepost不高(如果你确定它不会超过4),你也应该在循环之前检查:

$valuepost = (int) $this->input->post('value');
// declaring the array that will store the final result
$val = [];

// looping as many times as necessary
for ($i = 1; $i <= $valuepost; $i++) {
    $val[] = [
        'NoOfinput' => $this->input->post('val' . $i . 'input'),
        'NoOfbox' => $this->input->post('val' . $i . 'box'),
        'boxvalue' => null,
    ];
}

var_dump($val);

(例外或重定向)