dongzhan8620 2014-12-18 20:30
浏览 40
已采纳

如果语句覆盖下面的CSS语句,停止PHP? 用MySQL纠正?

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!

  • 写回答

1条回答 默认 最新

  • dpjpo746884 2014-12-18 21:27
    关注

    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'];)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)