Do not display blank fields

HoddzDJ

Active member
Hello everyone, I am back for a little help/advice on what to do! As most of you will be aware, I run an online radio site and I am currently re-designing it to look fresher and cleaner than it currently does. I use a DJ Panel that is coded in PHP and uses a MySQL Database which is all well and good and honky dory.

One of the features of it is an online schedule booking system that allows any DJ to book and un-book their sets. On our public website we then grab this data and display it for our visitors to see who is on and when. Simple enough.

Something that I would like to do now is only display the entries from today that have a DJ booked. All slots where no-one is booked are blank so it should be a case of running an if such and such == "" then do not display, but I wouldn't know where to start for this!!

Below I have pasted the code that is currently used to grab all of the times from the current day.

PHP:
$day = $_GET['week'];
if($_GET['week'] == ""){
$day = date("l",time() + 18000);
$_GET['week'] = date("l",time() + 18000);
}
$result = mysql_query("SELECT * FROM rp_timetable WHERE day = '$day'");
while($slot = mysql_fetch_assoc($result)) {
if($slot["1"] == "")
{ $one = "OPEN SLOT"; }
else
{ $one = "<b>DJ {$slot["1"]}</b>"; }

if($slot["2"] == "")
{ $two = "OPEN SLOT"; }
else
{ $two = "<b>DJ {$slot["2"]}</b>"; }

if($slot["3"] == "")
{ $three = "OPEN SLOT"; }
else
{ $three = "<b>DJ {$slot["3"]}</b>"; }

if($slot["4"] == "")
{ $four = "OPEN SLOT"; }
else
{ $four = "<b>DJ {$slot["4"]}</b>"; }

if($slot["5"] == "")
{ $five = "OPEN SLOT"; }
else
{ $five = "<b>DJ {$slot["5"]}</b>"; }

if($slot["6"] == "")
{ $six = "OPEN SLOT"; }
else
{ $six = "<b>DJ {$slot["6"]}</b>"; }

if($slot["7"] == "")
{ $seven = "OPEN SLOT"; }
else
{ $seven = "<b>DJ {$slot["7"]}</b>"; }

if($slot["8"] == "")
{ $eight = "OPEN SLOT"; }
else
{ $eight = "<b>DJ {$slot["8"]}</b>"; }

if($slot["9"] == "")
{ $nine = "OPEN SLOT"; }
else
{ $nine = "<b>DJ {$slot["9"]}</b>"; }

if($slot["10"] == "")
{ $ten = "OPEN SLOT"; }
else
{ $ten = "<b>DJ {$slot["10"]}</b>"; }

if($slot["11"] == "")
{ $eleven = "OPEN SLOT"; }
else
{ $eleven = "<b>DJ {$slot["11"]}</b>"; }

if($slot["12"] == "")
{ $twelve = "OPEN SLOT"; }
else
{ $twelve = "<b>DJ {$slot["12"]}</b>"; }

if($slot["13"] == "")
{ $thirteen = "OPEN SLOT"; }
else
{ $thirteen = "<b>DJ {$slot["13"]}</b>"; }

if($slot["14"] == "")
{ $fourteen = "OPEN SLOT"; }
else
{ $fourteen = "<b>DJ {$slot["14"]}</b>"; }

if($slot["15"] == "")
{ $fifteen = "OPEN SLOT"; }
else
{ $fifteen = "<b>DJ {$slot["15"]}</b>"; }

if($slot["16"] == "")
{ $sixteen = "OPEN SLOT"; }
else
{ $sixteen = "<b>DJ {$slot["16"]}</b>"; }

if($slot["17"] == "")
{ $seventeen = "OPEN SLOT"; }
else
{ $seventeen = "<b>DJ {$slot["17"]}</b>"; }

if($slot["18"] == "")
{ $eighteen = "OPEN SLOT"; }
else
{ $eighteen = "<b>DJ {$slot["18"]}</b>"; }
if($slot["19"] == "")
{ $nineteen = "OPEN SLOT"; }
else
{ $nineteen = "<b>DJ {$slot["19"]}</b>"; }

if($slot["20"] == "")
{ $twenty = "OPEN SLOT"; }
else
{ $twenty = "<b>DJ {$slot["20"]}</b>"; }

if($slot["21"] == "")
{ $twentyone = "OPEN SLOT"; }
else
{ $twentyone = "<b>DJ {$slot["21"]}</b>"; }

if($slot["22"] == "")
{ $twentytwo = "OPEN SLOT"; }
else
{ $twentytwo = "<b>DJ {$slot["22"]}</b>"; }

if($slot["23"] == "")
{ $twentythree = "OPEN SLOT"; }
else
{ $twentythree = "<b>DJ {$slot["23"]}</b>"; }

if($slot["24"] == "")
{ $twentyfour = "OPEN SLOT"; }
else
{ $twentyfour = "<b>DJ {$slot["24"]}</b>"; }

$day = $_GET['week'];
echo "<p class=\"firstP\">You are currently viewing ";
echo "$day";
echo "'s times<p>";
echo "<p>0000 | $one<br />
0100 | $two<br />
0200 | $three<br />
0300 | $four<br />
0400 | $five<br />
0500 | $six<br />
0600 | $seven<br />
0700 | $eight<br />
0800 | $nine<br />
0900 | $ten<br />
1000 | $eleven<br />
1100 | $twelve<br />
1200 | $thirteen<br />
1300 | $fourteen<br />
1400 | $fifteen<br />
1500 | $sixteen<br />
1600 | $seventeen<br />
1700 | $eighteen<br />
1800 | $nineteen<br />
1900 | $twenty<br />
2000 | $twentyone<br />
2100 | $twentytwo<br />
2200 | $twentythree<br />
2300 | $twentyfour<p>";
}

Any help would be greatly appreciated!!
 
I hope this helps you, and I've optimised the code. :)
PHP:
// create an array with the hours of the day, from 0 to 23
$hours = range(0, 23);

$valid_days = array (
    'Sunday',
    'Monday',
    'Tuesday',
    'Wednesday',
    'Thursday',
    'Friday',
    'Saturday',
);

if($_GET['week'] == "" OR !in_array($_GET['week'], $valid_days)) // you should also check if $_GET['week'] is valid
{
    $_GET['week'] = date("l",time() + 18000);
}

$day = $_GET['week'];

$result = mysql_query("SELECT * FROM rp_timetable WHERE day = '$day'");
while($slot = mysql_fetch_assoc($result))
{
    echo "<p class=\"firstP\">You are currently viewing ";
    echo "$day";
    echo "'s times</p>\n";
    echo "<p>\n";

    foreach($hours AS $hour)
    {
        if($slot[$hour] != "")
        {
            // a little bit formatting
            // if you want to assing this value to a variable use sprintf instead,
            // read the manual at php.net
            printf("%02d00 | <b>DJ %s</b><br />\n", $hour, $slot[$hour]);
        }
    }

    echo "</p>";
}
 
Wow! I didn't even realise anyone had replied to this, so thanks both of you!! I have just tried the code that Boothby has put and it works like a charm, the only thing I can think of is if there is a way to display the time and then the end time. I know it wasn't in my original code, but that was because I display even the empty slots so it's not really needed. All slots are an hour long, so it would be 2000 - 2100 or 0800 - 0900 etc. Do you know an easy way to do this?

Something else, is there a way to display a paragraph if there are no slots available?

Again, thanks so much for your help!!
 
Or something else, if someone books a slot for two sets one after the other, maybe block them? At the moment it would display as
0800 - DJ Someone
0900 - DJ Someone

When maybe it could be 0800 - 1000 - DJ Someone if you know what I mean? Not sure if it's even possible, or if it's too hard to do?
 
Something I have just noticed, is that the times are out by an hour. A set that should be at 10 and is at 10 on our current schedule is being displayed at 9 on the schedule that only displayed the booked slots. I think it is something to do with grabbing the times, but I guess I could change the way it's displayed on the other pages, but it would require a lot of work.

At the moment there is no rush for the new feature to be rolled out, and it is being tested in a private directory so it's not a problem. Would just love to get it right before a publish it live!
 
I hope I understood all correct.

PHP:
<?php

// create an array with the hours of the day, from 0 to 23
$hours = range(0, 23);

$valid_days = array (
    'Sunday',
    'Monday',
    'Tuesday',
    'Wednesday',
    'Thursday',
    'Friday',
    'Saturday',
);

if($_GET['week'] == "" OR !in_array($_GET['week'], $valid_days)) // you should also check if this $_GET['week'] is valid
{
    $_GET['week'] = date("l",time() + 18000);
}

$day = $_GET['week'];

$result = mysql_query("SELECT * FROM rp_timetable WHERE day = '$day'");
while($slot = mysql_fetch_assoc($result))
{
    echo "<p class=\"firstP\">You are currently viewing ";
    echo "$day";
    echo "'s times</p>\n";
    echo "<p>\n";

    $lastslot = null;
    foreach($hours AS $hour)
    {
        $slot_number = $hour + 1;
        if($lastslot === null)
        {
            $starthour = $hour;
        }

        if($slot[$slot_number] != "")
        {
            if(($hour + 1) <= 23 AND $slot[$slot_number + 1] == $slot[$slot_number])
            {
                $lastslot = $slot[$slot_number];
                continue;
            }
            else
            {
                $endhour = $hour + 1;
                printf("%02d00 - %02d00 | <b>DJ %s</b><br />\n", $starthour, $endhour == 24 ? 0 : $endhour, $slot[$slot_number]);
                $lastslot = null;
            }
        }
    }

    echo "</p>";
}
 
Wow! It worked like a charm. The only thing that is left confusing me now is that the alterations on the time seem to be out for some reason. I would't know how to even find out why, but on our main site we use +18000 and it puts the time to the UK time (currently BST) but it seems to shoot it way off in that code :/
 
Okay, I have changed it to add on an hour now (+3600) and that has set it to the right date so hopefully that will fix it. I have no idea why it has done that as the time is displaying correctly on our public site with 18000, how very strange! I will have to check what time the server we are using is set to now, and make sure it's not all wrong!!

I'd like to take this opportunity to thank Boothby so much, and if there is anything I could do then don't hesitate to say!
 
For some reason it should be 18000 but it's pushing it right out and making the time much to fast, and I really can't figure out why. I have put in the some PHP date format as below and the date that is displayed is below that also to keep things clear...

PHP:
<?php $serverdate = date('l, jS F Y. H:i',time()); echo($serverdate); ?>
The output I get from that is 'Monday, 10th October 2011. 17:12' and the UK time is 22:12 so that is 5 hours ahead.

I alter it using seconds, so 60*60*5 = 18,000 so I add that in put then I bumps it up to 'Tuesday, 11th October 2011. 02:16' and that is 4 hours too far ahead! I'm not sure if it's mixing two things up in the PHP somewhere, but I can't think of anything that would cause this, it seems really strange!!
 
Okay, with a bit more tweaking about, I think I have managed to figure it out! I had a PHP date function at the bottom of my page to get the current year for our copyright text, 2007 - *current year* and I have now removed that and it seems to be fine!

Got there in the end!!

Thanks again for your help Boothby, if there is anything we can do please shout up!!
 
Before
Code:
echo "<p class=\"firstP\">You are currently viewing ";
add:

Code:
$today_live_sets = false;

Before
Code:
printf("%02d00 - %02d00 | <b>DJ %s< ...
add:

Code:
$today_live_sets = true;

After last
Code:
echo "</p>";
add:
Code:
if($today_live_sets == false)
{
    echo "<p>No live sets today!</p>\n";
}
 
Top Bottom