Codeigniter& PHP。
我想从数据库中检索单个位数据,将该单个数据位转换为变量并将其传递给控制器并将该数据用作单个变量?例如,我可以使用控制器中的数据执行if $ string = $ string等等。
如果有人能够制作模型和控制器的示例,我们将不胜感激。
答案 0 :(得分:5)
这非常直截了当taken right from CodeIgniter's documentation,您应该仔细阅读(代码中的注释主要是我的):
控制器
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
}