我下拉的内容如下所示
<select class="selectpicker form-control" multiple name="category[]">
<option value="1" id="MENTAL HEALTH">MENTAL HEALTH</option>
<option value="2" id="SUICIDE SUPPORT">SUICIDE SUPPORT</option>
<option value="3" id="HEALTH">HEALTH</option>
<option value="4" id="SUPPORT">SUPPORT</option>
</select>
在我的控制器中,我有以下代码从下拉列表中获取数据
public function save()
{
$ctgry= ($this->input->post('category'));
$orgName= ($this->input->post('InputName'));
$streetNo= ($this->input->post('StreetNo'));
$streetName= ($this->input->post('StreetName'));
$suburb= ($this->input->post('Suburb'));
$state= ($this->input->post('state'));
$postCode= ($this->input->post('PostCode'));
$count = count($ctgry);
$supprtGroup = array(
'name' => $orgName,
'street_no' => $streetNo,
'street' => $streetName,
'suburb' => $suburb,
'state' => $state,
'pc' => $postCode
);
$supportgroup_id = $this->Home_Model->addSupportGroup($supprtGroup);
$j = 0;
i = 0;
$sc_id = 0
echo 'aa';//just for error checking purpose
if ($count > 1)
{
echo ' aa1';//just for error checking purpose
while ($i > $count)
{
echo ' aa2';//just for error checking purpose
$support_category = array(
'supportgroup_id' => $supportgroup_id,
'category_id' => $ctgry[$j]
);
$sc_id = $this->Home_Model->addSupportrCategory($support_category);
$i++;
echo $sc_id;//just for error checking purpose
}
}
else
{
echo ' aa3';//just for error checking purpose
$support_category = array(
'supportgroup_id' => $supportgroup_id,
'category_id' => $ctgry[$j]
);
$sc_id = $this->Home_Model->addSupportrCategory($support_category);
echo $sc_id;//just for error checking purpose
}
echo'bb';//just for error checking purpose
}
以下是我的模型类
function addSupportGroup($supprtGroup=NULL)
{
$this->db->insert('support_group', $supprtGroup);
return $this->db->insert_id();
}
function addSupportrCategory($support_category=NULL)
{
$this->db->insert('support_category', $support_category);
return $this->db->insert_id();
}
当从下拉列表中选择多个值时,输出如下
aa aa1bb
如果是单一价值,有人可以给我一个答案,为什么会发生这种情况 从下拉列表中选择它保存到DB精细我搜索每个类似的 帖子但找不到答案。
答案 0 :(得分:1)
我无法看到你问题中$count
的定义,所以我不知道它的价值是什么,但是,如果你得到“aa1”作为输出你会说它更大比1。
使用while()
,您说$i
必须大于$count
但$ i等于0,因此while循环不会启动。
如果您只想循环浏览类别,那么更容易:
$supportgroup_id = $this->Home_Model->addSupportGroup($supprtGroup);
foreach ($ctgry as $cat) {
$insert = array(
'supportgroup_id' => $supportgroup_id,
'category_id' => $cat
);
$sc_id = $this->Home_Model->addSupportrCategory($support_category);
}
这将有效,因为下拉列表将始终作为数组被选中。
你应还要考虑为此添加一些验证!
希望这有帮助!