duandushang5148 2016-05-23 20:41
浏览 33

存储打开和关闭时间,打开时显示关闭

I am trying to show if a shop is opened or closed, based on opening and closing times in my db. If it is open show the opening and closing times for that day, if it is closed echo closed. My problem at the moment is my query is echoing closed even when the shop is scheduled to be open (attempt 1) or is not echoing anything at all (attempt 2).

A closed store in my DB is represented as 00:00. Any suggestions or guidance would be greatly appreciated, as i am teaching myself and have come to a stand still.

DB

CREATE TABLE `Opening_hrs` (
`OH_ID` bigint(255) NOT NULL AUTO_INCREMENT,
`Restaurant_ID` bigint(255) NOT NULL,
`Day_of_week` int(11) NOT NULL,
`Open_time` time NOT NULL,
`Closing_time` time NOT NULL,
PRIMARY KEY (`OH_ID`),
KEY `Restaurant_ID` (`Restaurant_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8

This was my first attempt

date_default_timezone_set("Europe/London");
  $output_ohr = '';

  $ohrs = mysqli_query($dbc, "SELECT * FROM Opening_hrs 
WHERE Restaurant_ID='$rest_id' AND Day_of_week = DATE_FORMAT(NOW(), '%w')
AND CURTIME() BETWEEN Open_time AND Closing_time");

echo var_dump($ohrs);

$count_ohrs = mysqli_num_rows($ohrs);
if ($count_ohrs === 0) {
    $output_ohr = '<b> Closed</b>';
} else {
    $i = 1;
}while ($row_ohr = mysqli_fetch_array($ohrs )) {
    $o_time = $row_ohr['Open_time'];
    $c_time = $row_ohr['Closing_time'];



    $output_ohr = $output_ohr . '<p>Open</p>' .
            '<p>' .$o_time. ' - ' .$c_time. '</p>' 
    ;
    $i++;
 }

My second attempt

  date_default_timezone_set("Europe/London");

  $closed= strtotime("00:00am today GMT");
  $output_ohr = '';

  $ohrs = mysqli_query($dbc, "SELECT * FROM Opening_hrs 
WHERE Restaurant_ID='$rest_id' AND Day_of_week = DATE_FORMAT(NOW(), '%w')
AND CURTIME() BETWEEN Open_time AND Closing_time");

  echo var_dump($ohrs);

    $i = 1;
   while ($row_ohr = mysqli_fetch_array($ohrs )) {
    $o_time = $row_ohr['Open_time'];
    $c_time = $row_ohr['Closing_time'];

   if($o_time === $closed){
   $output_ohr = '<p>closed</p>';
  }else{

    $output_ohr = $output_ohr . '<p>Open</p>' .
            '<p>' .$o_time. ' - ' .$c_time. '</p>' 
    ;
    $i++;
      }
  }       
  • 写回答

1条回答 默认 最新

  • druybew06513 2016-05-23 21:09
    关注

    In your second attempt you are selecting only the open shops. If the shop is closed, the query won't return any rows and thus the while block does not run even once.

    Use the following code to see if a shop is open or closed:

    $stmt = mysqli_prepare($dbc, "SELECT COUNT(*) FROM Opening_hrs 
    WHERE Restaurant_ID=? AND Day_of_week = DATE_FORMAT(NOW(), '%w')
    AND CURTIME() BETWEEN Open_time AND Closing_time");
    
    $stmt->bind_param('i', $rest_id);
    $stmt->execute();
    $stmt->bind_result($result);
    $stmt->fetch();
    
    if ($result) {
        echo 'Open';
    } else {
        echo 'Closed';
    }
    

    (Note the usage of prepared statements that prevent a possible SQL injection.)

    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答