gpt4 book ai didi

php - CodeIgniter - 在更新记录时将默认值添加到数据库中的 form_dropdown

转载 作者:行者123 更新时间:2023-11-29 03:31:11 25 4
gpt4 key购买 nike

I'm wondering if someone can help me out. I have a form_dropdown which is filled with options from the database. I want to show the default value which the user selected while inserting a record, but can't figure out how to do it without adding that to the database This is Model

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();
}

This is the Controller

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);
}

}

this is the view of dropdown function TYPE only

    <?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>
?>

最佳答案

请考虑以下事项。从这里可以明显看出我不是 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>

关于php - CodeIgniter - 在更新记录时将默认值添加到数据库中的 form_dropdown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30366902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com