0

我真的想渲染一个复选框列表,之前从未尝试过它,所以我遇到了问题,我试图渲染每个列或属性的所有复选框,这是动作控制器:

public function actionCreate()
    {
        $model=new Parametro;

        $variable = file_get_contents('protected\column.txt');
        $vars = explode(' ', $variable);

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Parametro']))
        {
            $model->attributes=$_POST['Parametro'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('create',array(
            'model'=>$model,
            'variable'=>$vars,
        ));
    }

这是带有另一种方法的模型

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    $variable = file_get_contents('protected\column.txt');
    return array(
        array('parametro_id', 'required'),
        array('parametro_id', 'numerical', 'integerOnly'=>true),
        // The following rule is used by search().
        // @todo Please remove those attributes that should not be searched.
        array('id, parametro_id', 'safe', 'on'=>'search'),
        array($variable, 'safe', 'on'=>'search'),   
    );
}
public function generateAttributeLabel($variable = null)
{
    if($variable)
    {
        $variable = file_get_contents('protected\column.txt');
    }
    return $variable;
}   

public function attributeLabels()
{
        return array(
        'id' => 'ID',
        'parametro_id' => 'Parametro',
    );
}

我试图制作复选框列表的表单

<div>
        <?php echo $form->checkboxList($model, $variable, array(0 => 1)); ?>
</div>

和文件的内容,如果你问

lololo trying

有一些列是自动生成的,我将它们的名称存储在这个文件中,用空格分隔,一切似乎都很好,直到我到达表单上的复选框列表,它给了我类似 strpos() 之类的错误,我该怎么做此复选框用于呈现并将 1 个数据保存到由特定列检查的那些?

4

1 回答 1

1

如果问题是您无法获取复选框,请尝试此操作

更新

在你的_form.php

 <div>
            <?php echo CHtml::checkBoxList('createCheck', array(), $variable); ?>
    </div>


您需要为此 更改控制器操作

public function actionCreate()
        {
            $model=new Parametro;

            $variable = file_get_contents('protected\column.txt');
            $vars = explode(' ', $variable);
          // make SURE that you are getting $vars as array
          if(isset($_POST['Parametro']))
            {
                $model->attributes=$_POST['Parametro'];
                if(isset($_POST['createCheck']))
            {
                 $newVar=array();
                $checkVariables=$_POST['createCheck'];
                foreach($checkVariables as $key)
                {
                    $newVar[]=$vars[$key];
                }
                if(!empty($newVar))
                {

                    foreach($newVar as $saveIt)
                    {

                        $model->$saveIt='y';
                    }
                     $model->save();
                }    

               }
               $this->redirect(array('view','id'=>$model->id));
            }

            $this->render('create',array(
                'model'=>$model,
                'variable'=>$vars,
            ));
        }

注意:-它可能会给你一些错误,因为我不知道你的表结构。如果您遇到任何问题,请尝试解决或询问

于 2013-12-28T05:00:51.787 回答