Results 1 to 8 of 8

Thread: referencing code in a different file?

  1. #1
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default referencing code in a different file?

    Ok, so I am using this menu:

    http://www.dynamicdrive.com/style/cs...el_css_menu_2/

    It's great! However, I need to use it on 50 pages. I don't want to have to edit 50 pages every time I need to make a change to the menu. How can I avoid that and create one file with all the info and reference it in the 50 pages and just change the one? Hope I worded this correctly. Any questions or help, please let me know!

    Jenn

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Can you use PHP on your server? It's very easy and will be seamless. There may be ways around it with Javascript, but using PHP is more reliable and probably easier, if you can use it. Other similar options include SSI, ASP, etc, if you have those instead.

    If you do have PHP, all you need to do is:
    1. Convert all of your pages to .php [instead of .html]-- this will make your server process them as PHP code [which generates HTML-- a .html file renamed .php will be identical, it just allows PHP also].*
    2. Create a new file called anything you like, let's say menu.php.
    3. In each page you want the menu, just use this PHP code:
    <?php include('menu.php'); ?>
    (Note that you may need to adjust the path to be relative to the current page.)


    *I know this sounds like a lot of work, but it'll help in the end and PHP can do more automation processes, like moving more than just the menu to a central file. That way you can edit/maintain many pages at once.
    If you really want to avoid renaming your pages to .php you can configure your server (through .htaccess probably) to parse .html/.htm files as PHP as well, but this is bad for two reasons: 1) it may be a security risk: it's not dangerous to let people upload html files to your server, but if they parse as PHP it would be-- so if you have any uploads of this sort, it's not a good idea. That's probably not the case, though. And 2) this just means your server will overall work a little harder, parsing every page as PHP even if it is just a regular html page. But if all of your pages are going to have the menu anyway, it won't be a problem. If you want more advice on this issue, explain the general plans for your site.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok so, do I have to rename ALL pages on the site to php or just the ones using this particular menu?

  4. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    You'll either need to change the pages to php or make the server read the extension they have as if they are php.

    http://php.about.com/od/advancedphp/p/html_php.htm
    Corrections to my coding/thoughts welcome.

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You don't need to rename every single file on the site, just those that you are going to use PHP code in. You can have both htm and php files, but php will only work in the .php files. However, I'd suggest renaming ALL of them, so that you keep things consistent and can easily add the menu (or another menu, etc) to other pages later if you want, rather than splitting things up in a hard-to-remember way. But, sure, it's fine if you just have some php pages with this menu and other unrelated .htm files.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Well, the pages I need to use the .php are in a password protected folder and will only be used on those pages, so it actually won't be confusing. However, I am using another menu for other pages on the site that I just might change over eventually anyway down the road since it will make life soooo much easier! Thanks so much!

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Ok, glad that works. Before you rename all of the files, be sure that you do have PHP installed. I mentioned that above, but you didn't confirm it is on your server. If you go through all of that effort then find you can't use PHP, it will be frustrating. Most hosts do have it, though, so it shouldn't be a problem.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Oh yes sorry, we do have that on our servers. It's worked out very well!!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •