如何在mysql中使用codeigniter插入多个checkbox值 [英] How to insert multiple checkbox value in mysql using codeigniter

查看:160
本文介绍了如何在mysql中使用codeigniter插入多个checkbox值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手在codeigniter。我试图插入多个新闻,其中包含一些复选框,其值从数据库循环。但我不知道什么应该代码我写在控制器&要插入值的模型。 / p>

查看 - content.php

  //类别名称
<?php foreach($ result as $ aresult){?>
< input type =checkboxname =category_name []value =<?php echo $ aresult-> category_name;?& /> <?php echo $ aresult-> category_name;?>
<?php}?>

控制器 - 新闻

  public function savecontent()
{
$ data = array();
foreach($ this-> input-> post('category_name')as $ category_name)
{
$ data [] = array('category_name'=> $ category_name) ;
}
$ data ['content_headline'] = $ this-> input-> post('content_headline',true);
$ this-> co_model-> save_content($ data);
}

型号:

  public function save_content($ data)
{
$ this-> db-> insert('content',$ data);
}

数据库结构:

  id(auto incr。),category_name(varchar 50),content_headline(varchar 100)

现在我应该怎么改变插入多行使用复选框?这里还有另一个文本框/列也插入数据库。但只有类别名称(复选框值)应该为所有值更改。

解决方案

首先,您需要调整数据库结构。显然, content 类别之间存在M:N关系。更合适的模式是:

 创建表内容(
id int not null auto_increment主键,
content_headline varchar(100)not null
);

create table category(
id int not null auto_increment主键,
caregory_name varchar(100)not null
);

create table content_category(
content int not null references content,
category int not null references category,
primary key(content,category)
) ;

然后,您需要调整代码以反映新模式。



假设你从中选择选择,你的视图会沸腾到:

 < label> 
标题:
< input type ='text'name ='headline'>
< / label>
<?php foreach($ result as $ aresult):?>
< label>
< input type =checkboxname =category [< ?? php echo $ aresult-> id;?>]>
<?php echo $ aresult-> category_name; >
< / label>
<?php endforeach; >

发布时,将提供一个类似如下的数组:

  array(
'headline'=>'War and Peace',
'category'=& b 45 =>'on',
6 =>'on'

);

...其中45和6是选中的类别ID。请注意,如果未选中类别,则不会定义类别数组。



  public function savecontent()
{
$ post = $ this-> input-> ; post();
$ selected_cats =(isset($ post-> category)?array_keys($ post-> category):array());
$ data = array(
'headline'=> $ post-> headline,
'category'=> $ chosen_cats
);
$ this-> co_model-> save_content($ data);
}

最后,模型:

  public function save_content($ data)
{
$ this-> db-& ,$ data ['headline']);
$ content = $ this-> db-> insert_id(); //我们插入的内容
foreach($ data ['category'] as $ cat)
$ this-> db-> insert(
'content_category',
array(
'content'=> $ content,
'category '=> $ cat

);
}


i am novice in codeigniter. I am trying to insert multiple news that contains some check-box where its values are looped from a database. But i don't know what should code i write for this in controller & Model to insert values. can any one suggest or help me to write code?

view - content.php:

//category name
<?php foreach($result as $aresult) { ?>
    <input type="checkbox" name="category_name[]" value="<?php echo $aresult->category_name;?>" /> <?php echo $aresult->category_name;?>
<?php  } ?>

controller - news:

public function savecontent()
{         
    $data=array();
    foreach($this->input->post('category_name') as $category_name) 
    {
       $data[] = array('category_name' => $category_name);
    }
    $data['content_headline']=$this->input->post('content_headline',true);
    $this->co_model->save_content($data);
}

Model:

public function save_content($data)
{
    $this->db->insert('content',$data);
}

Database structure:

id(auto incr.), category_name(varchar 50),  content_headline(varchar 100) 

now what should i change to insert multiple row using checkbox ? here there have also another text box/column which also insert in database. but only category name(checkbox value) should be change for all value.

解决方案

First of all you need to adjust your database structure. Apparently, there's a M:N relation between content and category. A more appropriate schema would be:

create table content (
  id int not null auto_increment primary key,
  content_headline varchar(100) not null
  );

create table category (
  id int not null auto_increment primary key,
  caregory_name varchar(100) not null
  );

create table content_category (
  content int not null references content,
  category int not null references category,
  primary key (content,category)
  );

Then, you need to adjust your code to reflect the new schema.

Supposing you get $result from a select * from category, your view would boil down to:

<label>
  Headline:
  <input type='text' name='headline'>
</label>
<?php foreach($result as $aresult): ?>
  <label>
    <input type="checkbox" name="category[<?php echo $aresult->id; ?>]">
    <?php echo $aresult->category_name; ?>
  </label>
<?php endforeach; ?>

When posted, this will provide you with an array like the following:

array(
  'headline'=>'War and Peace',
  'category'=>array(
    45=>'on',
    6=>'on'
    )
  );

...where 45 and 6 are the checked category ids. Please note that, if no category is checked, the category array will not be defined.

So the controller becomes:

public function savecontent()
  {         
  $post=$this->input->post();
  $selected_cats=(isset($post->category)?array_keys($post->category):array());
  $data=array(
    'headline'=>$post->headline,
    'category'=>$selected_cats
    );
  $this->co_model->save_content($data);
  }

And finally, the model:

public function save_content($data)
  {
  $this->db->insert('content',array('content_headline',$data['headline']);
  $content=$this->db->insert_id(); // what we inserted
  foreach($data['category'] as $cat)
    $this->db->insert(
      'content_category',
      array(
        'content'=>$content,
        'category'=>$cat
        )
      );
  }

这篇关于如何在mysql中使用codeigniter插入多个checkbox值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆