douzuan5365 2016-02-09 10:16
浏览 135

像MWF,TThS这些缩短的日子(这是我的班级时间表)

Need help on this: in my sql table I have this Schedule and is written in MWF, TThS, Sun. I want it to be filtered so that only the schedule for today will be the output...e.g Today is Tuesday so TThS schedule is being outputted. Hope you could help me.

  • 写回答

1条回答 默认 最新

  • dongyi7513 2016-02-09 11:31
    关注

    The problem with the way your schedules times are stored is that you can't do a simple LIKE %T%, as it would get schedules for Th as well.

    A possible solution would be to use MySQL REGEXP for the specific cases of Tuesday (T not followed by h) and Saturday (S not followed by un).

    Basically you would build an array of the where clauses that should be used depends of the week day:

    $where_date_clauses = array(
        1 => 'LIKE "M%"',
        2 => 'REGEXP "T(?!h)"',
        3 => 'LIKE "%W%"',
        4 => 'LIKE "%Th%"',
        5 => 'LIKE "%F%"',
        6 => 'REGEXP "S(?!un)"',
        7=> 'LIKE "%Sun"'
    );
    

    The regexp here use the negative lookahead pattern - it will match only if the character is not immediately followed by the lookahead content.

    And then build your query like this:

    $sql = 'SELECT * FROM schedules_table WHERE schedule ' . $where_date_clauses[date('N')];
    

    Where date('N') return the number of the week day.

    Side note

    Here is an example of how to set up your query using PDO:

    try {
        $dbh = new PDO('mysql:dbname=your_db_name;host=your_db_host', 'mysql_user_name', 'mysql_user_password');
        $sth = $dbh->prepare('SELECT * FROM block_subject2 where id_number = ? ORDER BY timed DESC LIMIT 1');
        $sth->execute(array(
            (int) $_SESSION['stud_id']
        ));
        $row = $sth->fetch();
    } catch(PDOException $e) {
        echo 'Failed to connect to DB: ' . $e->getMessage();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大