5

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:

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 Answers 4

16

few tips ~

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

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.
    }
?>
Sign up to request clarification or add additional context in comments.

4 Comments

see how having a method in your login_model now makes more semantic sense? it's obvious what it does, you can extend it easily...rewrite the get_departments function to take an ID as a parameter, then return a specific department. No ID? Return them all.
yea, that makes sense... but it seems like a lot more work if I'm only going to use that query in that one spot... why don't I just put it in the controller?
Because that's how MVC is supposed to work, or rather, the concept. You don't have to do anything. But what happens, if you one day need to add another function or query? Will that go in the controller as well? By breaking in to MVC you can create far more manageable code.
hmm seems to make sense... and then if I have other sql statements that I use more often, I can just call that model instead of redoing it... I'll give that a try. Thanks
3

The SQL query should be done in the model.

Cheap mnemonic device: the D in model = database.

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

$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:

    <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!

2 Comments

you should probably structure your answer around the question more specifically
Copying and pasting is no way to learn anything. I posted a generic example so the OP can work with it and actually understand what the code does.
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.

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.

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)

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

Comments

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.

2 Comments

load view just displays an html file
$this->load->view($foo) is one way for CI to output your html, where $foo is the name of a view. The function may take an array of $data to parse variables and the like. more @ codeigniter.com/user_guide/general/views.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.