Welcome Guest, Not a member yet? Register   Sign In
Where do you do your loops and query results?
#1

[eluser]NachoF[/eluser]
What is the best practice when you want to get a table from a databse?... I know the model is supposed to do the query, but where are you supposed to do the query->result()?? the foreach?? and the "<td> $row['rowname'] </td>"... I want tofollow the MVC pattern as strictly as possible.
#2

[eluser]Tom Schlick[/eluser]
well i do the query result in the model and return it if its not blank and return false if it is, i do the foreach with the html in the view, but any calculations or any other operations get done in the controller or libraries before hitting the view.
#3

[eluser]jedd[/eluser]
What he said. In general terms:

Anything involving query-> stuff is done in the model.

Anything involving HTML tags is done in the view.

Everything else is done in the controller. (This is the least true of these statements, but it'll do for now.)
#4

[eluser]JulianM[/eluser]
Hi,

I just use something like this in the "view":

Code:
&lt;? foreach($list->result() as $item): ?&gt;
#5

[eluser]CroNiX[/eluser]
I usually do everything in the model and return an array back to the controller (not the query result object).

In controller:

Code:
$data['stats'] = $this->my_model->get_stats($client_id);

In model:

Code:
return $this->db->where('client_id', $client_id)->get($table)->result_array();

Then when I send $data to the view I just have to loop over the array.
#6

[eluser]siubie[/eluser]
i always done my query in model and return an array to controller.
usually i use forloop in the view but sometimes i used it in controller
#7

[eluser]Matt Egan[/eluser]
It all depends for me. Sometimes, I have the HTML generated in the model. Other times, I do otherwise. But, anyways, for example, if I have a navigation list that I want to create. If I know, in all cases how its going to be formatted, I just generated the UL with the LI's inside, inside the model. Here's my code, this is in my model.

Code:
function pagenavigation()    {
        
        $this->db->order_by('Title', "DESC");
        $query = $this->db->get('pages');
        
        $result = "<ul>";
        
        $result .= "<li><a >Blog</a></li>";
        
        foreach($query->result() as $row)    {
            
            $result .= "<li><a >id."\"\\>".$row->navtitle;
            $result .= "<li></a>";
            
        }
        
        $result.="</ul>";
        
        $query->free_result();
        
        echo $result;        
        
    }

I would use this in my view by...

Code:
$this->mymodel->pagenavigation();

And it would generate something like

Code:
<ul>

     <li><a href="http://www.example.net/website/">Blog</a></li>
     <li><a href="http://www.example.net/website/page/index/1">Page<li></a>

</ul>

Hope that helps, note that I only do this on special occasions, but I'm just showing you another way that you could go about things. Really, its all up to you, code is never set in stone, code however you like.




Theme © iAndrew 2016 - Forum software by © MyBB