MySQL在CodeIgniter中使用下拉列表显示数据
在本文中,我们将介绍如何使用MySQL数据库和CodeIgniter框架,将从数据库中检索出的数据显示在下拉菜单中。
阅读更多:MySQL 教程
准备
在开始之前,您需要确保您已经安装好以下软件:
- PHP
- MySQL数据库
- CodeIgniter框架
如果您还没有安装这些软件,您可以在以下网站中下载并安装:
- PHP:https://windows.php.net/download/
- MySQL数据库:https://www.mysql.com/downloads/
- CodeIgniter框架:https://codeigniter.com/download
数据库表
我们将在以下示例中使用一个名为“countries”的数据库表。它包含两个列:“id”和“country”。
CREATE TABLE `countries` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`country` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
在数据库表中插入一些数据:
INSERT INTO `countries` (`id`, `country`) VALUES
(1, 'China'),
(2, 'India'),
(3, 'United States'),
(4, 'Indonesia'),
(5, 'Pakistan'),
(6, 'Brazil'),
(7, 'Nigeria'),
(8, 'Bangladesh'),
(9, 'Russia'),
(10, 'Mexico');
控制器
我们将创建一个名为“Country”控制器,它将包含以下方法:
- index()方法:显示所有国家的下拉菜单。
- get_country()方法:获取指定国家的详细信息。
class Country extends CI_Controller {
public function index() {
//Load the model
this->load->model('Country_model');
//Get all countriesdata['countries'] = this->Country_model->get_all_countries();
//Load the viewthis->load->view('country/index', data);
}
public function get_country(country_id) {
//Load the model
this->load->model('Country_model');
//Get the country by IDcountry = this->Country_model->get_country_by_id(country_id);
//Return the response as JSON
echo json_encode($country);
}
}
模型
我们将创建一个名为“Country_model”模型,它将包含以下方法:
- get_all_countries()方法:获取所有国家信息。
- get_country_by_id($country_id)方法:根据国家ID获取详细信息。
class Country_model extends CI_Model {
public function get_all_countries() {
this->db->select('*');this->db->from('countries');
query =this->db->get();
return query->result();
}
public function get_country_by_id(country_id) {
this->db->select('*');this->db->from('countries');
this->db->where('id',country_id);
query =this->db->get();
return $query->row();
}
}
视图
我们将创建一个名为“index”视图,在这个视图中,我们将会显示从数据库中获取的数据并显示在下拉菜单中。
<!DOCTYPE html>
<html>
<head>
<title>Countries Dropdown</title>
</head>
<body>
<h1>Countries Dropdown</h1>
<form method="post" action="#">
<select name="country" id="country">
<option value="">Select Country</option>
<?php foreach (countries ascountry): ?>
<option value="<?php echo country->id; ?>"><?php echocountry->country; ?></option>
<?php endforeach; ?>
</select>
<button type="button" onclick="getCountry()">Get Country</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
function getCountry() {
var country_id = ('#country').val();
if (country_id != '') {.ajax({
url: '<?php echo base_url(); ?>country/get_country/' + country_id,
type: 'get',
dataType: 'json',
success: function(response) {
if (response) {
alert('Country ID: ' + response.id + '\nCountry Name: ' + response.country);
} else {
alert('Unable to get country details');
}
},
error: function() {
alert('Error while retrieving country details!');
}
});
} else {
alert('Please select a country');
}
}
</script>
</body>
</html>
运行
在浏览器中访问您的CodeIgniter应用程序,并导航到“http://localhost/country” URL。您将看到一个下拉菜单,其中包含从数据库表中的数据。
选择一个国家并点击“Get Country”按钮。您将看到一个JavaScript alert对话框,其中包含您选择的国家的详细信息。
总结
通过使用MySQL数据库和CodeIgniter框架,我们可以轻松地从数据库中检索数据,并将其显示在下拉菜单中。这种方法可以让用户轻松地选择他们感兴趣的选项,并在JavaScript中获取选项的详细信息。
极客教程