Welcome Guest, Not a member yet? Register   Sign In
Can echo out of controller but don't understand how pass to view?
#1

[eluser]2think[/eluser]
Hi everyone, I'm new to Code Igniter and come from a C/Java background. I tried doing some simple db stuff and I think I need a conceptual understanding or pseudo-code if possible. I can load a Controller and echo a table (customers) like this(using CI example):

class Allusers extends Controller {
function index()
{
$query = $this->db->query('SELECT name, title, email FROM my_table');

foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->email;
}

echo 'Total Results: ' . $query->num_rows();


However, if I try to pass either $query or $row to the View, it doesn't display anything and throws errors about it being a non-object.

I think I really need to understand what I should be passing to the View to echo there instead of the Controller? I do understand MVC but guess not well enough!

I'd ideally like to understand how to pass (and what to pass) objects/variables from my Models to my Controllers then to my Views.

Thanks in advance for any help.
#2

[eluser]Beren[/eluser]
You need to assign your data to a variable, such as $view_data:
Code:
$view_data['total_results'] = $query->num_rows();
and then pass that as an argument to the call
Code:
$this->load->view('view_file', $view_data);

This will trigger CI to load the file 'view_file.php' located in your application/views driectory with each key of the $view_data array as a the name of a variable available within that view, so for example in view_file.php you could have
Code:
...
<tr><td>Total Results</td><td>&lt;?=$total_results?&gt;</td></tr>
....

Obviously with a complex application you'll want to be able to put views within other views but theres a thousand and one posts that already cover that, and a lot of good libraries that help you achieve that, and much more.

Hope it helps
#3

[eluser]2think[/eluser]
Thanks for the help & suggestions. I think it has helped me and I'll post with my (hopefully successful) results.
#4

[eluser]Edemilson Lima[/eluser]
You must set a variable with your result and them pass it to your view:

Code:
$data['results']=$query->result();
$this->load->view('myview',$data);

Then you must place the foreach() into your view file, using the alternative PHP syntax:

Code:
<table>
&lt;?php foreach($results as $row): ?&gt;
<tr>
    <td>&lt;?=$row['field1']?&gt;</td>
    <td>&lt;?=$row['field2']?&gt;</td>
</tr>
&lt;?php endforeach; ?&gt;
</table>

By this way, you also keep your presentation logic separated from your controller.
#5

[eluser]sandwormusmc[/eluser]
Code:
class Allusers extends Controller {
function index()
{
// this should be a function in a model, [b]not[/b] in the controller
// $query = $this->db->query(’SELECT name, title, email FROM my_table’);

/*foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->email;
}

echo ‘Total Results: ‘ . $query->num_rows();*/

$this->load->model('whatever',,TRUE);
$results=$this->whatever->getResults();
$data = array(
'results'=>$results
);
$this->load->view('showme',$data);

In your model,

Code:
class whatever extends Model {

    public function __construct() {
        parent::Model();
    }

    public function getResults($vars=NULL) {
        // leave $vars in there if you want dynamic SQL calls by vars in $vars
        $sql='SELECT something FROM my_table';
        $results=$this->db->query($sql);
        // result_array() will return an array of results (obvious i guess)
        // otherwise a CI db resource object is all that is returned
        return $results->result_array();
    }
}

In your view,

Code:
foreach($data['results'] as $item=>$value){
        echo "item: $item, value: $value<br>";
    }

To reiterate ... keep your SQL out of controller logic. To keep your app from being tied to only one type of type of database, all of your database logic should only be in the model. Controllers should only guide the application on where to go next, manipulate data returned by the models, and package it up neatly for the views to present.
#6

[eluser]2think[/eluser]
Thanks for all the help, wish I could do the same for others once I'm up to speed. I hope to post some results later with success.

Awesome user community.
#7

[eluser]Jackattack[/eluser]
I'm definitely understanding the idea behind what is going on, however trying to implement the examples found in this thread only results in errors. I'm able to pass 1 result to the view and have it display correctly but passing my entire result set doesn't work. I'm getting errors with my foreach statement

Code:
&lt;?php foreach($results as $row): ?&gt;
<tr>
    <td>&lt;?=$row['field1']?&gt;</td>
    <td>&lt;?=$row['field2']?&gt;</td>
</tr>
&lt;?php endforeach; ?&gt;

That is close to what I want to do but it spits out errors about undefined variables and invalid arguments. It seems that I am so close but have a small syntax problem. Thanks for any help
#8

[eluser]Pascal Kriete[/eluser]
Are you passing in a database result? Or are you just trying to pass in a bunch of variables?
#9

[eluser]Jackattack[/eluser]
A database result
#10

[eluser]Pascal Kriete[/eluser]
Hmm, ok - could you post some more of your code? What you have in your view is correct. What does your model return?




Theme © iAndrew 2016 - Forum software by © MyBB