0

我有一个小问题,试图在某些条件下回显数据库中的数据。问题是在同一张表中,我在同一小时有 3 个不同的值。

带有 if 语句的过滤器运行良好,唯一的问题是如果发现“name1 from 07 to 08 and week 1”和“name2 from 07 to 08 and week 2”同时显示。

我的问题是,我怎样才能根据过滤器只显示 1?

请看一下数据库结构和 php 脚本。

//**    Table structure

     id name    week    start   end
    1   name1   1   07  08
    2   name2   1   08  09
    3   name3   2   07  08
    4   name4   3   07  08

     //$week    -> 1 from 1 to 5, 2 Saturday, 3 Sunday
     //$start   -> 07, 08 ... 14, hours
     //$end     -> 07, 08 ... 14, hours

     $Date = date('H');
     $user = '';
     $pass = '';
     $dbh = new PDO('mysql:host=localhost;dbname=mydatabase', $user, $pass);
     foreach($dbh->query('SELECT * from table') as $row):
        $week = $row['week'];
        $start = $row['start'];
        $end = $row['end'];

     if(($Date >= $start) && ($Date < $end) && ($week == 1)):
        echo 'This Week i will show this name' . $row['name'];

     elseif(($Date >= $start) && ($Date < $end) && ($week == 2)):
        echo 'This Saturday i will show this name' . $row['name'];

     elseif(($Date >= $start) && ($Date < $end) && ($week == 3)):
        echo 'This Sunday i will show this name' . $row['name'];
     endif;
     endforeach;

    $dbh = null;

任何帮助表示赞赏。

4

0 回答 0