Yii Framework official guide series 20-use forms: collecting table input in batches

Source: Internet
Author: User
Tags php foreach
Sometimes we want to collect user input in batch mode. That is, you can enter information for multiple model instances and submit them at a time. We call this table input (tabularinput) because these input items are usually...



Sometimes we want to collect user input in batch mode. That is, you can enter information for multiple model instances and submit them at a time. We call thisTabular input)Because these input items are usually presented as HTML tables.

To use table input, we first need to create or fill in an array of model instances, depending on whether we want to insert or update data. Then we start from$_POSTExtract user input data from the variable and assign it to each model. It is slightly different from single model input: we need to use$_POST['ModelClass'][$i]Extract input data instead of using$_POST['ModelClass'].


Public function actionBatchUpdate () {// assume that each item (Item) is an instance of the 'item' class, // extract the items to be updated in batch mode $ items = $ this-> getItemsToUpdate (); if (isset ($ _ POST ['item']) {$ valid = true; foreach ($ items as $ I => $ item) {if (isset ($ _ POST ['item'] [$ I]) $ item-> attributes =$ _ POST ['item'] [$ I]; $ valid = $ valid & $ Item-> validate ();} if ($ valid) // if all projects are valid //... perform some operations here} // display the View collection table input $ this-> render ('batchupdate', array ('items '=> $ items ));}

We have prepared this action, and we need to continue.batchUpdateView to display input items in an HTML table.


$item): ?>
Name Price Count Description

Note that we used"[$i]name"Instead"name"The second parameter used to call CHtml: activeTextField.

If any verification error occurs, the corresponding input items are automatically highlighted, just like the single model input we described earlier.

The above is the Yii Framework official guide series 20-using forms: collecting the content of table input in batches. For more information, see PHP Chinese website (www.php1.cn )!

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.