这很简单,taken right from CodeIgniter's documentation,你绝对应该通读一遍(代码中的 cmets 主要是我的):
控制者
class Blog_controller extends CI_Controller {
function blog()
{
// Load the Blog model so we can get some data
$this->load->model('Blog');
// Call "get_last_ten_entries" function and assign its result to a variable
$data['query'] = $this->Blog->get_last_ten_entries();
// Load view and pass our variable to display data to the user
$this->load->view('blog', $data);
}
}
模型
class Blogmodel extends CI_Model {
var $title = '';
var $content = '';
var $date = '';
function __construct()
{
// Call the Model constructor
parent::__construct();
}
// Query the database to get some data and return the result
function get_last_ten_entries()
{
$query = $this->db->get('entries', 10);
return $query->result();
}
// ... truncated for brevity
}
编辑
这是非常基本的东西,我强烈推荐 reading through the documentation 和 walking through some tutorials,但无论如何我会尽力提供帮助:
根据您在下面的评论,您需要以下内容(诚然,这很模糊):
- 从查询中获取一位数据
- 将其传递给变量(您的意思是“将其分配给变量”吗?)
- 验证数据库中的那部分数据
请仔细阅读Database class documentation。这实际上取决于您正在运行的特定查询以及您想要从中获取的数据。根据上面的示例,在您的模型中的某些函数中它可能看起来像这样(请记住,这完全是任意的,因为我不知道您的查询是什么样的或您想要什么数据):
// Get a single entry record
$query = $this->db->get('entries', 1);
// Did the query return a single record?
if($query->num_rows() === 1){
// It returned a result
// Get a single value from the record and assign it to a variable
$your_variable = $this->query()->row()->SOME_VALUE_FROM_RETURNED_RECORD;
// "Validate" the variable.
// This is incredibly vague, but you do whatever you want with the value here
// e.g. pass it to some "validator" function, return it to the controller, etc.
if($your_variable == $some_other_value){
// It validated!
} else {
// It did not validate
}
} else {
// It did not return any results
}