Welcome Guest, Not a member yet? Register   Sign In
Forced to run DB Query from View (Bad practice?)
#10

[eluser]ROFLwtfBBQ?[/eluser]
I'm not sure how I would solve this with "one query" as people are suggesting. However, I moved the query into the model now, but I'm not entirly convinced about the benefits of it. It seems like alot of extra code, and a waste of performance having to first sort the results into the data array in the model, then iterate through the array in the view. But i guess that's an extra neccesary step to keep the business and presentation logic apart.

In the model:

Code:
$query = $this->db->get('categories');
foreach ($query->result() as $category)
{
     $anotherQuery = $this->db->get_where('subcategory', array('category_id' => $category->id));
     foreach ($anotherQuery->result() as $subcategory)
     {
         $data['categories'][$category->title][$subcategory->id]['title'] = $subcategory->title;
         $data['categories'][$category->title][$subcategory->id]['body'] = $subcategory->body;
     }
}

And in the view

Code:
<?php foreach ($categories as $category => $sub):?>
    <h2>&lt;?php echo $category;?&gt;</h2>
    &lt;?php foreach ($sub as $subcategories => $subcategory):?&gt;
        <h3>&lt;?php echo $subcategory['title']?&gt;</h3>
        <p>&lt;?php echo $subcategory['body']?&gt;</p
    &lt;?php endforeach;?&gt;    
&lt;?php endforeach;?&gt;


Messages In This Thread
Forced to run DB Query from View (Bad practice?) - by El Forum - 05-26-2008, 09:10 AM
Forced to run DB Query from View (Bad practice?) - by El Forum - 05-26-2008, 09:16 AM
Forced to run DB Query from View (Bad practice?) - by El Forum - 05-26-2008, 10:45 AM
Forced to run DB Query from View (Bad practice?) - by El Forum - 05-26-2008, 12:30 PM
Forced to run DB Query from View (Bad practice?) - by El Forum - 05-26-2008, 01:39 PM
Forced to run DB Query from View (Bad practice?) - by El Forum - 05-26-2008, 04:31 PM
Forced to run DB Query from View (Bad practice?) - by El Forum - 05-26-2008, 05:23 PM
Forced to run DB Query from View (Bad practice?) - by El Forum - 05-26-2008, 05:30 PM
Forced to run DB Query from View (Bad practice?) - by El Forum - 05-26-2008, 06:24 PM
Forced to run DB Query from View (Bad practice?) - by El Forum - 05-27-2008, 04:04 AM
Forced to run DB Query from View (Bad practice?) - by El Forum - 05-27-2008, 05:14 AM
Forced to run DB Query from View (Bad practice?) - by El Forum - 05-27-2008, 06:42 AM
Forced to run DB Query from View (Bad practice?) - by El Forum - 05-27-2008, 07:39 AM
Forced to run DB Query from View (Bad practice?) - by El Forum - 07-28-2008, 10:37 PM
Forced to run DB Query from View (Bad practice?) - by El Forum - 07-28-2008, 10:53 PM
Forced to run DB Query from View (Bad practice?) - by El Forum - 07-29-2008, 07:10 AM
Forced to run DB Query from View (Bad practice?) - by El Forum - 07-29-2008, 05:11 PM
Forced to run DB Query from View (Bad practice?) - by El Forum - 07-29-2008, 06:17 PM
Forced to run DB Query from View (Bad practice?) - by El Forum - 07-29-2008, 07:37 PM
Forced to run DB Query from View (Bad practice?) - by El Forum - 07-29-2008, 08:55 PM
Forced to run DB Query from View (Bad practice?) - by El Forum - 07-29-2008, 10:38 PM



Theme © iAndrew 2016 - Forum software by © MyBB