Welcome Guest, Not a member yet? Register   Sign In
Help in include files
#1

[eluser]khuram[/eluser]
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. Smile

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

[eluser]Rey Philip Regis[/eluser]
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.

Code:
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>

&lt;/body&gt;

&lt;/html&gt;


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

[eluser]Rey Philip Regis[/eluser]
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

[eluser]khuram[/eluser]
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

[eluser]mradlmaier[/eluser]
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...

Quote: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.

Quote: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:

Quote: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)

Quote: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.

Quote: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/

Quote: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

[eluser]eger[/eluser]
I use something similar to Rey. To pass stuff to the page you can also add a $data key such as
Code:
$data['data'] = $yourdata;
and then in the template load the templated page with
Code:
$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

[eluser]Rey Philip Regis[/eluser]
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

[eluser]khuram[/eluser]
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. Smile
#9

[eluser]Rey Philip Regis[/eluser]
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

[eluser]khuram[/eluser]
Hey its starting to make sense.
Wow, this opens up a whole new world for me.

Much regards




Theme © iAndrew 2016 - Forum software by © MyBB