0

I have a calendar where there are different background colours applied to each cell dependant on different variables using PHP. I'm using if statements to alter the style set to the TD based on whether the day is todays date, if a shift has been booked that day or if a holiday is booked that day.

This has worked, but then I realised that if a holiday was booked on a day that is a shift regardless of whether or not its a holiday it still gets coloured as a shift. I understand that this is because of the order of the IF statements. So I was going to ask whether there was any way around this...

But then I realised as I'm working with a system with different users not just one person, if it was one person this wouldn't matter because they can't book the same day as a shift AND holiday.

However... each user can see if a day is booked as a shift or holiday by the colour of it (without showing more detail, such as the user who has that day booked) that I would need to assign a different colour to a day that has been booked for both a shift or a holiday.

So after realising all this (sorry if its useless backstory) my question is now (at least I think it is..!) how do I create a MySQL statement that will select from the table where the date booked has entries as a shift and a holiday.

My database is designed with five columns: id, surname, stafflevel, datebooked and typebooked. my current code for the way it works now is:

$ifShift ="SELECT * FROM schedule WHERE dateBooked = '".$dateToCompare."' AND typeBooked = 'shift'";
$tdShift = mysql_num_rows(mysql_query($ifShift));
$ifHoliday ="SELECT * FROM schedule WHERE dateBooked = '".$dateToCompare."' AND typeBooked = 'holiday'";
$tdHoliday = mysql_num_rows(mysql_query($ifHoliday));

if ($tdShift) {
    echo "class='shift'";
} elseif($tdHoliday) {
        echo "class='holiday'";
}

I tried using

select * from schedule where dateBooked = '26/12/2014' AND (typeBooked = 'shift' AND 'holiday');

in terminal to test this with my table and got a message "Empty set, 1 warning"... so how do I go about this... if it's even possible?

But also wont I still have my original problem of the order of the if statements?

Thanks guys!

3
  • 1
    You're using 'and' wrong for one. It would look like AND (typeBooked = 'shift' AND typeBooked ='holiday'); Which when read makes you think...how could they be shift AND holiday? Another column would be your friend in that case. Commented Dec 18, 2014 at 20:45
  • Ok thanks for that, is there any way I could do it without adding another column? Although I guess what you suggest may be the easiest and can't believe I didn't think of it. But then... anyway around the if statements situation? Commented Dec 18, 2014 at 20:48
  • If you were to select the correct value from typeBooked then yes. Is typeBooked "shift" or "holiday" or "shiftHoliday"? It may seem like a lot of work, but adding separating your data will be amazing for you in the end. Commented Dec 18, 2014 at 21:26

1 Answer 1

1

Try this:

$classes = array();
if ($tdShift) {
    $classes[] = 'shift';
}
if($tdHoliday) {
    $classes[] = 'holiday';
}

if(!empty($classes)) {
    echo 'class="'.implode(' ',$classes).'"';
}

And in your css something similar to:

td.shift {
    background-color: #77B;
}
td.holiday {
    background-color: #B77;
}
td.shift.holiday {
    background-color: #7B7;
}

You should end up with blue shifts (heh), red holidays and green shifts on holidays. Modify at will.

[EDIT] Suggested one-query solution:

//grab all types for this date
$sched_types = mysql_query("SELECT typeBooked FROM schedule WHERE dateBooked = '".$dateToCompare."'");
//empty classes array
$classes = array();
//append classes array with collected types
if($sched_types && mysql_num_rows($sched_types) > 0) {
    while($sched_type = mysql_fetch_assoc($sched_types)) {
        $classes[] = $sched_type['typeBooked'];
    }
}
//echo your classes
if(!empty($classes)) {
    echo 'class="'.implode(' ',$classes).'"';
}
//free result
mysql_close($sched_types);

It should support more types, for instance - you could add a third record with typeBooked other than 'shift' or 'holiday' and the code will append a new class by itself. Also, it'd be better to use class names that won't collide with others, like "day-shift" and "day-holiday".

($classes[] = 'day-' . $sched_type['typeBooked'];)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Ecter that worked! Although no I've broken some other code in the meantime... hahah it never ends does it!
Well, of course you could make other parts of your code better, i just focused on the presented problem. For instance, you could just grab all records for this date in one query. I'll add this bit to my answer...
Thanks @Ecter yet again! I'll get that done when I've sorted out all my other little problems haha :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.