Solved

modify on-air now php script

Posted on 2012-03-15
12
212 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

920 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

16 Experts available now in Live!

Get 1:1 Help Now