SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP conditional in HTML header question

    i have a situation where i want to add an additional stylesheet in the header of a HTML/PHP page. i think i can add it using either of the following 2 ways...

    Code:
    <link rel="stylesheet" href="styles.css" type="text/css" />
    <?php if ($_SESSION['screen_size'] == 'big') {
    	echo '<link rel="stylesheet" href="more-styles.css" type="text/css" />';
    } ?>
    or...

    Code:
    <link rel="stylesheet" href="styles.css" type="text/css" />
    <?php if ($_SESSION['screen_size'] == 'big') { ?>
    <link rel="stylesheet" href="more-styles.css" type="text/css" />
    <?php } ?>
    can anyone tell me if both these ways are acceptable & if one of them is preferred?

  2. #2
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Both should work fine.

    I would go with the second approach as it handles quotes and double quotes and allows multiple lines.

    But it's really personal style.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cheers dude

  4. #4
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    @lancsDavid

    Not a PHP based solution but what you are doing with PHP can be achieved with C.S.S. Media Queries like:
    HTML Code:
    /* LAYOUT -------------- */
    #wrapper {    margin: 0 6%;}
    #header {    margin: 0 0 20px 0;
        overflow: hidden;    padding: 0 0;
        position: relative;
    }
    .ie6 #header {
        width: 100%;
    }
    #nav-main {
        float: right;
        margin: 40px 0 0 0;
    }
    /* MEDIA QUERIES ----------------------------------- */
    @media screen and (min-width: 1200px) { /*--- BIG SCREEN ---*/
       #nav-main {
           position: fixed;
          top: 260px;
           width: 13%;
          margin: 0;
            -moz-box-shadow: 0 0 8px hsla(0,0%,0%,.1);
            -webkit-box-shadow: 0 0 8px hsla(0,0%,0%,.1);
           box-shadow: 0 0 8px hsla(0,0%,0%,.1);
            -moz-border-radius: 3px;
            -webkit-border-radius: 3px; 
           border-radius: 3px;
            background: hsla(0,0%,100%,.3); 
           text-align: right; 
       }
       ... More CSS styles here ...
    }
    @media screen and (max-width: 760px) { /* -- Medium Size Say when a browser is not full screen on a desktop -- */
      em.header_img {
            float:left;
            display: block;
            background:  url(images/header_background_760px.jpg) no-repeat left top;
            width: 900px;
            height: 201px;
          }
      ... More CSS styles here ...
    }
    @media screen and (max-width: 550px) { /* -- Screen get smaller so lets get rid of some of the big asses and resize elements -- */
        em.header_img {
            display: none; 
       }
      ... More CSS styles here ...
    }
    @media screen and (max-width: 400px) { /* -- Smart Phone Sizing and Styles -- */
       #header {
          margin: 0; 
         overflow: hidden; 
          padding: 0 0;
          position: relative;
       }
      ... More CSS styles here ...
    }
    @media screen and (-webkit-min-device-pixel-ratio: 2) and (max-width: 480px) { /*-- Mobile Web-Kit Smart Phones -- */
      #header {
        margin: 0;
        overflow: hidden;
        padding: 0 0;
        position: relative;
      }
      ... More CSS styles here ...
    }
    For older browsers such as I.E. 6,7 and 8 there are pre-made javascript shims that can be used to give the same media query support to these browsers.

    This is another way for you to consider doing it that off-loads some of your server processing to the browser.

    Regards,
    Steve
    ictus==""

  5. #5
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would also choose the second method, but with this caviot: I'd use the following code:

    Code:
    <link rel="stylesheet" href="styles.css" type="text/css" />
    <?php if ($_SESSION['screen_size'] == 'big'): ?>
    <link rel="stylesheet" href="more-styles.css" type="text/css" />
    <?php endif ?>
    That alternative conditional syntax is preferable, at least for me, when embedded in HTML.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  6. #6
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,827
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    @lancsDavid ,

    I would go with the second method since it is easier to see what is happening, easier to debug and I cannot see the point of passing data to the browser if it is not required. Let your fast server do all the work and reduce your precious bandwidth.

    Bandwidth will also be reduced for SmartPhones since the latter "more-styles.css" will only be downloaded for the "screen_size=big".


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
  •