YII tutorial-multiple check boxes in the list

Source: Internet
Author: User
Tags php foreach
This is a common situation for programmers. For example, these check boxes aim to delete these selected records. First, let's define a very useful 'polymorphic 'form. I believe you will use it!

This is a common situation for programmers. For example, these check boxes aim to delete these selected records.

First, let's define a very useful 'polymorphic 'form. I believe you will use it!

 data[$key]) ? $this->data[$key] : null); } public function __set($key, $value) { $this->data[$key] = $value; } } ?>

To continue the model, let's define these fields in the 'example 'table: keyExample, Field_one, Field_two. You know how to do it, right ?...

Then, in the controller ExampleAction. php, process the 'example 'list in the action.

... // an over simplified 'list' action  public function actionList () { $form = new PolymorphicForm; $this->render("list", array( "models" => Example::model()->findAll(), "form" => $form, )); } ...

Then define the view in the file list. php.

 
 "myForm")); ?> 
 
  $rec): ?> 
   
All "checkAll")); ?> Delete
Field_one); ?> Field_two); ?> keyExample"); ?>

Finally, we cleverly handle these checkboxs in ExampleAction. php:

... /*** Deletes a range of model* If deletion is successful, the browser will be redirected to the "list" page. */ public function actionDelete() { // I want a post  if(Yii::app()->request->isPostRequest) { // parse $_POST variables foreach($_POST["PolymorphicForm"] as $key => $val) { // is one a these checkbox ? if (strstr ($key, "checkRecord")) { // checkbox in state checked ? if ($val == 1) { // get the key of the record $ar = explode ("_", $key); // deleting record  $model = Example::model()->findByPk ($ar[1])->delete (); } } } $this->redirect(array("list")); } else throw new CHttpException(400,"Invalid request. Please do not repeat this request again."); } ...

The result is as follows: http: // myHttpServer/myApp? R = Example/list can be verified

I hope this will be especially helpful to some Yii programmers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.