ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Help in include files

November 14, 2008 12:22pm

Subscribe [4]
  • #1 / Nov 14, 2008 12:22pm

    khuram

    12 posts

    Hi,

    I am a totally noob in CI. Please bear with me. I have a petty set of problems which I am finding answers for if I am to stick with CI.

    Every page has many parts. Header, Footer, Images, CSS, Config Variables (my own not CIs). My questions are

    1. If a page has Header and Footer, do I make model-view-controller set for each of them.

    2. If yes then this is definitely time consuming.

    3. If no, where(location inside the application folder) to make my Include folder where I can put the header, footer files.

    4. Can I use the Database class in these include (header,footer, category list) php files directly without going through MVC everytime.

    5. How to include these files in my main MVC page.

    6. Since these file will be needed on more than one pages, how can I make them global with the option to include them at some places and not include them some places.

    7. Where to put my CSS, Images and .js files. Should I put them on the document root.

    8. The config variables I need like site title, contact us email etc. are going to be loaded from the database. How can I do that, I’m basically stuck with file inclusion and going through MVC for each step.

    9. Can anyone please provide me with a sample set of files just to get me acquainted with the heirarchy.

    I hope these questions make sense. I am using CI 1.7 on PHP4. My URL is like http://localhost/site/. I have managed to hide the index.php through htaccess though. 😊

    A timely reply would be greatly appreciated.

    I am at this forum for the first time but hopefully not the last. I will share my website with you people in a week’s time when it passes through first phase.

    Best Regards

    Khuram Javaid
    PHP Developer

  • #2 / Nov 14, 2008 3:10pm

    Rey Philip Regis

    92 posts

    Hi,

    Well if you have a header and footer in all your pages, you dont need to include or code them on every page of your site, that’s a bad practice. What I suggest you to do is make a layout (template) for your views. Like loading a view within a view.

    Sample: In the sample there is a css file inside the css folder. The css folder is inside the system folder.

    Controller:
    
    Sample.php
    class Sample extends Controller
    {
       function Sample()
       {
         parent::Controller();
    
         // need this to use base_url() in the template.php
         $this->load->helper('url');
       }
    
       function index()
       {
          $data['title'] = 'Sample Template';
          $data['content'] = 'main_page';
    
          $this->load->view('template', $data);
       }
    }
    
    
    template.php
    <html>
    <head>
      <title><?php echo $title; ?></title>
    
      <link type="text/css" rel="stylesheet" href="<?php echo base_url().'system/css/default.css'; ?>" />
    </head>
    
    <body>
    
       <div id="wrapper">
          <div id="header">
              $this->load->view('header');
          </div>
    
          <div id="content">
             $this->load->view($content);
          </div>
    
          <div id="footer">
             $this->load->view('footer');
          </div>
       </div>
    
    </body>
    
    </html>
    
    
    main_page.php
    <h1>This is the dynamic content (main page) of the page…..</h1>
    
    header.php
    <h1>Welcome to the website</h1>
    
    footer.php
    <h1>Copyright 2008</h1>

    This is a simple example of what youre trying to do….

    Hope this sample helps you…..

  • #3 / Nov 14, 2008 3:18pm

    Rey Philip Regis

    92 posts

    This is how I arranged my folders in CI

    website
      - system
        - application
          - config
          - controllers
          - models
          - views
          - ....
        - cache
        - codeigniter
        - css(Manually added this for css files)
        - database
        - fonts
        - javascripts (Manually added this for javascripts files)
        - .....

    Actually its the deafult file arrangement/hierarchy just added css folder and javascript folders inside the system folder.

    Good day.

  • #4 / Nov 14, 2008 3:25pm

    khuram

    12 posts

    Hi Rey

    Thanks very much for the timely reply buddy. Some questions have sprung to my mind.

    1. where does template.php reside.
    2. Where do header,footer etc. files reside.
    3. Can i use SQL queries in header.php etc. and how?

    I hope you’ll be kind as before.

  • #5 / Nov 14, 2008 3:26pm

    mradlmaier

    63 posts

    Hi Khuram,

    Although the User Guide is a bit short at some points you really should go through introductionary chapters. This will explains some of your questions.
    I am am also a newbie to CI, but I have worked with similar frameworks before…

    1. If a page has Header and Footer, do I make model-view-controller set for each of them.

    The footer, header, etc. stuff has nothing to do with the concept of model-view-controller. Footer, header, etc. go into the views.

    For Beginners, you should start by using only views and controllers. The controllers basically contain the logic of your application, while views ideally only displays the results. So your controller can ‘include’ different views.

    4. Can I use the Database class in these include (header,footer, category list) php files directly without going through MVC everytime.

    I think so, but database stuff should go in the controller and from there is passed tho the view. If you use a model, it actually represents your data in the application an see this thread:

    5. How to include these files in my main MVC page.

    There is no main MVC page. This is separated into Views, Controllers and, if you need it, Models. The reason to do this is that if you mixed them all in one page and later want to change a view or add a view, it is difficult todo. For example there could be a tree-like view and a table-like view of the data (the rows in a database table) and a view which displays a single row of your db table. All these three views display the same data (~Model) while, they allow for diffent user interactions on these views. Your controller handles these interactions (i.e. the click on a edit-button) and makes the changes to your data, either through your Model or directly (in a simple application you might choose to manipulate the dat directly) 

    6. Since these file will be needed on more than one pages, how can I make them global with the option to include them at some places and not include them some places.

    A view can be called from different methods in a controller or even different controllers. Different data can be passed to the view. So views are global in a sense. I guess you should read some general stuff about the Model-View-Controller Design Pattern to clearly understand that. MVC is not specific to CI or PHP, all good frameworks and object-oriented languages use it.

    7. Where to put my CSS, Images and .js files. Should I put them on the document root.

    Put them in there own folders and allow access to these folders in .htaccess. There is an article in the wiki about it and a thread at: http://ellislab.com/forums/viewthread/96424/

    8. The config variables I need like site title, contact us email etc. are going to be loaded from the database. How can I do that, I’m basically stuck with file inclusion and going through MVC for each step.

    They go in config files and can be loaded from there. Look at the User Guide for how to do that. Database would be bad for performance. DB is for data that changes, but not for configuration, which hardly ever changes.
    You really need to look at the user guide, most of your questions are answered there, and you should read some general stuff about MVC. It`s worth it, once you get yourself aquainted it will save you a lot
    of time…

    Michael

  • #6 / Nov 14, 2008 3:29pm

    eger

    19 posts

    I use something similar to Rey. To pass stuff to the page you can also add a $data key such as

    $data['data'] = $yourdata;
    and then in the template load the templated page with
    $this->load->view($content, $data);

    to get all your data set in the $yourdata variable to the view that will be loaded into the template. I make small apps and this simple approach seems to work just fine for me.

  • #7 / Nov 14, 2008 3:33pm

    Rey Philip Regis

    92 posts

    Hi,

      To answer your questions you should put your template.php, header.php, footer.php inside the view folder like your other view files. But I suggest for better management of your files you should create a folders inside you view files for every template/layout you have.

    Sample:

    - view
      - template_folder1
        - template.php
        - header.php
        - footer.php
      - sample /* this is the folder for your sample controller views */
        - index.php
        - main_page.php
        - about.php
        - contact.php

    This is to manage your files easily and example in the future you have a new layout you just create a new template_folder2 and put your new template.php, header.php and footer.php there and incase you want to go back to the original template just use the template_folder1.

  • #8 / Nov 14, 2008 3:36pm

    khuram

    12 posts

    Thanks Mike, appreciated.
    I did read the manual uptil first four topics of class reference. So its not like I started crying without trying to learn. Its my own choice as to use CI. I am sticking with it and people like you are making it easy for me by replying kindly.
    I’ll get back to my studies again.

    Best regards
    Do visit this thread again if you see it in bold. 😊

  • #9 / Nov 14, 2008 3:37pm

    Rey Philip Regis

    92 posts

    To answer your last question, yes you can do SQL queries in the header and footer file. Just load them using $this->load->model(‘mymodel’). This is valid if youre using a model.

  • #10 / Nov 14, 2008 4:07pm

    khuram

    12 posts

    Hey its starting to make sense.
    Wow, this opens up a whole new world for me.

    Much regards

  • #11 / Nov 14, 2008 4:10pm

    Rey Philip Regis

    92 posts

    Now you realize how easy to make webpages using CI. Its just creativity and understanding the thoery on how to do a certain thing.

    Its been a pleasure helping you….

    Good day and happy code igniting haha..

  • #12 / Nov 17, 2008 6:52am

    khuram

    12 posts

    There is only one thing that i havent been able to understand.
    Lets say on my template page, I have to load a list of categories in a table.

    Ideally there should be a controller for template which can be called from anyother control to initialize the stuff.
    I havent been able to find this out anywhere.
    Right now, I am having to do this in every new page that I make.

    $this->load->model('template/template','TemplateFunctions', TRUE);
    $data['cats'] = $this->TemplateFunctions->get_categories();
    $this->load->view('template/template', $data);

    Ideally this is not the best situation. Please advice me into how to make a master control which I include in every control and it knows how to react.

  • #13 / Nov 17, 2008 12:00pm

    Rey Philip Regis

    92 posts

    $this->load->model('template/template','TemplateFunctions', TRUE);
    $data['cats'] = $this->TemplateFunctions->get_categories();
    $this->load->view('template/template', $data);

    Is the code above dynamic? I mean does $this->TemplateFunctions->get_categories(); change in every page? If its the same in every page, I have a suggestion but havent tried it yet. Try to use hooks put your code i mean the above code to a method then register it in a hook its configurations is inside the config folder, then after executing the above code in your newly created method put it in a session so it will be available in all of your page.

    Personally, just like what I said a while ago, I haven’t tried that technique yet. Just try that, if you like….

    Good day.

  • #14 / Nov 17, 2008 1:22pm

    khuram

    12 posts

    Hi Rey

    Yes this list is populated from database but it is a list which wont change on pages except the category detail pages themselves where it includes the subcategories for that selected category. This templating system is not becoming any easier. I must be dumb. :(

  • #15 / Nov 17, 2008 1:26pm

    khuram

    12 posts

    I am having a hard enough time learning the basics of CI, if I go after hooks with my half cooked knowledge, I will probably get lost pretty soon.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases