Question about php (conditions)

Discussion in 'Web Design and Development' started by olup, May 30, 2013.

  1. olup macrumors 6502

    Joined:
    Oct 11, 2011
    #1
    I'm not entirely sure, how to word this correctly as I'm completely new to php, but I have the following questions regarding php.

    I'm working on a site, where I created php parts that are pulled in with include/doc-header.part, for instance. This would be the doc-header.part:

    Code:
    <!DOCTYPE html>
    
    <html lang="de">
    <!--[if lt IE 7]>      <html class="ie6"> <![endif]-->
    <!--[if IE 7]>         <html class="ie7"> <![endif]-->
    <!--[if IE 8]>         <html class="ie8"> <![endif]-->
    <!--[if gt IE 8]><!--><!--<![endif]-->
    
    <head>
        <meta charset="UTF-8">
        <link href="favicon.ico" rel="shortcut icon">
        <link href="css/style.css" media="all" rel="stylesheet">
        <link href=
        'http://fonts.googleapis.com/css?family=Titillium+Web:400,200|Roboto+Condensed:400,700'
        rel='stylesheet' type='text/css'>
        <title><?php if(isset($title)) { print $title; } else { print "solaris empire"; } ?></title>
         <meta http-equiv="keywords" content="<?php if(isset($keywords)) { print $keywords; } else { print "default,key,words"; } ?>" />
         <meta http-equiv="description" content="<?php if(isset($description)) { print $description; } else { print "Default Description!"; } ?>" />
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
       
    </head>
          <body>

    The two questions I have regarding the header are:

    a) How can I make the title of each page dynamic, so it shows the title of the respective page, like solaris empire | releases and so on?

    b) I have a subfolder with some php pages. Before they were html pages and it was no problem linking the css file to the pages in the subfolder.

    Code:
     <link href="../css/style.css" media="all" rel="stylesheet">
    How would I need to change the doc-header.part to fetch the css file?

    Also on those pages I have two JQuery scripts that only load on those pages.
    I wanted to avoid to have those scripts load on every page of the site, so I didn't include it in the doc-footer.part, which looks like this.

    My question regarding the footer is, how can I conditionally load those scripts only on those pages that require it?

    Code:
            <footer class="footer">
                <ul>
                  <li>
                    <a href="index.php">home</a>
                  </li>
                  <li>
                      <a href="artists.php">artists</a>
                  </li>
    
                  <li>
                      <a href="releases.php">releases</a>
                  </li>
    
                  <li>
                      <a href="about.php">about us</a>
                  </li>
    
                  <li>
                      <a href="promotion.php">promotion</a>
                  </li>
    
                  <li>
                      <a href="links.php">links</a>
                  </li>
    
                  <li>
                      <a href="projects.php">more projects</a>
                  </li>
    
                  <li>
                      <a href="contact.php">contact</a>
                  </li>
                  
                    
                </ul>
            </footer>
        </div>
    </body>
    </html>
    Thanks in advance and apologies for not expressing myself clearly enough.
     
  2. aarond12 macrumors 65816

    aarond12

    Joined:
    May 20, 2002
    Location:
    Dallas, TX USA
    #2
    PHP is a wonderful language. The entire page can be dynamically written using PHP code exclusively. For example, if you wanted the title of the page to be the file name of the page, your code might be:

    Code:
    <html>
    <head>
    <title>My page name is <?php print $_SERVER["PHP_SELF"] ?></title>
    </head>
    ...
    </html>
    
    Just think of it this way: You are having the PHP script write the specific parts of the HTML code that you would normally hard-code.
     
  3. jsm4182, May 30, 2013
    Last edited: May 30, 2013

    jsm4182 macrumors 6502

    Joined:
    Apr 3, 2006
    Location:
    Beacon, NY
    #3
    To have dynamic page title, you can put something similar to what aarond12 demonstrated but print a variable such as

    PHP:
    <html>
    <head>
    <title><?php print $page_title?></title>
    </head>
    Then before you include doc-header.part give a value to $page_title

    PHP:
    <?php

    $page_title 
    "The title of the page";
    include 
    "include/doc-header.part";

    ?>
    For your css path issue, you could just use an absolute path or path relative to the site root

    Code:
    <link href="/css/style.css" media="all" rel="stylesheet">
    or

    Code:
    <link href="http://yourdomain.com/css/style.css" media="all" rel="stylesheet">
    For conditionally adding scripts the solution could be similar to the page title solution. Print a variable in the footer include, on the php page before the footer include define that variable as the script code.
     
  4. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #4
    One very important thing to remember - it is uncommon for most web servers to parse embedded PHP if the document filename does not have a .php extension on the end. So index.html becomes index.php for example.

    Note: This is not important to do in files that are referenced by PHP's include() or required(), i.e. you can use .inc as the extension, if desired if you like to follow a naming convention.

    This is also a good time to remind folks of the powerful PHP "HEREDOC" syntax which makes templates so much easier to write:

    Instead of this:

    PHP:
    <div id="foobar">
    Hello, world.  My name is <?php print $myname?> and today is <?php print $date?> and you're in section <?php print $section['name']; ?>.
    </div>

    The same thing using HEREDOC syntax:

    PHP:
    print<<<TOBROWSER
    <div id="foobar">
    Hello, world.  My name is 
    $myname and today is $date and you're in section {$section['name']}.
    </div>
    TOBROWSER;
    I picked an example that includes HTML, PHP print and variables including an array with a keyname defining an element (wrap with { and } to embed in a print statement), single and double quotes. HEREDOC allows you to put all that between the first and last line, noting I made up "TOBROWSER" -- that could be any label I want in upper case. The secret is the the left arrows after print, redirecting all output to a buffer named TOBROWSER which is displayed following the terminate signal, that last line.

    The only thing to remember is put the first and last line at column 1, no spaces or tabs before, to be compatible with any version of PHP. Anything between the first and last can be tabbed/spaced as you see fit.

    BTW, you can use HEREDOC to define a variable instead of print, i.e.

    PHP:
    $myvariable=<<<TOVARIABLE
    <div id="foobar">
    Hello, world.  My name is 
    $myname and today is $date and you're in section {$section['name']}.
    </div>
    TOVARIABLE;

    print 
    strtoupper(trim($myvariable));


    Full details here: http://www.tuxradar.com/practicalphp/2/6/3
     
  5. 960design, May 30, 2013
    Last edited: May 30, 2013

    960design macrumors 68020

    Joined:
    Apr 17, 2012
    Location:
    Destin, FL
    #5
    I'm with SrWebDeveloper. I keep all my code separate from the HTML sent to the screen using heredoc.

    For example, I declare and create all the variables at the top of the page and return with a heredoc loaded with the variables.

    something like:
    Code:
    /* Variable Assignments
    --------------------------------*/
    $pageTitle = _960design_getPageTitle();
    $javascript = _960design_getJavascript($pageTitle);
    /*------------------------------*/
    
    return <<<HTML
    
    <!doctype html>
    <html lang='en'>
    <head>
      <title>$pageTitle</title>
      $javascript
    </head>...
    HTML;
    
    
    I typically use a functions.php file to hold all the functions and call this class automatically, but you could include it explicitly as well with an include at the top of the page.

    It could be as simple at
    Code:
    /*LOAD bootstrap
    -------------------------------------------*/
    require_once dirname(__FILE__).'/_includes/functions.php';
    /*-----------------------------------------*/
    
    inside the functions.php file I would create the function that generates the page title.
    Code:
    function _960design_getPageTitle($title = NULL)
    {
      //you can put all sorts of logic in here to get the proper page.
      return (is_null($title)) ? 'default title' : '$title';
    }
    
    function _960design_getJavascript($pageTitle = NULL)
    {
      $javascript = '';
      switch($pageTitle)
      {
        case 'bob':
        case 'dave':
          $javascript = "<script type='text/javascript' scr='http://ajax.googleapis.com/libs/jqueryui/1.8.18/jquery-ui.min.js?ver=3.5.1'>"
          break;
        default:
      }
    }
    
    I didn't test any of the above ( the return is giving me the evil eye ), but I don't have time right now, sorry. Hope this helps
     
  6. 960design, May 30, 2013
    Last edited: May 30, 2013

    960design macrumors 68020

    Joined:
    Apr 17, 2012
    Location:
    Destin, FL
  7. olup thread starter macrumors 6502

    Joined:
    Oct 11, 2011
    #7
    thanks so much for the responses. Am I right in the assumption that heredoc is the best practice approach? In the functions.php I can call all those snippets of code that were called with include before at the top of every page?

    The similarities to javascript surprise me a bit.
     
  8. 960design, May 30, 2013
    Last edited: May 30, 2013

    960design macrumors 68020

    Joined:
    Apr 17, 2012
    Location:
    Destin, FL
    #8
    I cannot say that heredoc is 'best practice approach'; it's just the way I do it and the people that work with me. When I 'fix' older sites it take tons of time finding all the little <?php //whatever ?> snippets in a thousand line mega function.

    For me declaring all the variables at the top and functions in a separate file(s) helps keep me sane and productive. That way I can scroll to the bottom and just see basically HTML inside the heredoc. I can scroll to the top to see all the variables used. I separate my function files into several files. I use a core.php for functions that are used on 99% of sites. These functions get smarter, faster and better and I 'know' that they are debugged and safe to use. I use a functions.php file for custom functions just for that site; database.php for database.... well you get it.

    Over time, I've learned how to write code that is easier to expand and debug and for groups of people to work on at the same time. The best practice with just about all programming tasks is to do what works. You will NEVER figure out what the BEST way is, your skillz will continue to advance and you will look back at code that you wrote last year and think to yourself... NOOB!

    I've been writing code for almost 30 years ( since I was 9 ) and learn a new and better way to do something everyday. I love seeing how someone else solves a problem and think to myself, now that is pretty dang amazing.

    ----------

    The more languages you learn the more similarities you'll see. When you learn a core language like C/C++ you'll be able to pick up other languages very easily. Same constructs just different places to forget to put a semicolon. ;)
     
  9. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #9
    Naw, just an approach I like in templates for CMS design mostly. The examples I posted are for syntax, the code and technique 960design posted about are excellent suggestions and I concur. HEREDOC is just an informal suggestion to you as to how to more easily parse variables into HTML or content into variables and produce cleaner code that can be easily copy/pasted during development and easier to read when debugging. Learned through experience and gladly passing along to you and others browsing this topic for those interested.

    :cool:

    ----------

    BINGO.

    :p
     

Share This Page