SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 32
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to creater a central header page for Logo and Menu

    Hi

    Is there a good way to incorporate logos/menus in one central file which can be called into the different webpages (php pages). This means I only need to change the content of the menu and logo in one place as opposed to every single page?

    If i use the statement include_once(file.php) the problem here is file has to be without any HTML tags like Header section etc.

    Regards

  2. #2
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Yes you want to use include_once().

    You can easily include html in a php file:

    Code HTML4Strict:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <?php 
    //lots of clever php code here;
    //no we stop writing php
    ?>
    <head>
    <title></title>

    Php is an embedded language...meaning that you embed it directly in html. There are better ways such as templating engines (such as smarty)which abstrct logic from html.

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You could setup some constants...

    Constants.php
    PHP Code:
    <?php
    define
    ('LOGO_IMAGE','LogoOne.jpg');
    define('LOGO_TAG','Some random tag line');
    ?>
    Sample.php
    PHP Code:
    <?php include('Constants.php'); ?>
    <html>
        <head>
            <title>Testing</title>
        </head>
        <body>
            <img src="<?php print(LOGO_IMAGE);?>" />
            <p><?php print(LOGO_TAG);?></p>
        </body>
    </html>
    Something like that, maybe?

    SilverB.

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Silverbullet

    The problem with Constant.php is I want to include menu and formatting from a central css file and Javascript for menus!

    Hence I need to use the HTML tags of:

    <html><head> reference to css and Javascript code for menu </head></html>



    Once I do that it does not work

  5. #5
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Eh?

    You can put html in php files.....

    Code HTML4Strict:
    <html><head> reference to css and Javascript code for menu </head></html>
    <?php //php code here
     
    ?>
    <p>more html here</p>

    You can then save this code as a php file and call it using include_once()

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by elduderino View Post
    Eh?

    You can put html in php files.....

    Code HTML4Strict:
    <html><head> reference to css and Javascript code for menu </head></html>
    <?php //php code here
     
    ?>
    <p>more html here</p>

    You can then save this code as a php file and call it using include_once()
    Yes when I do that it causes problems. The code for the main file does not show well

  7. #7
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well that's the correct way to do itso your problem lies somewhere else....what do you mean it cause problems?

  8. #8
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Elduderino

    Sorrry it does work that well perfectly. In fact it worked from the very beginning with my code. But the view in Dreamweaver design mode is a mess implying that the code is not right. Or in any case the problem is the environment is a problem as it looks a mess in Dreamweaver, but when the files are uploaded it looks perfect.

    I am gone ask the adobe people now as to why

    Thanks

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your problem is invalid html.

    You should be using php to combine fragments of an html page to form a single, complete html page.

    What you're trying to do is combine multiple complete html pages, which results in extremly invalid html.

  10. #10
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No your problem is that you're trying to run php code in dreamweaver. Php must be parsed on a server running php. You can't view a php file locally and expect it to render properly.

    What you're trying to do is combine multiple complete html pages, which results in extremly invalid html.
    No i don't think that is what he's trying to do...he's trying to extract the common header code (with links to css/javascript) in to a seperate document so one edit can effect multiple pages. It's a common method used to save development time and keep pages uncluttered.

    (it can also be achieved using html pages combined with some apache configuration and server side includes....if you don't know anytihg about programming that is!)

  11. #11
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Just put it all in a separate file, and include() it. include_once() will mean you can't use it later on in the same page (in case you wanted to) - best to just use include() for HTML includes.

  12. #12
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HI,

    I disagree. You should use include_once() when you only want to include the file once....this will assure that the code is added to your script only once and avoid clashes with variable values or function names that can happen. It sounds like the poster wants to include a common header/nav in every page. So include_once() is appropriate.

    So for including documents like headers you should use include_once(). It is ,programmatically speaking, good practice.

  13. #13
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    For php, I agree. But then when classes etc are involved, I have a 'central' file that includes all the others and sets up __autoload(). I require() that file at the top of each page - there is no way I can accidentally include the same file twice. For html includes (header, nav, footer), I use include().

    The only way I think the _once() functions are useful is if you include all the files you need at the top of each page, and some of those include each other, but that is just a big mess and not what I do.

    If you need to include, eg, the navigation twice on the same page (for example on a sitemap page), you are stuck if you have used include_once().

  14. #14
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Thank you all for your input. Eldudirno, your description is correct in terms of what I am trying to do. Yes I know about server side includes (SSI) but I think most people tell me that is out of date and as you say requires further configuration of the server. Means more headache!

    Now as for include_once I agree with you on that too.

    The code seems to work fine but on dreamweaver it looks terrible - in design mode it looks all wrong. In another case my statements like <h1></h1> or <p> </p> does not show after the incude statement in PHP. I dont think it has anything to do with rendering HTML document as the files are PHP files.

    In another case the phpinclude simply appears as a php tag and the content is not displayed.

    I have raised a cased with Adobe to examine this issue.


    It makes my work kind of annoying as the output looks wrong.

    Regards

  15. #15
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by explorer07 View Post
    Hi
    The code seems to work fine but on dreamweaver it looks terrible - in design mode it looks all wrong. In another case my statements like <h1></h1> or <p> </p> does not show after the incude statement in PHP. I dont think it has anything to do with rendering HTML document as the files are PHP files.

    In another case the phpinclude simply appears as a php tag and the content is not displayed.
    As i've said the reason it renders ok in a browser and not in dreamweaver is because php files must be parsed by a php parser. I take it you're running your files on a server either local or remote? The reason they don't output
    correctly in dreamweaver is because dreamweaver does not have a php parser.

    Stormrider, thanks for the __autoload() tip. I was not familiar with this until you mentioned it. Thanks

  16. #16
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use include_once() because require doesn't work on all platforms.
    Have a good day.

  17. #17
    SitePoint Guru SSJ's Avatar
    Join Date
    Jan 2007
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Darren884 View Post
    Use include_once() because require doesn't work on all platforms.
    Yes you are correct, I will also prefer to use include_once()

  18. #18
    I Never Give Up roosevelt's Avatar
    Join Date
    May 2005
    Posts
    515
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If you have only few (e.g. 20 or so) PHP pages and they contain the same design or structure then you can do this classic trick.

    Here's my template:

    Code:
    <html>
    <head>
    <title><?php echo $title; ?></title>
    </head>
    <body>
    <!-- Your Super HTML Codes -->
    
    <?php require($_GET['pagename'].".html"); ?>
    
    <!-- Footer Area HTML -->
    </body>
    </html>
    Basically, all you have to do is save the above file as .php. And store just the contents of a web page in a .html file. Then access the PHP file and give it a parameter.

    So if I saved the php file as

    /index.php

    and my about us page is in:

    /pages/aboutus.html

    Then I would access /index.php?pagename=pages/aboutus

    You can also use mod_rewrite to make the web pages more friendly.

  19. #19
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    roosevelt, thats also the number 1 way php powered websites get hacked.

    You need to validate user input before using it.

  20. #20
    SitePoint Guru
    Join Date
    Sep 2004
    Posts
    613
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The most effective way to do this is to use Smarty's template managing system. yes it sounds complicated, but it couldn't get any easier and more efficient. The best route to take with designing web sites is to seperate all of your PHP / HTML using a template engine. The reason is because this way you're either programming PHP or html, you never do it in the same file. Thus if you have a programmer who works for you who does PHP, and an HTML/CSS guy (who get paid on a different scale), they never touch the same file and your layout guy will never accidentally mess up any php.

    http://en.wikipedia.org/wiki/Smarty

    Official Site: http://www.smarty.net

    The sample on wikipedia is great. The second box is the setup code, which should be included in your index.php and if you're setting up your other files accordingly you won't really need to put it anywhere else. Or just put it in an include that goes on every page.

    The engine is powerful, at first I didn't want to use it, but my partner insisted. Now I wouldn't do it any other way, it's so awesome and it really helps you write PHP. For instance you can setup 1 template file to do all of your pagination which could be used on 6 completely different pages.


    For instance, we created a class to do it all for us.
    PHP Code:
    <?php
    class TruDisplay {
        function 
    __construct ($tru) {
            
    $this->tru $tru;
            
            
    define("SMARTY_DIR"$this->tru->config->get("root.path").$this->tru->config->get("root.smarty"));

            require_once(
    SMARTY_DIR."Smarty.class.php");
            
            
    $this->smarty = new Smarty();
            
            
    $this->smarty->template_dir $this->tru->config->get("root.path").$this->tru->config->get("root.template")."/";
            
    $this->smarty->compile_dir $this->tru->config->get("root.path").$this->tru->config->get("root.template")."/_compiled/";
            
    $this->smarty->config_dir $this->tru->config->get("root.path").$this->tru->config->get("root.template")."/_config/";
            
    $this->smarty->cache_dir $this->tru->config->get("root.path").$this->tru->config->get("root.template")."/_cache/";
        }
        
    /**
         * @method addToken
         * Adds a token with the given name and value to the smarty engine.
         * @param {String} $name The name of the token
         * @param {Mixed} $value The value of the token
         */
        
    function addToken ($name$value) {
            if (
    is_object($value)) {
                
    $this->smarty->assign_by_ref($name$value);
            } else {
                
    $this->smarty->assign($name$value);
            }
        }
        
    /**
         * @method getTemplate
         * Gets the content of the given template after it is parsed.
         * @param {String} $template The path to the template from the root template folder
         * @return {String} The parsed template's content
         */
        
    function getTemplate ($template) {
            return 
    $this->smarty->fetch($template);
        }
        
    /**
         * @method loadForm
         * Adds tokens for each member of the given form.
         * @param {Tru.Stream.Form} $form The form to load
         */
        
    function loadForm ($form) {
            if (!
    $this->tru->isInstance($form"tru.stream.form")) {
                
    $this->tru->error->add(array(
                    
    "type" => "Tru.Display::loadForm fail",
                    
    "message" => "Supplied form is not an instance of Tru.Stream.Form"
                
    ));
            } else {
                
    $stream $form->getRawStream();
                
                foreach (
    $stream as $key => $val) {
                    
    $this->addToken($key$val);
                }
            }
        }
        
    /**
         * @method showTemplate
         * Outputs the given template after it has been parsed.
         * @param {String} $template The path to the template from the root template folder
         */
        
    function showTemplate ($template) {
            
    $this->smarty->display($template);
        }
    }
    ?>
    Last edited by Webnet; Aug 7, 2008 at 21:56. Reason: added the display class

  21. #21
    I Never Give Up roosevelt's Avatar
    Join Date
    May 2005
    Posts
    515
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    roosevelt, thats also the number 1 way php powered websites get hacked.

    You need to validate user input before using it.
    Actually all php websites get hacked, it's only how strong your security measure is . My way is just an example, you surely don't expect me to paste the whole program .

    Some of the things you can do is for example pass in the file names as capital letters but change them to lowercase in the file, make sure the file exist, and do not let it load any file other then the designated folder .

    If you are familiar with MVC then that is the best way to go. It will save you a lot of time and effort. Also, the use of smarty template engine works great as well.

  22. #22
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Webnet View Post
    The most effective way to do this is to use Smarty's template managing system.
    My personal favourite

  23. #23
    Trash Boat mkoenig's Avatar
    Join Date
    Aug 2007
    Posts
    1,232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah... server side includes

    PHP Code:
    <?
    include 'menu.inc';
    ?>
    I always end my includes with an .inc then deny access to it from the .htaccess so users cant browse directly to that file.


  24. #24
    PHP/Rails Developer Czaries's Avatar
    Join Date
    May 2004
    Location
    Central USA
    Posts
    806
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For most sites I make, I use a framework that I have. For sites that are just so dead simple that it doesn't make sense to use any framework (i.e. no database or interactive features), my pages look something like this:

    PHP Code:
    <?php
    // Defaults for all optional layout settings are defined in config
    include('./includes/config.inc.php');

    // Layout options can be set on a page-by-page basis
    $pageTitle "About us";
    $metaKeywords "";
    $metaDescription "";
    $showSidebar true;
    $showMenu true;
    $someOtherOption false;
    $includeSomeJavascript false;

    // Header include contains conditional statements
    // and other includes based on set options above
    include(DIR_INC 'header.inc.php');
    ?>

          <h1><?php echo $pageTitle?></h1>
          <div class="indent">
            <h2 style="padding:0;">A Web Design Company</h2>
            <p>This is some really super-duper cool company.  It's awesome.</p>
          </div>
          <br />
          <div class="center">
            <p>Some more text about the company!</p>
          </div>
          <br />

    <?php
    // Footer can also contain conditional statements based on options above
    include(DIR_INC 'footer.inc.php');
    ?>
    So instead of the more traditional 2 file header/footer include, it ends up being a 3-file config/header/footer include setup that allows much more flexibility for layout control and still keeps most of the specific HTML out of your individual pages.

  25. #25
    I &lt;3 Internet Tekime's Avatar
    Join Date
    Dec 2003
    Location
    Maine
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is no Best way, but some great suggestions above.

    For simplicity just make a config.php with your constants, then header.php and footer.php for sitewide code.

    Personally I don't use Smarty (wrote my own class), and I don't recommend it very often. I think beginners should try to learn the basics themselves before inviting a huge system like Smarty onboard. It's also nice knowing that your scripts don't require Smarty to be installed.
    Scriptalicious SEO Scripts
    Save 20% with coupon code SPROCKS



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
  •