我想知道是否有人可以帮助我。我有一个form_dropdown,它填充了数据库中的选项。我想显示用户在插入记录时选择的默认值,但无法在不将其添加到数据库的情况下弄清楚如何执行此操作 这是模型
function get_type()
{
$results = $this->db->select('t_id, t_name')->from('account_type')->get()->result();
$t_id = array('-SELECT-');
$t_name = array('-SELECT-');
for ($i = 0; $i < count($results); $i++)
{
array_push($t_id, $results[$i]->t_id);
array_push($t_name, $results[$i]->t_name);
}
return $type_result = array_combine($t_id, $t_name);
}
function get_account_record($a_id)
{
$this->db->where('a_id', $a_id);
$this->db->from('account_info');
$query = $this->db->get();
return $query->result();
}
这是控制器
function update($a_id)
{
$data['a_id'] = $a_id;
$data['type'] = $this->sf_model->get_type();
$data['view'] = $this->sf_model->get_account_record($a_id);
$this->form_validation->set_rules('a_name', 'Account Name', 'trim|required|xss_clean|callback_alpha_only_space');
$this->form_validation->set_rules('a_web', 'Website', 'trim|required|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('viewUpdate', $data);
}
else
{
$data = array(
'a_id' => $this->input->post('a_id'),
'a_name' => $this->input->post('a_name'),
'a_website' => $this->input->post('a_web'),
't_id' => $this->input->post('a_type'),
'a_billingStreet' => $this->input->post('a_billingStreet'),
'a_billingCountry' => $this->input->post('a_billingCountry'),
'a_mobile' => $this->input->post('a_mobile')
);
$this->db->where('a_id',$_POST['a_id']);
$this->db->update('account_info', $data);
redirect('salesforce' . $a_id);
}
}
这是仅下拉功能TYPE的视图
<?php
$attributes = 'class = "form-control" id = "a_type"';
echo form_dropdown('a_type',$type,set_value('a_type'),$attributes);?>
<span class="text-danger"><?php echo form_error('a_type'); ?></span>
?>
答案 0 :(得分:0)
请考虑以下事项。从这一点可以明显看出,我不是PHP编码器,但它应该给你一个想法......
<?php
/* DROP TABLE IF EXISTS colours;
CREATE TABLE colours(colour VARCHAR(12) NOT NULL PRIMARY KEY);
INSERT INTO colours VALUES
('Red'),('Orange'),('Yellow'),('Green'),('Blue'),('Indigo'),('Violet');
DROP TABLE IF EXISTS users;
CREATE TABLE users
(username VARCHAR(12) NOT NULL PRIMARY KEY
,colour VARCHAR(12) NOT NULL
);
INSERT INTO users VALUES
('Adam','Orange'),('Bob','Green'),('Charlie','Red'),('Dan','Yellow');
*/
include('path/to/connection/stateme.nts');
$query = "
SELECT c.*
, CASE WHEN u.colour = c.colour THEN 1 ELSE 0 END selected
FROM colours c
LEFT
JOIN users u
ON u.colour = c.colour
AND u.username = 'Adam';";
$result = mysqli_query($db,$query);
$options = '';
while($row = mysqli_fetch_assoc($result)){
if ($row['selected'] == 1){
$selected = 'selected';
} else {
$selected = '';
}
$options .= "<option $selected >{$row['colour']}\n";
} // end of while loop
?>
<select><? echo "\n$options"; ?> </select>
输出:
<select>
<option >Blue
<option >Green
<option >Indigo
<option selected >Orange
<option >Red
<option >Violet
<option >Yellow
</select>