Welcome Guest, Not a member yet? Register   Sign In
CI for dummies
#1

[eluser]zool[/eluser]
Hi all,
I am a very very new CI user...or better...I'm trying to use CI Smile

Before start to do something I would like to understand better how CI works. I already read something in the user doc and some posts on the forum but I don't have a clear idea how to "solve" a situation.

I know that there is only a Controller for request but... if, for example, I have in a page this situation:
1) header (no problem) -> view
2) navigation (no problem) -> probably view
3) news -> ???
4) product description -> ???
5) footer (no problem) -> view

how can I "describe" it? If in the page there is only an "element" (like news) I can create a Controller for it and it works (like blog in the video guide). But in this case I should have two Controllers (one for the news "element" and one for the "product element"). Or should be a Controller for the page with the two Model? (news and product)?

Probably I didn't understood anything about CI!!! :cheese:

Sorry for the long post and my bad English :-P

Bye and thanks in advance
#2

[eluser]minimal design[/eluser]
A good way to get started is to download other people script and analyze them... MVC was a little confusing for me to at first... just keep going at it and at some point it will "click."

Basically, for your scenario, you can use a view for header, nav, footer, news content, and product description.

The header, nav, footer will be almost static plain PHP files, while your news and description views will have variables that will be replaced by data assigned to those variables by the controller. Sor for the news, your new model will get the news content from the database, then your news controller will pass that data to the news view.

This is super simplified and is just to give you an idea of the "flow"

model:
Code:
function get_single($article_uri) {
    $this->db->where('uri',$article_uri);
    $query = $this->db->get($this->articles_table, 1);
    
    if ($query->num_rows() == 1) {
        $article = $query->row_array();
        return $article;
    } else {
        return false;
    }
}

controller:
Code:
function read($article_title) {
    $data['article'] = $this->articles_model->get_single($article_title);
    $this->load->view('header', $data);
    $this->load->view('article_single');
    $this->load->view('footer');
}

view:
Code:
<h2>&lt;?= $article['title']; ?&gt;</h2>
<div class="body">
    &lt;?= $article['body']; ?&gt;
</div>&lt;!-- .body --&gt;

If you need 2 sets of data , just retrieve it with 2 models, load both in your controller and you'll have both sets of data available in your (single) view.

Hope that helps...
#3

[eluser]nmweb[/eluser]
They keyword for me in understanding and working with CI/MVC is DRY (Don't repeat yourself). The minute you repeat a line of code you should think for a minute and consider if it wouldn't be better to abstract the code a little.

Since you're a beginner in CI I would suggest what minimal design says, two sets of data, use two models. However, normally I'd opt for one controller one function.
#4

[eluser]minimal design[/eluser]
well, in his example I'm assuming news and products are 2 tables in the database, 2 different sets of data, so I think 2 models makes more sense. Most likely it will be used in many different places and keeping the models separated seems to be a more modular architecture that will help minimize code redundancy. If I'm wrong, please expand on how you'd do it... I'm always curious to learn new things Smile
#5

[eluser]zool[/eluser]
So I don't have two controllers (one for product and one for news) but only one with two Models.
Is it correct?
#6

[eluser]minimal design[/eluser]
Keeping in mind that it's not a "do or don't" situation... this just *one way* to do it, and this is how your controller could look like

Code:
function index() {
    $data['news'] = $this->news_model->get_news();
    $data['products'] = $this->products_model->get_products();
    $this->load->view('header');
    $this->load->view('your_view', $data);
    $this->load->view('footer');
}
#7

[eluser]nmweb[/eluser]
[quote author="minimal design" date="1212609581"]well, in his example I'm assuming news and products are 2 tables in the database, 2 different sets of data, so I think 2 models makes more sense. Most likely it will be used in many different places and keeping the models separated seems to be a more modular architecture that will help minimize code redundancy. If I'm wrong, please expand on how you'd do it... I'm always curious to learn new things Smile[/quote]

I'm a fan of wireddesignz HMVC meaning that would call in the controller the News_Controller for the news, and the Product_Controller for the product. The called controller would then load the model.
#8

[eluser]minimal design[/eluser]
[quote author="nmweb" date="1212610613"][quote author="minimal design" date="1212609581"]well, in his example I'm assuming news and products are 2 tables in the database, 2 different sets of data, so I think 2 models makes more sense. Most likely it will be used in many different places and keeping the models separated seems to be a more modular architecture that will help minimize code redundancy. If I'm wrong, please expand on how you'd do it... I'm always curious to learn new things Smile[/quote]

I'm a fan of wireddesignz HMVC meaning that would call in the controller the News_Controller for the news, and the Product_Controller for the product. The called controller would then load the model.[/quote]

I don't really get it... got an example online somewhere? Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB