Solved

modify on-air now php script

Posted on 2012-03-15
12
209 Views
Last Modified: 2014-08-03
I'm looking for a php script that will display an on-air schedule for a radio station I work for. Ray_Paseur did the heavy lifting and wrote the following code that accurately switches my images based on what time it is, but I also need to display:

a) the title of the show (as a hyperlink),
b) the time the show begins and ends,
c) the following show (as a hyperlink).

Plus, this script needs to include the entire week (weekend programs and weekday programs are different).


Here's Ray_Paseur's script, which masterfully rotates my images based on the time of day:
 
<?php // RAY_temp_billypilgrim32.php
error_reporting(E_ALL);


// SET THE CORRECT TIMEZONE
date_default_timezone_set('America/Chicago');


// A FUNCTION TO RETURN THE IMAGE URL BASED ON A GIVEN TIME
function imageUrl($hhmm='now')

{

    // DEFINE A PROGRAM CLASS
    if (!class_exists('Program'))
    {
        Class Program
        {
            public function __construct
            ( $a // START TIME
            , $z // END TIME
            , $i // IMAGE URL
&#9;&#9;&#9;, $t // Show Title
            )
            {
                $this->a = $a;
                $this->z = $z;
                $this->i = $i;
            }
        }
    }

    // MAKE AN ARRAY OF PROGRAM OBJECTS
    $programs = array
    ( new Program('0000', '0200', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host1.jpg')
    , new Program('0200', '0400', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/shows/radionews.gif')
    , new Program('0400', '0600', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host2.jpg')
    , new Program('0600', '0800', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host3.jpg')
    , new Program('0800', '1000', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host4.jpg')
    , new Program('1000', '1200', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host5.jpg')
    , new Program('1200', '1400', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host6.jpg')
    , new Program('1400', '1600', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host1.jpg')
    , new Program('1600', '1800', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host7.jpg')
    , new Program('1800', '2000', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host8.jpg')
    , new Program('2000', '2200', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host9.jpg')
    , new Program('2200', '2400', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host10.jpg')
    )
    ;

    // NORMALIZE THE DATETIME STRING
    $hhmm = date('hi', strtotime($hhmm));

    // DETERMINE THE IMAGE NEEDED
    foreach ($programs as $timeslot)
    {
        if ($hhmm >= $timeslot->a)
        {
            if ($hhmm <= $timeslot->z)
            {
                $url = $timeslot->i;
            }
        }
    }

    // CREATE THE IMAGE TAG
    $tag = <<<TAG
    <img alt="Host Image" title="$hhmm" src="$url" />
TAG;

    return $tag;
}

// TEST THE FUNCTION
echo '<br clear="all" />' . imageurl();
echo '<br clear="all" />' . imageurl('now+37 minutes');
echo '<br clear="all" />' . imageurl('now+237 minutes');
?>

Open in new window


Thanks!
0
Comment
Question by:billypilgrim32
  • 4
12 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37727562
In my initial post, the strategy was to create an array of objects.  My thinking worked along the lines of "What else should these objects contain?"  Your line 23 giving a title plays nicely into the design pattern. I think we know the time the show begins and ends from the $a and $z arguments that are passed to the class constructor.  I think the only remaining issue is to find the object in the array that starts after the current start time.  We can either assume that the array is ordered by start time (probably a brittle assumption) or we can use USort() to order the array of objects by start times and end times.  

I am going to bed now, and I will look at it again in the morning.   However there are many wise and clever developers here at EE who may be able to get a quick answer overnight.

Best regards, ~Ray
0
 

Author Comment

by:billypilgrim32
ID: 37729635
Appreciate all your hard work Ray---the challenge for me is to somehow deconstruct this brilliant piece of coding. I'm just an HTML guy, so all of this looks like Mandarin Chinese to me.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37768933
I'll look at this again.  If it still looks chinese to you, then you really should consider hiring a professional developer to help you with it.  Nothing you want is "hard" but it is time-consuming and a developer should be paid for the time it will take to do all you need to have done.  If this does not have any economic value and you're just using it as a learning tool, then consider getting this excellent little book and give yourself a few weeks to work through the examples.  It will not make you a pro, but it will give you some foundation in how PHP and MySQL work together.
http://www.sitepoint.com/books/phpmysql4/
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37769179
This is as far as I can get tonight.  But hopefully it will provide some inspiration for you or your developer.  Best regards, ~Ray
<?php // RAY_temp_billypilgrim32.php
error_reporting(E_ALL);


// SET THE CORRECT TIMEZONE
date_default_timezone_set('America/Chicago');


// DEFINE A PROGRAM CLASS
Class Program
{
    public function __construct
    ( $a // START TIME
    , $z // END TIME
    , $i // IMAGE URL
    , $t // TITLE
    )
    {
        $this->a = $a;
        $this->z = $z;
        $this->i = $i;
        $this->t = $t;
    }
    public function getTitle() { return $this->t; }
    public function getTimes() { return array('a' => $this->a, 'z' => $this->z); }
    public function getImage() { return $this->i; }
}


// MAKE AN ARRAY OF PROGRAM OBJECTS
$programs = array
( new Program('0000', '0200', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host1.jpg', 'Title 0000')
, new Program('0200', '0400', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/shows/radionews.gif', 'Title 0200')
, new Program('0400', '0600', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host2.jpg', 'Title 0400')
, new Program('0600', '0800', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host3.jpg', 'Title 0600')
, new Program('0800', '1000', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host4.jpg', 'Title 0800')
, new Program('1000', '1200', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host5.jpg', 'Title 1000')
, new Program('1200', '1400', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host6.jpg', 'Title 1200')
, new Program('1400', '1600', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host1.jpg', 'Title 1400')
, new Program('1600', '1800', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host7.jpg', 'Title 1600')
, new Program('1800', '2000', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host8.jpg', 'Title 1800')
, new Program('2000', '2200', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host9.jpg', 'Title 2000')
, new Program('2200', '2400', 'http://www.scotthennen.com/wp-content/themes/shs/schedule/host10.jpg', 'Title 2200')
)
;

// A FUNCTION TO RETURN THE IMAGE URL BASED ON A GIVEN TIME
function imageUrl($hhmm='now', $programs=array())
{
    // NORMALIZE THE DATETIME STRING
    $hhmm = date('hi', strtotime($hhmm));

    // DETERMINE THE IMAGE NEEDED
    foreach ($programs as $timeslot)
    {
        if ($hhmm >= $timeslot->a)
        {
            if ($hhmm <= $timeslot->z)
            {
                $url = $timeslot->getimage();
            }
        }
    }

    // CREATE THE IMAGE TAG
    $tag = <<<TAG
    <img alt="Host Image" title="$hhmm" src="$url" />
TAG;

    return $tag;
}

// TEST THE FUNCTION
echo PHP_EOL . '<br clear="all" />' . imageurl('now', $programs);
echo PHP_EOL . '<br clear="all" />' . imageurl('now+37 minutes', $programs);
echo PHP_EOL . '<br clear="all" />' . imageurl('now+237 minutes', $programs);


// SHOW THE PROGRAM NAMES AND THE TIMES
foreach ($programs as $thing)
{
    // CAST THE OBJECT PROPERTY AS STRING VARIABLE
    $t = (string)$thing->getTitle();

    // USE THE OBJECT PROPERTY AS AN ARRAY VARIABLE
    $x = $thing->getTimes();
    echo PHP_EOL . '<br clear="all" />' . "$t STARTS AT {$x['a']} AND ENDS AT {$x['z']}";
}

Open in new window

0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40234157
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40234158
Recommend accept this.  It has a tested and working code sample that shows exactly how to solve the problem:
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_27634413.html#a37769179
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now