lundi 22 octobre 2018

I want to print data from database in checkboxes checked and unchecked through junction table... in PHP codeigniter

blogger data added in the blogger table with a unique key.. and another table in which categories stored. An junction table in between blogger and categories in which blogger id linked with the category. At a time blogger can select multiple categories.

i want to show blogger lies in different categories through checkboxes in codeigniter..

this is my blogger model..

public function update(){ //Editing blogger data from blogger/view.php in tbl_bloggers
        $id = $this->input->post('txt_hidden');

        $field = array(
            'blogger_name'=>$this->input->post('blogger_name'),
            'email'=>$this->input->post('email'),
            'phone'=>$this->input->post('phone'),
            'fb_page'=>$this->input->post('fb_page'),
            'fb_group'=>$this->input->post('fb_group'),
            'instagram'=>$this->input->post('instagram'),
            'twitter'=>$this->input->post('twitter'),
            'snapchat'=>$this->input->post('snapchat'),
            'youtube'=>$this->input->post('youtube'),
            'web'=>$this->input->post('web'),
            'bank'=>$this->input->post('bank'),
            'b_acc_title'=>$this->input->post('b_acc_title'),
            'b_branch'=>$this->input->post('b_branch'),
            'b_account'=>$this->input->post('b_account')
            );



        foreach($this->input->post('cat') as $cat) {        
            // $query=$this->db->query("INSERT INTO tbl_bloggers_cat (blogger_id, Cat_id) VALUES('$id',$cat)");
            $query=$this->db->query("UPDATE tbl_bloggers_cat SET Cat_id = $cat WHERE blogger_id = $id");                
        }

        $this->db->where('blogger_id', $id);
        $this->db->update('tbl_bloggers', $field);
        echo $this->db->last_query();extit;
        if($this->db->affected_rows() > 0){
            return true;
        }else{
            return false;
        }
    }

this is my view where i m trying to show check boxes checked and unchecked through database junction table data

<?php 
                            $blog = $bloggers->blogger_id;

                            foreach ($list as $bcat) { // get data from main categ
                                //if($blog  == $ccat->blogger_id){
                                $a = $bcat->cat_id; //
                                foreach($list2 as $ccat){ // getting cat id from junction table
                                    if($blog  == $ccat->blogger_id && $a == $ccat->Cat_id){
                                    ?>
                                        <label><input type="checkbox" id="cat" name="cat[]" value="<?php echo $bcat->cat_id; ?>" checked<?php echo set_checkbox('accept_terms_checkbox', '$bcat->cat_id'); ?>><?php echo ucfirst($bcat->category); ?></label>
                                    <?php } 
                                    else{
                                        ?>

                                    <?php
                                }
                                    }
                                }

?>

and this is my controller

public function update(){
    $result = $this->bm->update();
    if($result){
        $this->session->set_flashdata('success_msg', 'Record updated successfully');
    }else{
        $this->session->set_flashdata('error_msg', 'Faill to update record');
    }
    redirect(base_url('blogger/index'));
}

i am facing issue in my view.. if i print it in else then it print many categories repeatedly... i just want to print categories marked when data is available in junction table... and when its not it also print but unchecked...




Aucun commentaire:

Enregistrer un commentaire