Yii 中的升序数字

2022-01-04 00:00:00 php yii

在 Yii 中,我正在做一个表单,其中表单具有用于用户详细信息的输入字段.我为所有这些字段创建了必要的输入字段.用户可以提交所有值.现在我有一个字段,它将显示用户不会输入的表单编号.它将按照升序随机生成,例如对于第一个表单,它将像这样 FORM:001, 对于第二个表单,它将像这样 FORM:002 并且它会继续.现在我希望表单编号类似于 Form:001 那么如何做到这一点?任何帮助和建议都将非常有用.

[更新]

 

<?php echo $form->labelEx($model,'id');?><?php echo Yii::app()->db->getLastInsertId();?><?php echo $form->error($model,'id');?>

这是视图的代码 > _form.php 文件.结果是 ID 0

解决方案

您需要的是以下任一项:

$maxFormId= Yii::app()->db->createCommand()->select('max(id) as max')->from('tbl_yourtable')->queryScalar();$yourFormId = "表格:".($maxFormId+ 1);

或者在插入后运行以下命令,然后只显示表单ID:

$yourFormId = "Form:".Yii::app()->db->getLastInsertId();

更新:

公共函数actionCreate(){$model=new YourModel;if(isset($_POST['YourForm'])){$model->attributes=$_POST['YourForm'];if($model->save())$this->redirect(array('view','id'=>$model->id));}$this->render('create',array('模型'=>$模型,));}

以上:

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

自动为您提供最后插入的 ID,因此您可以将以下内容放入您的视图中:

echo "Form:".$id;

In Yii I am doing a form in which the form has input fields for user details.I have made necessary input fields for all those fields.Where a user can submit all the values.Now I have a field where it will show the form number which will not be entered by user.It will be generated randomly with ascending order like for the 1st form it will be like this FORM:001, For the second form it will be like this FORM:002 and it will go on. Now I want that the form number will be like Form:001 so how to do that?HAny help and suggestions will be highly appriciable.

[UPDATED]

  <div class="row">
    <?php echo $form->labelEx($model,'id'); ?>
    <?php echo Yii::app()->db->getLastInsertId();?>
    <?php echo $form->error($model,'id'); ?>
  </div>

This is the code for view > _form.php file. and the result is ID 0

解决方案

What you need is either of the following:

$maxFormId= Yii::app()->db->createCommand()
->select('max(id) as max')
->from('tbl_yourtable')
->queryScalar();
$yourFormId = "Form:".($maxFormId+ 1);

or alternatively run the following after the insert, and only display the form id then:

$yourFormId = "Form:".Yii::app()->db->getLastInsertId();

UPDATE:

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

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

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

On the above:

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

automatically gives you the last ID inserted so you can just put the following in your view:

echo "Form:".$id;

相关文章