Codeigniter,将变量从模型传递到控制器

发布于 2025-01-01 09:08:23 字数 160 浏览 0 评论 0原文

Codeigniter 新手PHP。

我想从数据库中检索一位数据,将该一位数据转换为一个变量并将其传递给控制器​​并将该数据用作单个变量?例如,我可以使用控制器中的数据执行 if $string=$string 等操作。

如果有人可以提供模型和控制器的示例,我们将不胜感激。

New to Codeigniter & PHP.

I would like to retrieve a single bit data from a database, turn that single bit of data into a variable and pass it to the controller and use that data as a single variable? For example I could do a if $string=$string and so on, with the data in the controller.

If anyone could produce an example of a model and controller it would be gratefully appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀念你的温柔 2025-01-08 09:08:23

这非常简单,直接取自 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);
    }
}

模型

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

}

编辑

这是非常基本的东西,我强烈推荐阅读文档浏览一些教程,但我会无论如何尝试提供帮助:

根据您下面的评论,您想要以下内容(诚然,这是相当模糊的):

  1. 从查询中获取一点数据,
  2. 将其传递给变量(您的意思是“将其分配给变量” “?)
  3. 验证来自的数据位数据库

请阅读数据库类文档。这实际上取决于您正在运行的特定查询以及您想要从中获取什么数据。根据上面的示例,在模型中的某些函数中,它可能看起来像这样(请记住,这完全是任意的,因为我不知道您的查询是什么样子或您想要什么数据):

// 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
}

This is pretty straightforward and taken right from CodeIgniter's documentation, which you should definitely read through (comments in the code are mostly mine):

The Controller

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);
    }
}

The Model

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

}

Edit

This is pretty basic stuff and I highly recommend simply reading through the documentation and walking through some tutorials, but I'll try to help anyway:

Based on your comment below, you want the following (which, admittedly, is pretty vague):

  1. get a single bit of data out of the query
  2. pass that to a variable (do you mean "assign that to a variable"?)
  3. validate that bit of data from the database

Please read over the Database class documentation. This really depends on the specific query you're running and what data you want from it. Based on the above example, it might look something like this in some function in your model (Keep in mind this is completely arbitrary as I have no idea what your query looks like or what data you want):

// 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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文