php - Need help creating editable checkboxes -
im trying create edit page contains checkboxes values looped database. ive tried different combinations of code seem having problems in making work properly, checkboxes(the checkboxes 1 im having problems with).
what happens code that...
for example if checkboxes checked , unchecked 2nd 1 , 3rd one
✓ dj lorem ■ dj ipsum ■ dj dolor ✓ dj sit ✓ dj amet after submitting
✓ dj lorem ✓ dj ipsum ✓ dj dolor ■ dj sit ■ dj amet the checked content goes up/the last checkbox 1 gets unchecked , on(if uncheck 2 checkboxes, last 2 checkboxes ones gets unchecked)
edit_news.php(view)
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() $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> the view php looks
edit news news title: ___party @ 6____ choose dj ■ dj lorem ■ dj ipsum ■ dj dolor ■ dj sit ■ dj amet cms.php (controller)
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(model)
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 $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; } } news(sql)
uid news_title 6 party @ 6 7 event cancelled on monday 8 , on roster(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 ive ran out of ideas , cant seem fix it
thanks
you have set oddly. cats[] only contain elements checked, checking if $cats[$i] != "" means count how many checked , loop, assign first x amount of djs listed. when setting html, i'd suggest
name="cats[<?php echo $d->uid; ?>]" value=... that way, indexed way expecting in update loop. simplest way solve issue - otherwise, check values checked against dj id's in update. again, chose explain how make work quickest have ^^
edit
if don't want change html, can change check from:
if ($cats[$i] == ""){$check="no";} else{$check="yes";} to:
$check = (in_array($d, $cats)) ? "yes" : "no"; as see if current dj in list of submitted checkbox values.
Comments
Post a Comment