Codeigniter批处理复选框的更新 [英] Codeigniter batch update for checkbox

查看:63
本文介绍了Codeigniter批处理复选框的更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到这样的情况,我想更新一些输入为复选框的数据,我在下面尝试了此代码.数据已经保存在数据库中,但是数据保存在另一行中,

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,

例如:我为BirdA检查了红色,黄色和灰色,并同时为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 :

我的Db表:

=============================================
| 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  |
=============================================

查看:

  <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>

和控制器:

    $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');

执行该代码后,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

......

谢谢您,致以问候

推荐答案

控制器代码:-

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');

更改已在您的视图代码中:-

 <?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>
  }
  
  
?>

这篇关于Codeigniter批处理复选框的更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆