Yii,创建一个复选框列表,每个复选框将更改列数据

时间:2013-12-20 00:30:37

标签: php mysql checkbox yii

我必须创建一个复选框列表,它将打印除ids之外的所有列,数据库中的表称为parametros,其中包含id和参数,不需要打印,但是会有更多的列要打印由应用程序创建,只有1-0或真假的数据。

我不知道复选框列表是如何工作的,我一直试图找到一个可以解释所有可以生成的方法的地方,我将展示由gii,模型和动作控制器生成的表单。

_form.php这个

<?php
/* @var $this ParametroController */
/* @var $model Parametro */
/* @var $form CActiveForm */
?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'parametro-form',
        // Please note: When you enable ajax validation, make sure the corresponding
        // controller action is handling ajax validation correctly.
        // There is a call to performAjaxValidation() commented in generated controller code.
        // See class documentation of CActiveForm for details on this.
        'enableAjaxValidation'=>false,
)); ?>

        <p class="note">Fields with <span class="required">*</span> are required.</p>

        <?php echo $form->errorSummary($model); ?>

        <div class="row">
                <?php echo $form->labelEx($model,'nombre'); ?>
                <?php echo $form->textField($model,'nombre',array('size'=>60,'maxlength'=>256)); ?>
                <?php echo $form->error($model,'nombre'); ?>
        </div>

        <div class="row buttons">
                <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
        </div>

<?php $this->endWidget(); ?>

</div><!-- form -->

Parametro.php(模型)

<?php

/**
 * This is the model class for table "parametro".
 *
 * The followings are the available columns in table 'parametro':
 * @property integer $id
 * @property string $nombre
 */
class Parametro extends CActiveRecord
{
        /**
         * @return string the associated database table name
         */
        public function tableName()
        {
                return 'parametro';
        }

        /**
         * @return array validation rules for model attributes.
         */
        public function rules()
        {
                // NOTE: you should only define rules for those attributes that
                // will receive user inputs.
                return array(
//                      array('nombre', 'required'),
//                      array('nombre', 'length', 'max'=>256),
                        // The following rule is used by search().
                        // @todo Please remove those attributes that should not be searched.
                        array('id', 'safe', 'on'=>'search'),
                        array('hplocal', 'safe', 'on'=>'search'),
//                      array('id, nombre', 'safe', 'on'=>'search'),
                );
        }

        /**
         * @return array relational rules.
         */
        public function relations()
        {
                // NOTE: you may need to adjust the relation name and the related
                // class name for the relations automatically generated below.
                return array(
                'peticion'=>array(self::BELONGS_TO,'Peticion','peticion_id'),
                );
        }

        /**
         * @return array customized attribute labels (name=>label)
         */
        public function attributeLabels()
        {
                return array(
                        'id' => 'ID',
//                      'nombre' => 'Nombre',
                );
        }

        /**
         * Retrieves a list of models based on the current search/filter conditions.
         *
         * Typical usecase:
         * - Initialize the model fields with values from filter form.
         * - Execute this method to get CActiveDataProvider instance which will filter
         * models according to data in model fields.
         * - Pass data provider to CGridView, CListView or any similar widget.
         *
         * @return CActiveDataProvider the data provider that can return the models
         * based on the search/filter conditions.
         */
        public function search()
        {
                // @todo Please modify the following code to remove attributes that should not be searched.

                $criteria=new CDbCriteria;

                $criteria->compare('id',$this->id);
//              $criteria->compare('nombre',$this->nombre,true);

                return new CActiveDataProvider($this, array(
                        'criteria'=>$criteria,
                ));
        }

        /**
         * Returns the static model of the specified AR class.
         * Please note that you should have this exact method in all your CActiveRecord descendants!
         * @param string $className active record class name.
         * @return Parametro the static model class
         */
        public static function model($className=__CLASS__)
        {
                return parent::model($className);
        }
}

ParametroController.php(行动)

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

        // 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,
        ));
}

你可以看到几乎没有任何改变,但只是我试图弄清楚如何开始,将由app创建的列将被称为“ph,h1,ho3等... ”。所以我想做一个复选框列表,它会打印应用程序创建的所有列,当你选择其中一些列并按下提交时,选中的复选框将按特定列保存为1或为真。

请帮忙。

2 个答案:

答案 0 :(得分:0)

您可以通过这种方式在yii中创建一个复选框列表。

<?php echo CHtml::activecheckBoxList($model, 'yourAttribute', array("1" => "Arts", "2" => "Science", "3" => "Culture"), array('separator' => '', 'id' => 'chk_lst_id')); ?>

这将在列表中创建3个复选框,

<input type="checkbox" name="Arts" value="1">Arts<br>
<input type="checkbox" name="Science" value="2">Science<br>
<input type="checkbox" name="Culture" value="3">Culture

点击此处查看其他一些示例:http://www.yiiframework.com/wiki/48/by-example-chtml/#hh4

答案 1 :(得分:0)