【发布时间】:2014-03-10 19:52:05
【问题描述】:
我是 CI 和 PHP 的新手。我有一个由我的数据库中的表填充的国家的下拉列表。我已经设法让 CI 通过 mysql 获取和呈现数据,但它以纯文本形式填充在我的表单上方,而不是在下拉列表的值中。下面是简化的代码。如何获取下拉列表中的值?
我的看法...
<body>
<?php echo validation_errors(); ?>
<?php echo form_open('form'); ?>
<h5>Country</h5>
<select name="countryselect">
<?php echo form_dropdown(countryselect);?>
</select>
<br><br>
<div><input type="submit" value="Submit" /></div>
<?php echo form_close() ?>
</body>
我的控制器...
<?php
class Form extends CI_Controller {
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('firstname', 'First Name', 'trim|required|max_length[12]|xss_clean');
$this->form_validation->set_rules('lastname', 'Last Name', 'trim|required|max_length[12]|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('pledge', 'Pledge', 'trim|required|max_length[12]|decimal|xss_clean');
$this->load->model('country');
$data['country'] = $this->country->get_country();
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->database();
$sql = "INSERT INTO donations (firstname, lastname, email, pledge) VALUES (
".$this->db->escape($this->input->post('firstname')).",
".$this->db->escape($this->input->post('lastname')).",
".$this->db->escape($this->input->post('email')).",
".$this->db->escape($this->input->post('pledge')).")";
$this->db->query($sql);
echo $this->db->affected_rows();
$this->load->view('formsuccess');
}
}
}
?>
我的模型...
<?php
class Country extends CI_Model
{
function get_country() {
$this->load->database();
$return[''] = 'please select';
$this->db->order_by('name');
$query = $this->db->get('countries');
foreach($query->result_array() as $row){
echo '<option value="'.$return[$row['id']] = $row['name'].'">',$return[$row['id']] = $row['name'],'</option>'. "\n";
}
return $return;
}
}
?>
【问题讨论】:
标签: php codeigniter