简体   繁体   English

复选框的 Codeigniter 批量更新

[英]Codeigniter batch update for checkbox

I have a situation like this, I want to update some data where the input are checkboxes, I was tried this code below.我有这样的情况,我想更新一些输入为复选框的数据,我在下面尝试了这段代码。 Data already saved in database, but data saved in another row,数据已经保存在数据库中,但数据保存在另一行中,

For example : I checked color red, yellow and grey for BirdA and I checked dark and blue for BirdD at the same time.例如:我检查了 BirdA 的红色、黄色和灰色,同时检查了 BirdD 的深色和蓝色。 In database, color for BirdA saved correctly, but for the 2nd bird, dark and blue saved on BirdB ( Should be save on BirdD )在数据库中,BirdA 的颜色保存正确,但对于第二只鸟,深蓝色保存在 BirdB 上(应该保存在 BirdD 上

I want to solve my problem above, so data should be save in the right place in database.我想解决我上面的问题,所以数据应该保存在数据库中的正确位置。 Please check my code below :请在下面检查我的代码:

My Db Table:我的数据库表:

=============================================
| birds | red | blue | grey | yellow | dark | 
=============================================
| BirdA |  0  |   0  |   0  |   0    |   0  |
| BirdB |  0  |   0  |   0  |   0    |   0  |
| BirdC |  0  |   0  |   0  |   0    |   0  |
| BirdD |  0  |   0  |   0  |   0    |   0  |
=============================================

View :看法 :

  <tr>
    <td><input type="hidden" name="birds[]" value='<?php echo $dp['birds']; ?>' /><?php echo $dp['birds']; ?></td>
    <td><p align='center'><input type="text" name="qtt[]" value='<?php echo $dp['qtt']; ?>' tabindex="2" style="width:30px;" /></td>
    <td><p align='center'><input type="checkbox" name="red[]" value='1' <?php if($dp['red']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="blue[]" value='1' <?php if($dp['blue']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="grey[]" value='1' <?php if($dp['grey']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="yellow[]" value='1' <?php if($dp['yellow']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="dark[]" value='1' <?php if($dp['dark']==1) { echo " checked=\"checked\""; } ?> /></td>
  </tr>

And the controller :和控制器:

    $birds= $this->input->post("birds");
        if (empty($this->input->post("birds"))){
            $birds= 0;
        }
    $qtt= $this->input->post("qtt");
        if (empty($this->input->post("qtt"))){
            $qtt= 0;
        }
    $red= $this->input->post("red");
        if (empty($this->input->post("red"))){
            $red= 0;
        }
    $blue= $this->input->post("blue");
        if (empty($this->input->post("blue"))){
            $blue= 0;
        }
    $grey= $this->input->post("grey");
        if (empty($this->input->post("grey"))){
            $grey= 0;
        }
    $yellow= $this->input->post("yellow");
        if (empty($this->input->post("yellow"))){
            $yellow= 0;
        }
    $dark= $this->input->post("dark");
        if (empty($this->input->post("dark"))){
            $dark= 0;
        }

    for($x = 0; $x < sizeof($birds); $x++){
        $reslt[$x] = array(
            "birds"    => $birds[$x],
            "qtt"  => $qtt[$x],
            "red"  => $red[$x],
            "blue"  => $blue[$x],
            "grey"  => $grey[$x],
            "yellow"  => $yellow[$x],
            "dark"  => $dark[$x]
            );
        }
        $this->db->update_batch('db_birds', $reslt, 'birds');

after I execute that code, php has an error like this :执行该代码后,php 出现如下错误:

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: x

Filename: controllers/....

Line Number: 613

Backtrace:

File: /home/.....
Line: 613
Function: _error_handler

File: /home/..../index.php
Line: 315
Function: require_once

A PHP Error was encountered
Severity: Notice

Message: Undefined offset: 3

Filename: controllers/....

Line Number: 621

Backtrace:

File: /home/....
Line: 621
Function: _error_handler

File: /home/..../index.php
Line: 315
Function: require_once

A PHP Error was encountered
Severity: Notice

Message: Undefined offset: 3

Filename: controllers/...

Line Number: 622

Backtrace:

File: /home/...
Line: 622
Function: _error_handler

File: /home/.../index.php
Line: 315
Function: require_once

A PHP Error was encountered
Severity: Notice

Message: Undefined offset: 4

Filename: controllers/...

Line Number: 620

Backtrace:

File: /home/...
Line: 620
Function: _error_handler

File: /home/.../index.php
Line: 315
Function: require_once

A PHP Error was encountered
Severity: Notice

Message: Undefined offset: 4

Filename: controllers/...

Line Number: 621

Backtrace:

File: /home/...
Line: 621
Function: _error_handler

File: /home/.../index.php
Line: 315
Function: require_once

A PHP Error was encountered
Severity: Notice

Message: Undefined offset: 4

Filename: controllers/...

Line Number: 622

Backtrace:

File: /home/...
Line: 622
Function: _error_handler

File: /home/.../index.php
Line: 315
Function: require_once

A PHP Error was encountered
Severity: Notice

Message: Undefined offset: 5

Filename: controllers/...

Line Number: 618

Backtrace:

File: /home/...
Line: 618
Function: _error_handler

File: /home/.../index.php
Line: 315
Function: require_once

......

Thank you, Best Regards谢谢您最好的问候

Controller Code:-控制器代码:-

for($x = 0; $x < sizeof($birds); $x++){
        $reslt[$x] = array(
            "birds"    => $birds[$x],
            "qtt"  => $qtt[$x],
            "red"  => $red[$x],
            "blue"  => $blue[$x],
            "grey"  => $grey[$x],
            "yellow"  => $yellow[$x],
            "dark"  => $dark[$x]
            );
        }
$this->db->update_batch('db_birds', $reslt, 'birds');

Change is in your view code:-更改在您的视图代码中:-

 <?php


//sql Query.
//execute Query.


$ii=0;
foreach($dps as $dp){
      $iii = $ii++; 
 <tr>
    <td><input type="hidden" name="birds[<?php echo $iii;?>]" value='<?php echo $dp['birds']; ?>' /><?php echo $dp['birds']; ?></td>
    <td><p align='center'><input type="text" name="qtt[]" value='<?php echo $dp['qtt']; ?>' tabindex="2" style="width:30px;" /></td>
    <td><p align='center'><input type="checkbox" name="red[<?php echo $iii;?>]" value='1' <?php if($dp['red']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="blue[<?php echo $iii;?>]" value='1' <?php if($dp['blue']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="grey[<?php echo $iii;?>]" value='1' <?php if($dp['grey']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="yellow[<?php echo $iii;?>]" value='1' <?php if($dp['yellow']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="dark[<?php echo $iii;?>]" value='1' <?php if($dp['dark']==1) { echo " checked=\"checked\""; } ?> /></td>
  </tr>
  }
  
  
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM