Results 1 to 5 of 5

Thread: Include PHP code in multiple pages

  1. #1
    Join Date
    Dec 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Include PHP code in multiple pages

    Hello All,
    I have a website I created for a radio station and they have a "Who's On Air Now" script which shows a different host's image depending on the time of day.

    I have all the code working just fine, but I have the code on 5 different pages in the <head> tag:

    Code:
    <?php
    $h = date('G'); //set variable $h to the hour of the day
    $d = date('w'); //set variable $d to the day of the week.
    $year = date('Y'); //set variable $year to the current year
    //G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.
    // Adjust 2 hour offset for MST below.
    $h = $h-2;
    
    // MONDAY SCHEDULE
    if ($d == 1 && $h >= 0 && $h < 4) $img = 'img/hosts/petem.jpg';
    else if ($d == 1 && $h >= 4 && $h < 8) $img = 'img/hosts/angelaa.jpg';
    else if ($d == 1 && $h >= 8 && $h < 12) $img = 'img/hosts/shonw.jpg';
    else if ($d == 1 && $h >= 12 && $h < 15) $img = 'img/hosts/mikef.jpg';
    else if ($d == 1 && $h >= 15 && $h < 19) $img = 'img/hosts/lizzy.jpg';
    else if ($d == 1 && $h >= 19) $img = 'img/hosts/danic.jpg';
    else if ($d == 2 && $h < 0) $img = 'img/hosts/danic.jpg';
    
    // TUESDAY SCHEDULE
    if ($d == 2 && $h >= 0 && $h < 4) $img = 'img/hosts/petem.jpg';
    else if ($d == 2 && $h >= 4 && $h < 8) $img = 'img/hosts/angelaa.jpg';
    else if ($d == 2 && $h >= 8 && $h < 12) $img = 'img/hosts/shonw.jpg';
    else if ($d == 2 && $h >= 12 && $h < 15) $img = 'img/hosts/mikef.jpg';
    else if ($d == 2 && $h >= 15 && $h < 17) $img = 'img/hosts/lizzy.jpg';
    else if ($d == 2 && $h >= 17 && $h < 20) $img = 'img/hosts/westmar.jpg';
    else if ($d == 2 && $h >= 20) $img = 'img/hosts/danic.jpg';
    else if ($d == 3 && $h < 0) $img = 'img/hosts/danic.jpg';
    
    // WEDNESDAY SCHEDULE
    if ($d == 3 && $h >= 0 && $h < 4) $img = 'img/hosts/petem.jpg';
    else if ($d == 3 && $h >= 4 && $h < 8) $img = 'img/hosts/angelaa.jpg';
    else if ($d == 3 && $h >= 8 && $h < 12) $img = 'img/hosts/shonw.jpg';
    else if ($d == 3 && $h >= 12 && $h < 15) $img = 'img/hosts/mikef.jpg';
    else if ($d == 3 && $h >= 15 && $h < 19) $img = 'img/hosts/lizzy.jpg';
    else if ($d == 3 && $h >= 19) $img = 'img/hosts/danic.jpg';
    else if ($d == 4 && $h < 0) $img = 'img/hosts/danic.jpg';
    
    // THURSDAY SCHEDULE
    if ($d == 4 && $h >= 0 && $h < 4) $img = 'img/hosts/petem.jpg';
    else if ($d == 4 && $h >= 4 && $h < 8) $img = 'img/hosts/angelaa.jpg';
    else if ($d == 4 && $h >= 8 && $h < 12) $img = 'img/hosts/shonw.jpg';
    else if ($d == 4 && $h >= 12 && $h < 15) $img = 'img/hosts/mikef.jpg';
    else if ($d == 4 && $h >= 15 && $h < 19) $img = 'img/hosts/lizzy.jpg';
    else if ($d == 4 && $h >= 19) $img = 'img/hosts/danic.jpg';
    else if ($d == 4 && $h >= 22) $img = 'img/hosts/canu.jpg';
    else if ($d == 5 && $h < 0) $img = 'img/hosts/canu.jpg';
    
    // FRIDAY SCHEDULE
    if ($d == 5 && $h >= 0 && $h < 4) $img = 'img/hosts/petem.jpg';
    else if ($d == 5 && $h >= 4 && $h < 8) $img = 'img/hosts/angelaa.jpg';
    else if ($d == 5 && $h >= 8 && $h < 10) $img = 'img/hosts/shonw.jpg';
    else if ($d == 5 && $h >= 10 && $h < 12) $img = 'img/hosts/patm.jpg';
    else if ($d == 5 && $h >= 12 && $h < 13) $img = 'img/hosts/mikef.jpg';
    else if ($d == 5 && $h >= 13 && $h < 15) $img = 'img/hosts/edp.jpg';
    else if ($d == 5 && $h >= 15 && $h < 18) $img = 'img/hosts/lizzy.jpg';
    else if ($d == 5 && $h >= 18 && $h < 20) $img = 'img/hosts/jeremyb.jpg';
    else if ($d == 5 && $h >= 20 && $h < 22) $img = 'img/hosts/exfyl.jpg';
    else if ($d == 5 && $h >= 22) $img = 'img/hosts/stickyb.jpg';
    else if ($d == 6 && $h < 0) $img = 'img/hosts/stickyb.jpg';
    
    // SATURDAY SCHEDULE
    else if ($d == 6 && $h >= 0 && $h < 4) $img = 'img/hosts/techtronic.jpg';
    else if ($d == 6 && $h >= 4 && $h < 5) $img = 'img/hosts/pmw.jpg';
    else if ($d == 6 && $h >= 5 && $h < 8) $img = 'img/hosts/geoffh.jpg';
    else if ($d == 6 && $h >= 8 && $h < 9) $img = 'img/hosts/tomf.jpg';
    else if ($d == 6 && $h >= 9 && $h < 10) $img = 'img/hosts/jimmyj.jpg';
    else if ($d == 6 && $h >= 10 && $h < 11) $img = 'img/hosts/jasonr.jpg';
    else if ($d == 6 && $h >= 11 && $h < 12) $img = 'img/hosts/hollyk.jpg';
    else if ($d == 6 && $h >= 12 && $h < 13) $img = 'img/hosts/tomt.jpg';
    else if ($d == 6 && $h >= 13 && $h < 14) $img = 'img/hosts/seanf.jpg';
    else if ($d == 6 && $h >= 14 && $h < 15) $img = 'img/hosts/pmw.jpg';
    else if ($d == 6 && $h >= 15 && $h < 17) $img = 'img/hosts/aaron_jenny.jpg';
    else if ($d == 6 && $h >= 17 && $h < 19) $img = 'img/hosts/pmw.jpg';
    else if ($d == 6 && $h >= 19 && $h < 22) $img = 'img/hosts/mattb.jpg';
    else if ($d == 6 && $h >= 22) $img = 'img/hosts/hairballj.jpg';
    else if ($d == 0 && $h < 0) $img = 'img/hosts/hairballj.jpg';
    
    // SUNDAY SCHEDULE
    else if ($d == 0 && $h >= 0 && $h < 2) $img = 'img/hosts/darrelm.jpg';
    else if ($d == 0 && $h >= 2 && $h < 4) $img = 'img/hosts/techtronic.jpg';
    else if ($d == 0 && $h >= 4 && $h < 5) $img = 'img/hosts/bigjon.jpg';
    else if ($d == 0 && $h >= 5 && $h < 6) $img = 'img/hosts/joebear.jpg';
    else if ($d == 0 && $h >= 6 && $h < 8) $img = 'img/hosts/russh.jpg';
    else if ($d == 0 && $h >= 8 && $h < 9) $img = 'img/hosts/ronk.jpg';
    else if ($d == 0 && $h >= 9 && $h < 10) $img = 'img/hosts/rockpoint.jpg';
    else if ($d == 0 && $h >= 10 && $h < 11) $img = 'img/hosts/churchatqc.jpg';
    else if ($d == 0 && $h >= 11 && $h < 12) $img = 'img/hosts/desertcf.jpg';
    else if ($d == 0 && $h >= 12 && $h < 16) $img = 'img/hosts/kristenm.jpg';
    else if ($d == 0 && $h >= 16 && $h < 17) $img = 'img/hosts/cdogg.jpg';
    else if ($d == 0 && $h >= 17 && $h < 18) $img = 'img/hosts/snarf_daff.jpg';
    else if ($d == 0 && $h >= 18 && $h < 19) $img = 'img/hosts/sonicsociety.jpg';
    else if ($d == 0 && $h >= 19 && $h < 21) $img = 'img/hosts/jscott.jpg';
    else if ($d == 0 && $h >= 21) $img = 'img/hosts/ghostlytalk.jpg';
    else if ($d == 1 && $h < 0) $img = 'img/hosts/ghostlytalk.jpg';
    ?>
    and then an image tag in the body of the page:

    Code:
    <img src="<?php echo $img; ?>">

    How do i create the PHP code from the HEAD section in it's own file, and just include that in my multiple pages? That way, I only have to update the host schedule in one file and it updates to all of the pages. I've seen something like an "include" tag before, but I'm a php newbie and am not sure how to implement it.

    Thanks for any help you can provide!

    Best regards,
    Torry
    Last edited by yrrot; 01-02-2007 at 05:50 AM. Reason: More Clarification

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Well, you could use

    Code:
    include('file.php');
    where file is the filename of the code you posted above. Then you would still need to place the img tag where it is now.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Dec 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I was hoping it would be simple. Do I still surround it with the PHP tag?

    Code:
    <?php
    include('file.php');
    ?>
    Thanks again,
    Torry

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    If there is none around the rest of the page, then yes.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Dec 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thaks TestingSite. Worked perfectly!

    Regards,
    Torry

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
  •