将sql查询结果从控制器传递到带有代码点火器的视图中

时间:2022-03-30 11:33:11

So I'm having this problem, it should be pretty simple, but I don't know why I can't figure it out. I"m new to the whole idea of MVC, and I'm trying to pass a database query from my controller into a view and display the results in the view. The way I'm doing it now says "undefined variable, sql" when I load the view. This is what I have:

我有这个问题,应该很简单,但我不知道为什么我解不出来。我对MVC的整个概念并不熟悉,我正在尝试将一个数据库查询从我的控制器传递到一个视图中,并在视图中显示结果。我现在的做法是在加载视图时显示“未定义变量,sql”。这就是我所拥有的:

CONTROLLER

控制器

function make_login()
{
    //Select list of departments for dropdown
    $this->load->database();
    $sql = $this->db->query('SELECT departmentName FROM department ORDER BY departmentName ASC');



    $this->load->view('siteheader.php');
    $this->load->view('makelogin.php', $sql->result_array());
    $this->load->view('sitefooter.php');
}

VIEW

视图

<?php
 foreach($sql->result_array() as $row)
    {
        echo $row['departmentName'];
    }
    ?>

(If I just echo it out in the controller, it displays the results)

(如果我只是在控制器中进行回显,它就会显示结果)

Any help would be awesome... THANKS!

任何帮助都是可怕的……谢谢!

4 个解决方案

#1


16  

few tips ~

一些小贴士~

your make_login should be in a model. your controller will look something like this:

您的make_login应该是一个模型。你的控制器看起来是这样的:

function make_login
{
    $this->load->model('login_model'); // whatever you call it

    $data['departments'] =  $this->login_model->get_departments();

    /* note - you don't need to have the extension when it's a php file */
    $this->load->view('siteheader');
    $this->load->view('makelogin', $data);
    $this->load->view('sitefooter');
}

now in your model, have something like:

在你的模型中,有如下内容:

function get_departments()
{
    $sql = $this->db->query('SELECT departmentName FROM department ORDER BY departmentName ASC');
    return $sql->result();
    /* you simply return the results as an object
     * also note you can use the ActiveRecord class for this...might make it easier
     */
}

and finally, your view:

最后,你的观点:

<?php
    foreach($departments as $store)
    {
        echo  $store->name . '<br />'; // your fields/whatever you want to output.
    }
?>

#2


2  

The SQL query should be done in the model.

应该在模型中执行SQL查询。

Cheap mnemonic device: the D in model = database.

便宜的助记设备:D in model = database。

In the Controller, you assign part of the $data array to the results of the query:

在控制器中,将$data数组的一部分分配给查询的结果:

$this->load->model('blog_model');
$data['posts'] = $this->blog_model->getPosts();

// Load the view with the data
$this->load->view('blog', $data);

In the Model, you do the actual query:

在模型中,您执行实际查询:

public function getPosts()
{
    // Method chaining supported in PHP 5
    $this->db->select('*')->from('posts');

    // Assign the query object to a variable
    $query = $this->db->get();

    // We don't want to return an empty result, so let's handle that error somehow
    if (!$query->num_rows() > 0) {
        die("There are no posts in the database.");
    }

    // Make a new array for the posts
    $posts = array();

    // For the purposes of this example, we'll only return the title
    foreach ($query->result() as $row) {
        $posts[$row->id] = $row->title;
    }

    // Pass the result back to the controller
    return $posts;
}

Now, in the view, each element of $data will be its own variable:

现在,在视图中,$data的每个元素都是它自己的变量:

    <div class="post">
    <?php foreach ($posts as $id => $title) : ?>
        <h1 class="post-title"><?php echo $title; ?> (ID: <?php echo $id; ?>)</h1>
        <p class="post-content">
        .......
        </p>
        <?php endforeach; ?>
    </div>

That's it!

就是这样!

#3


1  

It seems that either CI is confusing you or you are also new to PHP. They are just functions so you can't pass variables like that.

看起来要么CI让您感到困惑,要么您对PHP也不熟悉。它们只是函数,所以不能像这样传递变量。

When passing an associative array it will take the key and make that into a variable with the value in the array, using native PHP functions. What Ross said is exactly what you are supposed to do.

当传递一个关联数组时,它将使用本地PHP函数将密钥转换为数组中的值。罗斯说的正是你应该做的。

Model: all database stuff Controller: uses models to pass variables to views View: outputs the variables (a view should never have any sql in it)

模型:所有数据库内容控制器:使用模型将变量传递给视图:输出变量(视图中不应该有sql)

Also note that result and result_array have the same data but result returns objects and result_array returns associative arrays.

还要注意,result和result_array具有相同的数据,但result返回对象,result_array返回关联数组。

#4


0  

 foreach($array as $row)
    {
        echo $row['departmentName'];
    }
    ?>

You need to pass the $array in. I am not sure what ->load->view() does and how it treats the incoming parameters.

您需要将$数组传入。我不确定>load->view()做什么,以及它如何处理传入的参数。

#1


16  

few tips ~

一些小贴士~

your make_login should be in a model. your controller will look something like this:

您的make_login应该是一个模型。你的控制器看起来是这样的:

function make_login
{
    $this->load->model('login_model'); // whatever you call it

    $data['departments'] =  $this->login_model->get_departments();

    /* note - you don't need to have the extension when it's a php file */
    $this->load->view('siteheader');
    $this->load->view('makelogin', $data);
    $this->load->view('sitefooter');
}

now in your model, have something like:

在你的模型中,有如下内容:

function get_departments()
{
    $sql = $this->db->query('SELECT departmentName FROM department ORDER BY departmentName ASC');
    return $sql->result();
    /* you simply return the results as an object
     * also note you can use the ActiveRecord class for this...might make it easier
     */
}

and finally, your view:

最后,你的观点:

<?php
    foreach($departments as $store)
    {
        echo  $store->name . '<br />'; // your fields/whatever you want to output.
    }
?>

#2


2  

The SQL query should be done in the model.

应该在模型中执行SQL查询。

Cheap mnemonic device: the D in model = database.

便宜的助记设备:D in model = database。

In the Controller, you assign part of the $data array to the results of the query:

在控制器中,将$data数组的一部分分配给查询的结果:

$this->load->model('blog_model');
$data['posts'] = $this->blog_model->getPosts();

// Load the view with the data
$this->load->view('blog', $data);

In the Model, you do the actual query:

在模型中,您执行实际查询:

public function getPosts()
{
    // Method chaining supported in PHP 5
    $this->db->select('*')->from('posts');

    // Assign the query object to a variable
    $query = $this->db->get();

    // We don't want to return an empty result, so let's handle that error somehow
    if (!$query->num_rows() > 0) {
        die("There are no posts in the database.");
    }

    // Make a new array for the posts
    $posts = array();

    // For the purposes of this example, we'll only return the title
    foreach ($query->result() as $row) {
        $posts[$row->id] = $row->title;
    }

    // Pass the result back to the controller
    return $posts;
}

Now, in the view, each element of $data will be its own variable:

现在,在视图中,$data的每个元素都是它自己的变量:

    <div class="post">
    <?php foreach ($posts as $id => $title) : ?>
        <h1 class="post-title"><?php echo $title; ?> (ID: <?php echo $id; ?>)</h1>
        <p class="post-content">
        .......
        </p>
        <?php endforeach; ?>
    </div>

That's it!

就是这样!

#3


1  

It seems that either CI is confusing you or you are also new to PHP. They are just functions so you can't pass variables like that.

看起来要么CI让您感到困惑,要么您对PHP也不熟悉。它们只是函数,所以不能像这样传递变量。

When passing an associative array it will take the key and make that into a variable with the value in the array, using native PHP functions. What Ross said is exactly what you are supposed to do.

当传递一个关联数组时,它将使用本地PHP函数将密钥转换为数组中的值。罗斯说的正是你应该做的。

Model: all database stuff Controller: uses models to pass variables to views View: outputs the variables (a view should never have any sql in it)

模型:所有数据库内容控制器:使用模型将变量传递给视图:输出变量(视图中不应该有sql)

Also note that result and result_array have the same data but result returns objects and result_array returns associative arrays.

还要注意,result和result_array具有相同的数据,但result返回对象,result_array返回关联数组。

#4


0  

 foreach($array as $row)
    {
        echo $row['departmentName'];
    }
    ?>

You need to pass the $array in. I am not sure what ->load->view() does and how it treats the incoming parameters.

您需要将$数组传入。我不确定>load->view()做什么,以及它如何处理传入的参数。