从视图中的php变量获取值


Obtaining values from a php variable inside a view

我有一个表单,其中有多个上传按钮。横幅、封面和标题X,X由数字1,2…X代替这意味着我可以有多个按钮上传标题。

我有这个隐藏的输入(在我看来),我在其中存储标题的数量。

<input type="hidden" name="qtd_headliners" id="qtd_headliners" value="<?php echo $qtd_headliners?>" />

我尝试了这种方式(控制器中的方法)来访问它,但它什么也没做——它只添加了横幅和封面。

public function uploadOptions(){
    $opt = array();
    for ($i=1; $i <= $_POST['qtd_headliners']; $i++) { 
        if(!array_key_exists($i, $_POST))
            continue;
        $headliner = $_POST('headliners'.$i);
        $opt[$i] = $headliner;
        $this->set('Headliner' . $opt[$i] , 'debug');
    }
    array_push($opt, 'banner', 'cover');
    return $opt;
}

有人能给我指正确的方向吗?

检查此功能的工作版本:

function uploadOptions(){
    $opt = array();
    for ($i=1; $i <= $_POST['qtd_headliners']; $i++) { 
        if(!array_key_exists('headliners'.$i, $_POST)) // Note the 'headliners' string
            continue;
        $headliner = $_POST['headliners'.$i]; // $_POST is an array, so access its items with []
        $opt[$i] = $headliner;
        $this->set('Headliner' . $opt[$i] , 'debug');
    }
    array_push($opt, 'banner', 'cover');
    return $opt;
}

重读代码几分钟后,我找到了错误和解决方案。

public function uploadOptions(){
    $opt = array();
    array_push($opt, 'banner', 'cover');
    for ($i=1; $i <= $_POST['qtd_headliners']; $i++) { 
        $headliner = 'headliner'.$i;
        array_push($opt, $headliner);
    }
    return $opt;
}

无论如何都要感谢@Cili和@arilia