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

これは私のデータベースがどのように見えるかです

著者.sql

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

ニュース.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.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); 
    }
}
4

1 に答える 1

0

これがあなたが尋ねようとしていることだと思います。

チェックされていないチェックボックスを投稿する

オフのチェックボックスは、フォーム投稿では送信されません。したがって、設定されているかどうかを判断するには、可能なすべてのチェックボックス ($cats ではない) を反復処理する必要があります。

于 2013-04-04T19:22:58.757 に答える