Cập nhật hàng loạt Codeigniter cho hộp kiểm
Tôi gặp một tình huống như thế này, tôi muốn cập nhật một số dữ liệu mà đầu vào là các hộp kiểm, tôi đã thử mã này bên dưới. Dữ liệu đã được lưu trong cơ sở dữ liệu, nhưng dữ liệu được lưu trong một hàng khác,
Ví dụ: Tôi đã kiểm tra màu đỏ, vàng và xám cho BirdA và tôi đã kiểm tra màu sẫm và xanh lam cho BirdD cùng một lúc. Trong cơ sở dữ liệu, màu cho BirdA được lưu chính xác, nhưng đối với con thứ 2, màu sẫm và xanh lam được lưu trên BirdB ( Nên lưu trên BirdD )
Tôi muốn giải quyết vấn đề của mình ở trên, vì vậy dữ liệu phải được lưu vào đúng vị trí trong cơ sở dữ liệu. Vui lòng kiểm tra mã của tôi bên dưới:
Bảng Db của tôi:
=============================================
| 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 |
=============================================
Lượt xem :
<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>
Và bộ điều khiển:
$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');
sau khi tôi thực thi mã đó, php gặp lỗi như sau:
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
......
Xin cảm ơn, Trân trọng
Trả lời
Mã điều khiển: -
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');
Thay đổi nằm trong mã chế độ xem của bạn: -
<?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>
}
?>