Welcome Guest, Not a member yet? Register   Sign In
I think I'm missing something with CI regarding controllers and views!
#1

[eluser]Flemming[/eluser]
Hi guys,

I've been using CI for a couple of months now and I'm really happy with it. Really happy!

But I think I'm missing some logic ...

here's the example ...

I have a controller, lets call it 'products'.

I have a function/method in that controller, lets call it 'view_category_products'.

'view_category_products' loads a view ... 'productList.php' which displays all the products in the specified category, using a foreach loop.

I call the 'view_category_products' method : http://mysite/admin/products/view_category_products/1

so - I will see all the category 1 products displayed (using the productList.php view).

so far so good!

here's the bit I'm confused by ...

In my view I would like to display the product name AND do a lookup on each product WHILE I'm INSIDE the foreach loop, for example, using the productID to check if the product has any related products.

I'll try again to explain it more clearly!

In my foreach loop in my view I would like to call a function to perform additional work.

I can't think how I can do this in the controller BEFORE it passes everything to the view. I can only think that I have to do it from the view.

I expect this is just a question of changing my programming habits! Can anyone a) understand what I'm talking about? b) help me understand how to keep my view clean and free of calls back to controllers?

thanks in advance!

Flemming
#2

[eluser]xwero[/eluser]
The best way to do this is in the model. To keep it simple and maintainable you create a method in your model named like the page, in your case products. And in that method you create a sql statement with all the data you need to create the content for the page. You can even format the data as long as there is no html added, this can also be done in the sql statement. and the only thing you have to do is loop the returned output.
#3

[eluser]Flemming[/eluser]
Thanks xwero! I love the sound of that and am looking into it right now - but is there any chance of some example code as I think I'm still in the mindset of a procedural PHP developer!
#4

[eluser]Spockz[/eluser]
The most simple way is this:

In your model you put logic for loading an row. Due to the simplified models in CI it's a custom to also put the logic for loading multiple instances of the model in that same model.

In your controller you put the logic of the application, as well as calling the models and stuff. Then you pass your data you have generated to the view.

The view then displays it nicely. So you have no actual application logic in your views.
#5

[eluser]xwero[/eluser]
Code:
// model
function products($id)
{
   $query = $this->db->query('the query here'); // you could also use AR methods
   return $query->result(); // or result_array()
}
// controller
function products($id)
{
   // check if category exists
   if( ! $this->productsmodel->is_category($id))
   {
      // go to an error page or landing page the has an overview of the categories
   }
   else
   {
      // you could us an array instead and add it to the load->view as second parameter
      $this->load->vars(array('products'=>$this->productsmodel->products()));
      $this->load->view('category_products');
   }
}
// view
<table>&lt;!-- legitimate use of table it exists ;) --&gt;
<tr>
<th>Product</th>
<th>Related</th>
</tr>
&lt;?php foreach($products as $product): ?&gt;
<tr>
<td>
<h2>&lt;?php echo $product->name ?&gt;</h2>
&lt;?php echo $product->description ?&gt;
</td>
<td>
&lt;?php if(count($product->related) > 0): foreach($product->related as $related): ?&gt;
<p><a href="/product/&lt;?php echo $related->url ?&gt;">&lt;?php echo $related->name ?&gt;</a></p>
&lt;?php endforeach; endif ?&gt;
</td>
</tr>
&lt;?php endforeach ?&gt;
</table>
I didn't want to make it too complicated so i took a few liberties but i think you get the idea.
#6

[eluser]Flemming[/eluser]
Brilliant! Thanks xwero! You've explained it excellently and yes, I do get the idea!

Much appreciated! :-)




Theme © iAndrew 2016 - Forum software by © MyBB