我的表名是收件箱(id,date,message)。这是我的代码, 在视图中:inbox.php
<?php $attribute = array('class' => 'check', 'id' => 'myform'); ?>
<?php echo form_open('messages/remove_checked', $attribute); ?>
<table class="table table-striped table-bordered tablesorter" id="mytable">
<tr><th width="5px" class=specalt ><input type="checkbox" id="select_all" name="select_all"/></th><th>date</th><th>Message</th></tr>
<?php foreach($inbox as $row) : ?>
<tr>
<td width="5px" class=spec><input type="checkbox" name="cntact[]" class="check" value="<?php echo $row->ID; ?>"></td>
<td><? echo $row->Date; ?></td>
<td><? echo $row->Message; ?></td>
</tr>
<?php endforeach;?>
</table>
我添加这个以检查所有复选框,
<script>
$(document).ready(function() {
$('#select_all').click(function() {
var c = this.checked;
$(':checkbox').prop('checked',c);
});
});
</script>
我的控制器:messages.php
function remove_checked()
{
//validation rules
$this->load->library('form_validation');
$this->form_validation->set_rules('cntact[]', 'Private Contact', 'required|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$remove = "No <strong>Data</strong> deleted!";
$this -> json_response(FALSE, $remove);
redirect("messages/inbox");
}
else //success
{
$checked_messages = $this->input->post('cntact'); //selected messages
$this->Mysms_model->delete_checked($checked_messages);
//redirect to
redirect("messages/inbox");
}
}
和我的模特:Mysms_model.php
function delete_checked($cntact)
{
$this->db->where_in('ID', $cntact)
->delete('inbox');
return $this->db->affected_rows() > 0;
}
问题是,当我运行此代码时,我发现了一个错误。
“DELETE FROM inbox
WHERE ID
IN('on','on')”。
那么,有什么?它应该是ID的数量,任何答案?
谢谢。
答案 0 :(得分:0)
这并不是对这个问题的回答,但是,我只想向您展示使用内置库和帮助程序的codeignite来编写视图的另一种方法:
<?php
$this->load->helper('form');
$this->load->library('table');
echo form_open('messages/remove_checked', array('class' => 'check', 'id' => 'myform'));
$this->table->set_template(array('table_open' => '<table class="table table-striped table-bordered tablesorter" id="mytable">'));
$this->table->set_heading(
array('width' => '5px', 'class' => 'specalt', 'data' => form_checkbox(array('id' => 'select_all', 'name' => 'select_all'))),
'date',
'message'
);
foreach ($inbox as $row) {
$this->table->add_row(
array('width' => '5px', 'class' => 'spec', 'data' => form_checkbox(array('class' => 'check', 'name' => 'cntact[]', 'value' => $row->ID))),
$row->Date,
$row->Message
);
}
echo $this->table->generate();
echo form_close();