[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

modify on-air now php script

Posted on 2012-03-15
12
Medium Priority
?
228 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
6 Comments
 
LVL 111

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 111

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 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 71

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 111

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month18 days, 3 hours left to enroll

830 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