As pinadelsai already said, logic tells to put your query inside the Model. That servers 2 purposes: 1) mantain a stricter and better coding practice by separating the three logics bheind MVC; 2) keep your code organized and easier to mantain the day you want to make some changes in your query.
It looks you're calling the query just in a single rendred view, but what if you call it in more than once, and you decide one day to change it? You'll need to check all the views and make changes in each of them; by keeping the logic inside the model, you just change one method and you serve the corrected results to all the views at once.
A small addition and a piece of advice: you needn't necessarily call an array-returning function to have results in array form. Doing something like (in your Model):
$query = $this->db->get('mytable'); // this is your fetching;
$data = array();
foreach($query->result() as $row)
{
$data[] = $row;
}
return $data;
You will have a properties which is always in array form ($data), no matter if you choose to use result() and later change it to result_array(); your view will keep the same foreach logic, you will just need to change how you call the values (using array notation or object notation).
UPDATE:
as per your comment, you can either use a different view for when you have no results (but that strongly depends on your design. More commonly, you let the model pass with its data the information on the amount for results. For example:
$query = $this->db->get('mytable'); // this is your fetching;
$data = array();
if($query->num_rows() > 0)
{
foreach($query->result() as $row)
{
$data[] = $row;
}
}
return $data;
In this case, $data
starts as an empty array. If rows are returned, the array is populated, otherwise is returned empty as it is. And then, you decide in your view (or in our controller, if you need to load a whole different view in case) how to differentiate this condition by doing like you already do in your code.
Apart from all of this, there's no enforcing law you need to compel to. You can place whatever logic you want inside your view and CI will work nonetheless. Doing a strict separation of Business logic, Data manipulation and Data display just ensures your app will be much more managable in the future; even if you'll be the only one mantaining your code, I'm pretty sure six month from now, when you go back to those views containing a query, you'll curse yourself from not having done the "right" MVC way.