Ale*_*cey 2 variables controller model codeigniter
Codeigniter和PHP新手.
我想从数据库中检索单个位数据,将该单个数据位转换为变量并将其传递给控制器并将该数据用作单个变量?例如,我可以使用控制器中的数据执行if $ string = $ string等等.
如果有人能够生成模型和控制器的示例,将非常感激.
这非常简单,直接来自CodeIgniter的文档,您应该仔细阅读(代码中的注释主要是我的):
控制器
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);
}
}
Run Code Online (Sandbox Code Playgroud)
该模型
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
}
Run Code Online (Sandbox Code Playgroud)
编辑
这是非常基本的东西,我强烈建议您只需阅读文档并阅读一些教程,但无论如何我都会尝试提供帮助:
根据您在下面的评论,您需要以下内容(诚然,这是非常模糊的):
请仔细阅读Database类文档.这实际上取决于您正在运行的特定查询以及您希望从中获取哪些数据.基于上面的例子,它可能在你的模型中的某个函数中看起来像这样(请记住,这完全是任意的,因为我不知道你的查询是什么样的或你想要什么数据):
// 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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7440 次 |
| 最近记录: |