2013-04-04 28 views
0

我嘗試寫到哪,如果他們的價值可以在我的數據庫中找到我的複選框的名單將被檢查代碼中找到勾選複選框,如果其值可以在數據庫中使用笨

這是怎麼了我的形式如下像

author name:______________ 
check the categories the author will handle 
■ sports 
■ comics 
■ movies 
■ ads 

一次我去我的編輯作者頁面,這是它應該如何看起來像

author name:Lorem 
check the categories the author will handle 
✓ sports 
■ comics 
✓ movies 
■ ads 

這是我的數據庫看起來像

author.sql

UID  auth_name 
5  Lorem 
6  Ipsum 
7  Dolor 
8  Sit 
9  Amet 

news.sql

UID  title  date  
1  sports 1/1/2013 
2  comics 2/2/2013 
3  movies 3/3/2013 
4  ads  4/4/2013 

,這是我多麼希望我的其他表看起來像一次提交我的形式

newscheckbox.sql

author_id newstitle_id checked 
5   1    yes 
6   1    yes 
7   1    no 
8   1    yes 
9   1    no 

這裏是我的代碼(這個代碼的問題是它只插入那些帶有檢查的代碼。所以我newscheckbox表看起來像這樣)

newscheckbox.sql

author_id newstitle_id checked 
5   1    yes 
6   1    yes 
8   1    yes 

複選框沒有插入到數據庫中檢查列不檢查心不是

addauthor.php(圖)

<?php 
    $attributes = array('class' => 'form form-horizontal','id'=>'form_auth','name'=>'form_auth'); 
    echo form_open(base_url() . 'auth/submit_auth', $attributes); 
?> 
<input type="text" class="w300" name="auth_name" id="auth_name"> 

<?php 
    foreach($auth->result() as $a) 
    { 
     ?> 
         <input type="checkbox" name="cats[]" value="<?php echo $a->UID; ?>"> DJ <?php echo $a->auth_name;?> 

       <?php 
      } 
     ?> 
</form> 

控制器

public function auth_edit($id) 
{ 
    $data['auth'] = $this->jivecms_model->get_auth(); 
    $data['news'] = $this->jivecms_model->get_news($id); 
    $data['id'] = $id; 
    $this->load->vars($data); 
    $this->load->view('auth/auth_edit'); 
} 

public function submit_auth() 
{ 
$this->jivecms_model->submit_auth(); 
redirect(base_url() . "auth/auth_edit"); 
} 

模型

function submit_auth() 
{     
    $auth_name = $this->input->post("auth_name"); 
    $cats = $this->input->post("cats"); 

    $newdata = array('auth_name'=>$auth_name 
        ); 


    $this->db->insert('author', $newdata); 
    $id = $this->db->insert_id(); 

    foreach($cats as $c) 
    { 
     if ($c == ""){$check="no";} 
     else{$check="yes";} 
     $auth = array('news_id'=>$c,'author_id'=>$id,'checked'=>$check); 
     $this->db->insert('newscheckbox', $auth); 
    } 
} 

回答