I'm working on a category section for stories submitted to my website. I have a html select going on in my html and I have that submitting to the database using codeigniter functions. It is submitting to the database, but there seems to be a small problem...the select is only grabbing the last value that is selected. I need to have all the values that are selected entered into the database so I can pull them out at a later date.
我正在处理提交到我网站的故事的类别部分。我在我的html中有一个html选择,我使用codeigniter函数提交到数据库。它正在提交到数据库,但似乎有一个小问题......选择只是抓取所选的最后一个值。我需要将所有选择的值输入到数据库中,以便我可以在以后将它们删除。
This is the html I am using.
这是我正在使用的HTML。
<select multiple class="multi" name="wGenre">
<option value="Action/Adventure">Action/Adventure</option> <option value="Angst">Angst</option>
<option value="Crime">Crime</option>
</select>
and then this is what I have in my model.
然后这就是我的模型中的内容。
$this->title = $this->input->post('wTitle');
$this->genre = $this->input->post('wGenre');
$this->db->insert('story_tbl', $this);
Now, I believe the problem is with my database. I originally set up the field as an enum, but that won't work because it's either one or the other selection and I need to have multiples. So then I tried it as a text field, and it's just not grabbing everything. To be honest I'm at a loss and in need of some help. :)
现在,我认为问题在于我的数据库。我最初将该字段设置为枚举,但这不起作用,因为它是一个或另一个选择,我需要有倍数。所以我把它作为一个文本字段尝试,它只是没有抓住一切。说实话,我很茫然,需要一些帮助。 :)
2 个解决方案
#1
3
Firstly you need to make sure the select tag is named the array manner:
首先,您需要确保select标记以数组方式命名:
<select multiple="multiple" class="multi" name="wGenre[]">
Then To get all values of the multiple select you could do the following to save the values in an array!
然后要获取多重选择的所有值,您可以执行以下操作以将值保存在数组中!
$this->title = $this->input->post('wTitle');
$select_vals = array();
foreach($this->input->post('wGenre') as $val){
$val.' - '; // Used as a delimiter for later use
array_push($select_vals, $val);
}
$this->genre = $select_vals;
$this->db->insert('story_tbl', $this);
#2
2
I ended up solving this without a foreach loop.
我最终在没有foreach循环的情况下解决了这个问题。
First of all, I was writting my html select tag wrong. For it to select multiple values you have to add both [] at the end of the class and a muti tag...like this.
首先,我正在编写我的html选择标记错误。要选择多个值,您必须在类的末尾添加[]和多个标签......就像这样。
<select multiple="multiple" class="multi" name="wGenre[]">
and then in the php it has to be json encoded to be placed in the database correctly...like this...
然后在PHP中它必须被json编码才能正确放入数据库......就像这样......
$this->genre = json_encode($this->input->post('wGenre'));
Thank you for all your help! I hope this helps someone else in the future! :)
谢谢你的帮助!我希望将来可以帮助别人! :)
#1
3
Firstly you need to make sure the select tag is named the array manner:
首先,您需要确保select标记以数组方式命名:
<select multiple="multiple" class="multi" name="wGenre[]">
Then To get all values of the multiple select you could do the following to save the values in an array!
然后要获取多重选择的所有值,您可以执行以下操作以将值保存在数组中!
$this->title = $this->input->post('wTitle');
$select_vals = array();
foreach($this->input->post('wGenre') as $val){
$val.' - '; // Used as a delimiter for later use
array_push($select_vals, $val);
}
$this->genre = $select_vals;
$this->db->insert('story_tbl', $this);
#2
2
I ended up solving this without a foreach loop.
我最终在没有foreach循环的情况下解决了这个问题。
First of all, I was writting my html select tag wrong. For it to select multiple values you have to add both [] at the end of the class and a muti tag...like this.
首先,我正在编写我的html选择标记错误。要选择多个值,您必须在类的末尾添加[]和多个标签......就像这样。
<select multiple="multiple" class="multi" name="wGenre[]">
and then in the php it has to be json encoded to be placed in the database correctly...like this...
然后在PHP中它必须被json编码才能正确放入数据库......就像这样......
$this->genre = json_encode($this->input->post('wGenre'));
Thank you for all your help! I hope this helps someone else in the future! :)
谢谢你的帮助!我希望将来可以帮助别人! :)