我的删除功能无效。点击确认消息后,它不会删除。
我的观点如下:
<div id="selected-color" >
<?php if ($this->session->userdata('edit_colors')) {
foreach ($this->session->userdata('edit_colors') as $color)
{ ?>
<div class="col-md-2 selected-color">
<div class="color-box" style="background-color:<?php echo $color; ?>"></div>
<div class="selected-color-btn" color='<?php echo $color; ?>' data-toggle="modal" >
<input type="hidden" name="color_id" value="<?php echo $color; ?>">
<a class="fa fa-close" data-toggle="modal" onclick='return deleteItem()' name="delete" id="title" img="img1446224811.jpg" big-img="" title="Delete" href="<?php echo base_url();?>admin_control/delete_color/<?php echo $result->id;?>/<?php echo $color;?>"><i class="fa fa-trash-o" data-toggle="tooltip" data-placement="top" title="Delete"></i></a></span>
</div>
</div>
<?php
}
}?>
</div>
</div>
我的javascript看起来像这样:
<script type="text/javascript">
function deleteItem() {
var checkstr = confirm('are you sure want to delete this?');
if (!checkstr) {
return false;
}
}
</script>
我的控制器看起来像这样:
public function delete_color($id = null)
{
$this->roxmodel->delete_color($id);
redirect('admin_control/view_images');
}
我的模型看起来像这样:
public function delete_color($id)
{
$this->db->where('product_color.id', $id);
if ($this->db->delete('product_color'))
{
return true;
}
else
{
return false;
}
}
我的表格如下:
id | product_id | color |
----------------------------
711 | 341 | #f79646 |
712 | 341 | #31859b |
713 | 341 | #c0504d |
745 | 343 | #c0504d |
749 | 347 | #1f497d |
779 | 348 | #c0504d |
我猜我的问题出现在锚标签中......
我的编辑功能控制器看起来像这样......
public function edit_gallery($id)
{
$data['active_mn']='add_images';
$data['active_mn']='view_images';
if($_POST)
{
$this->form_validation->set_rules('image','Image','callback_is_upload_image');
if($this->form_validation->run()==true)
{
if($this->roxmodel->image_update1())
{
$this->session->unset_userdata('image');
//$this->session->unset_userdata('colors');
$this->session->set_flashdata('message','updated successfully');
redirect('admin_control/view_images');
}
else
{
$this->session->set_flashdata('message','update failed');
redirect('admin_control/view_images');
}
}
}
if($id){
$id=$this->uri->segment(3);
$data['result']=$this->roxmodel->view_gallery_id($id);
//$data['query']=$this->admin_model->get_all('products')->result();
// $data['detail']=$this->roxmodel->get_products(null,null,$id);
$colors=$this->roxmodel->get_product_color(null,$id)->result();
if($colors)
{
foreach ($colors as $row){ $color[]=$row->color; }
$this->session->set_userdata('edit_colors',$color);
}
$this->session->set_userdata('image',json_decode($data['result']->image));
$data['category']=$this->roxmodel->get_super_category();
$data['sub_category']=$this->roxmodel->get_sub_category();
}
$this->load->view('edit_image',$data);
}
查看我的视图,我如何获得相应的id值
答案 0 :(得分:0)
请参阅这是我删除记录的方式。 我在我的Base Model中创建了一个函数:
public function delete($id){
$filter = $this->_primary_filter;
$id = $filter($id);
if (!$id) {
return FALSE;
}
$this->db->where($this->_primary_key, $id);
$this->db->limit(1);
$this->db->delete($this->_table_name);
}
然后在我的控制器中,我将其初始化为:
public function delete($id)
{
$this->model->delete($id);
redirect('admin/dashboard');
}
在查看我的锚标记中:
<td><?php echo btn_delete('controller/delete/' . $user->id); ?></td>
还为编辑和删除按钮创建了一个帮助器:
function btn_delete ($uri)
{
return anchor($uri, '<i class="glyphicon glyphicon-remove"></i>', array(
'onclick' => "return confirm('You are about to delete a record. This cannot be undone. Are you sure?');"
));
}
MY_Model.php
<?php
class MY_Model extends CI_Model {
protected $_table_name = '';
protected $_primary_key = 'id';
protected $_primary_filter = 'intval';
protected $_order_by = '';
public $rules = array();
protected $_timestamps = FALSE;
function __construct() {
parent::__construct();
}
public function array_from_post($fields){
$data = array();
foreach ($fields as $field) {
$data[$field] = $this->input->post($field);
}
return $data;
}
public function array_from_get($fields){
$data = array();
foreach ($fields as $field) {
$data[$field] = $this->input->get($field);
}
return $data;
}
public function get($id = NULL, $single = FALSE){
if ($id != NULL) {
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->where($this->_primary_key, $id);
$method = 'row';
}
elseif($single == TRUE) {
$method = 'row';
}
else {
$method = 'result';
}
return $this->db->get($this->_table_name)->$method();
}
public function get_by($where, $single = FALSE){
$this->db->where($where);
return $this->get(NULL, $single);
}
public function save($data, $id = NULL){
// Set timestamps
if ($this->_timestamps == TRUE) {
$now = date('Y-m-d H:i:s');
$id || $data['created'] = $now;
$data['modified'] = $now;
}
// Insert
if ($id === NULL) {
!isset($data[$this->_primary_key]) || $data[$this->_primary_key] = NULL;
$this->db->set($data);
$this->db->insert($this->_table_name);
$id = $this->db->insert_id();
}
// Update
else {
$filter = $this->_primary_filter;
$id = $filter($id);
$this->db->set($data);
$this->db->where($this->_primary_key, $id);
$this->db->update($this->_table_name);
}
return $id;
}
public function delete($id){
$filter = $this->_primary_filter;
$id = $filter($id);
if (!$id) {
return FALSE;
}
$this->db->where($this->_primary_key, $id);
$this->db->limit(1);
$this->db->delete($this->_table_name);
}
}
答案 1 :(得分:-2)
*在模型*
中添加此项 $lines = preg_split("#\r?\n\s*\n#", $content);
$lines = array_reverse($lines);
echo implode(PHP_EOL . PHP_EOL, $lines);
和控制器
return $this->db->affected_rows();
并将结果归还给我