我试图创建一个编辑页面,其中包含一些复选框,其中的值是从数据库中循环的。我尝试了不同的代码组合,但是我似乎在使其正常工作方面遇到了问题,尤其是复选框(复选框是我唯一遇到问题的一个)。
我的代码会发生什么...
例如,如果所有复选框都被选中并且我取消选中第二个和第三个
✓ Dj Lorem ■ Dj Ipsum
■ Dj Dolor ✓ Dj Sit
✓ Dj Amet
提交后会是这个样子
✓ Dj Lorem ✓ Dj Ipsum
✓ Dj Dolor ■ Dj Sit
■ Dj Amet
选中的内容上升/最后一个复选框始终是未选中的复选框,依此类推(如果您取消选中 2 个复选框,则最后 2 个复选框是未选中的复选框)
edit_news.php(查看)
Edit News
<?php
$id = $news_id;
$attributes = array('class' => 'form form-horizontal','id'=>'form_news','name'=>'form_news');
echo form_open(base_url() . 'cms/update_news/'.$id, $attributes);
?>
News Title: <input type="text" class="w300" name="news_title" id="news_title" value="<?php echo $news1->news_title; ?>">
<?php
$i=0;
foreach($dj->result() as $d)
{
$q = $cdj->row($i);
?>
<div class="span3">
<label class="checkbox">
<input type="checkbox" name="cats[]" value="<?php echo $d->UID; ?>"<?php if($q->checked=="yes"){echo "checked";}else if($q->checked==""){};?> /> DJ <?php echo $d->dj_name;?>
<input type="hidden" name="dj_id[]" value="<?php echo $d->UID; ?>">
</label>
</div>
<?php
$i=$i+1;
}
?>
<input type="submit" class="btn btn-primary pull-right" value="Save">
</form>
视图 php 看起来像这样
Edit News
News Title: ___Party at 6____
Choose a DJ
■ Dj Lorem ■ Dj Ipsum
■ Dj Dolor ■ Dj Sit
■ Dj Amet
cms.php(控制器)
public function news_edit($id)
{
$data['dj'] = $this->cms_model->get_dj();
$data['news1'] = $this->cms_model->get_one_news($id);
$data['djnews'] = $this->cms_model->get_djnews($id);
$data['cdj'] = $this->cms_model->get_cdj($id);
$data['id'] = $id;
$this->load->vars($data);
$this->load->view('admin/news_edit');
}
public function update_news($id)
{
$this->cms_model->update_news($id);
redirect(base_url() . "cms/news_edit/".$id);
}
cms_model.php(模型)
function get_dj(){
return $this->db->select()->from("roster")->get();
}
function get_one_news($id){
return $this->db->select()->from("news")->where("UID",$id)->get()->row();
}
function get_cdj($id){
return $this->db->select()->from("news_dj")->where("news_id",$id)->get();
}
function update_news($id)
{
$news_title = $this->input->post("news_title");
$cats = $this->input->post("cats");
$dj_id = $this->input->post("dj_id");
$newdata = array('news_title'=>$news_title
);
$this->db->where('UID', $id);
$this->db->update('news', $newdata);
$i=0;
foreach($dj_id as $d)
{
if ($cats[$i] == ""){$check="no";}
else{$check="yes";}
$dj = array('dj_id'=>$d,'news_id'=>$id,'checked'=>$check);
$this->db->where('dj_id', $d);
$this->db->where('news_id', $id);
$this->db->update('news_dj', $dj);
$i=$i+1;
}
}
新闻(sql)
UID news_title
6 Party at 6
7 Event Cancelled on Monday
8 and so on
名册(sql)
UID news_title
1 Lorem
2 Ipsum
3 Dolor
4 Sit
5 Amet
news_dj(sql)
dj_id news_id checked
1 6 yes
2 6 yes
3 6 yes
4 6 no
5 6 no
我已经没有想法了,我似乎无法解决它
谢谢