Welcome Guest, Not a member yet? Register   Sign In
Showing SQL Query results in a View
#1

[eluser]birko19[/eluser]
Hi,

First post here Smile

I just started working with CI and I was wondering, how do you show the values you retrieved from a mySQL DB in the view? for instance I have two records that I received and I would like to show them in a basic html list, how would I go around doing that?

Here's my controller:

Code:
function index()
    {
        $this->load->database();
        
        $query = $this->db->query("SELECT * FROM msgboard");
            
        foreach ($query->result() as $row)
        {
           $myData['id'] = $row->id;
           $myData['name'] = $row->name;
           $myData['email'] = $row->email;
           $myData['time'] = $row->time;
           $myData['message'] = $row->message;
        }

        $this->load->view('feedback', $myData);
    }

But whenever I try to load it up in the View I get undefined errors, here's what I tried to do in the view:

Code:
<ul>
    &lt;?php for($i=0;$i<sizeof($myData);$i++) { ?&gt;

    <li>&lt;?php echo $myData[$i][$id]; ?&gt;</li>
    <li>&lt;?php echo $myData[$i][$name]; ?&gt;</li>
    <li>&lt;?php echo $myData[$i][$email]; ?&gt;</li>
    <li>&lt;?php echo $myData[$i][$time]; ?&gt;</li>
    <li>&lt;?php echo $myData[$i][$message]; ?&gt;</li>

    &lt;?php } ?&gt;
</ul>

The error it says is:

Quote: A PHP Error was encountered

Severity: Notice

Message: Undefined variable: myData

Filename: views/feedback.php

Line Number: 21

I know the array works because when I echo it all the values are there, but I have no clue how to load it in the View, please help, thanks.
#2

[eluser]LifeSteala[/eluser]
Hi,
Welcome to Code Igniter!

Firstly, you should look up Models in the user guide. Models is the layer in which your application communicates with the database. By not utilizing Models, you are moving away from the Model-View-Controller (MVC) approach.

So let me try to help you.

Model
Code:
class Messages extends Model
{
    function Messages()
    {
        parent::Model();
    }
    
    function getMessages()
    {
        $this->db->select('*');
        $this->db->from('msgboard');
        $result = $this->db->get();

        if (!$result) {
            return false;
        } else {
            return $result;
        }
    }
}

Controller
Code:
class Thiscontroller extends Controller
{
    function Thiscontroller()
    {
        parent::Controller();
        $this->load->database(); // This should be autoloaded

        $this->load->model('messages');
    }
    
    function index()
    {
        $myData['query'] = $this->messages->getMessages();
        $this->load->view('feedback', $myData);
    }
}

$myData is an array of key / value pairs. In CI, when passing an array to a view, the key becomes it's own variable which can be directly used as shown below in the foreach statement.

View
Code:
<ul>
    &lt;?php foreach ($query->result() as $row) { ?&gt;
    <li>&lt;?php echo $row->id; ?&gt;</li>
    <li>&lt;?php echo $row->name; ?&gt;</li>
    <li>&lt;?php echo $row->email; ?&gt;</li>
    <li>&lt;?php echo $row->time; ?&gt;</li>
    <li>&lt;?php echo $row->message; ?&gt;</li>
    &lt;? } ?&gt;
</ul>

I'm processing query results in the view as in this case, you are only wanting to view the data/results. If there were to be data manipulation, generally you would do that in the controller then pass whatever you need to display through.

I hope all that makes sense.

Note: This code is untested. Errors may arise.
#3

[eluser]birko19[/eluser]
Thanks a lot LifeSteala, that was very nice and helpful, it worked like a charm Smile
#4

[eluser]birko19[/eluser]
I have another question if you don't mind, is it possible to pass two values into a View from one Controller? We know that we can do the following:

Code:
$myData['query'] = $this->messages->getMessages();
$this->load->view('feedback', $myData);

But is it possible to do something like this?

Code:
$data['someData'] = "Hello"      
$myData['query'] = $this->messages->getMessages();
$this->load->view('feedback', $myData, $data);

What are the options to that?

Thanks.
#5

[eluser]LuckyFella73[/eluser]
Hi Birko,

you can send only one data-set to the view. But this
data set can contain as many variables or array as you
need.

Your question was if this is possible:
Code:
$data['someData'] = "Hello"      
$myData['query'] = $this->messages->getMessages();
$this->load->view('feedback', $myData, $data);

What you would have to do to get the desired result is:
Code:
$myData['someData'] = "Hello"      
$myData['query'] = $this->messages->getMessages();
$this->load->view('feedback', $myData);

Then in your view file you can acces the DB stuff like posted
in the earlier thread and additionally the variable
Code:
$someData
and its value.

Like this:
Code:
<h1>&lt;?php echo $someData; ?&gt;</h1>
<ul>
    &lt;?php foreach ($query->result() as $row) { ?&gt;
    <li>&lt;?php echo $row->id; ?&gt;</li>
    <li>&lt;?php echo $row->name; ?&gt;</li>
    <li>&lt;?php echo $row->email; ?&gt;</li>
    <li>&lt;?php echo $row->time; ?&gt;</li>
    <li>&lt;?php echo $row->message; ?&gt;</li>
    &lt;? } ?&gt;
</ul>

My english is quite limited but I hope you understand what I mean ..
#6

[eluser]birko19[/eluser]
Thanks a lot Lucky, now it's all making sense.

Cheers...




Theme © iAndrew 2016 - Forum software by © MyBB