1

I am working on a project. I'm almost close to finished. I am using CodeIgniter as a framework and I have over 360 main files in my views folder. However, I only have included <?php include "includes/footer.php" ?> in about 3 functions in my controller. I have over 360 functions and 200 controllers.

What's the fastest way to include a footer file in every one of the pages?

halfer
  • 19,824
  • 17
  • 99
  • 186
Taylor
  • 2,981
  • 2
  • 29
  • 70
  • Well you're out of luck. You can put the footer in the `header.php` file (assuming you have it included in all of your controllers) and then customize it so that the footer part always sticks to the bottom of the page. This won't work perfectly but maybe give it a shot? – user2453646 Mar 28 '15 at 22:59
  • This might be of some help: https://stackoverflow.com/questions/5586293/shell-script-to-append-text-to-each-file – Mathew Tinsley Mar 28 '15 at 23:11
  • Do you have the opening tag only 1 time in each file? – Rizier123 Mar 28 '15 at 23:48
  • 1
    Chances are you have done something **terribly** wrong. PHP isn't supposed to work that way. If you have 360 URLs that link to, for example, product pages, you should have **one** PHP file that serves all of these requests. And 200 controllers is **insane** and completely unmaintainable. – user229044 Mar 29 '15 at 00:06

4 Answers4

3

I'd use something such as Notepad++. With regular expression matching, you can easily add that to the bottom of files:

Here are the settings I used to accomplish this.

  • But I'm using a Mac. :( – Taylor Mar 28 '15 at 23:27
  • 1
    Any software that can handle regular expression editing with multiple files should be able to do what I've done. [Sublime Text](http://www.sublimetext.com/) should also have this capability. –  Mar 28 '15 at 23:32
1

The fastest (and most easily configurable) way to insert '' into alot of files (on linux command line) is sed:

find views/ -name '*.php' -exec sed -i.bak '$ i\<?php include "includes/footer.php" ?>' {} +

Explanation:

  1. -i.bak means "in-place" edit and create backup copy with '.bak' suffix. Any string append to -i causes backup file creation, and the string is appended to the backup filename. A bare -i causes only "in-place" editing and no backup file creation. Remove the -i disables in-place editing and causes output to std out.
  2. $ means "match last line". You could replace $ with /PATTERN/, where PATTERN is a regex.
  3. i\ means insert

The effect of this command is the literal string after the backslash, up to the last single quote, will be inserted before the last line of each file found by find.

Shane Voisard
  • 1,145
  • 8
  • 12
0

It's a bit of a problem but you can maybe search and replace multiple files... e.g. if you are loading views and have your footer after the body then replace your previous view i.e. $this->load->view('home_view', $data); with the same string but then add your footer as well?

Notepad++ as suggested would be a good idea or:

Find and replace in multiple files

Community
  • 1
  • 1
cnorthfield
  • 3,384
  • 15
  • 22
0

You could easily write a simple PHP script that: opens each file in the directory, appends the needed code to that file, and closes the file. You would then rinse and repeat for every file.

EDIT: Needed to provide more details for answer.

Some pseudocode below. Note its pythonic in style (since thats what I am familiar with but the general gist is the same).

def appendCode(file):
    code = "<?php require(\"footer.php\"); ?>"
    with open(file, 'a') as f:
        #append code here to end of file.
        f.append(f)

def readDir(dir):
    if isDir(dir):
        d = openDir(dir)

    return d.allFiles()

def main():
    dirs = ["dir1", "dir2"]

    for dir in dirs:
        files = readDir(dir)

        for f in files:
            appendCode(f)

Alternatively, you could use a one liner command prompt that have been posted here or linked to! I personally enjoy writing simple programs like this just for the fun of it.