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

这就是我希望我的其他表格在我提交表格后的样子

新闻复选框.sql

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

这是我的代码(这个代码的问题是它只插入那些带有检查的。所以我的新闻复选框表看起来像这样)

新闻复选框.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 回答