尝试合并两个组件时推荐脚本不起作用

时间:2019-03-02 13:24:07

标签: php mysql

上下文 我正在构建一个简单的推荐脚本,该脚本可为用户提供他/她预订最多的一周中特定日期的下一个即将到来的日期。 (即JohnDoe最受欢迎的预订日期是星期四,下一个星期四的日期是2019/03/07)

这就是我要处理的:

<?php
      $date = new DateTime();
      $date->modify('next thursday');
      echo $date->format('Y-m-d');
?>

<?php require "snippets/get_booking_recommended_day.php" ?>

第一个PHP代码返回要求下一天的下一个即将到来的日期。它可以正常工作。 PHP需要另一个文件夹中的引用代码,该文件夹以String格式返回用户最受欢迎的日期。 (例如,星期一,星期二)。也可以。

我的问题是试图获取第一部分代码以了解第二部分代码返回的内容。

我尝试了以下操作...

<?php
   $date = new DateTime();
   $date->modify('next' require "snippets/get_booking_recommended_day.php");
   echo $date->format('Y-m-d');
 ?>

我尝试了所有可能的变化。似乎没有任何作用。 我对PHP还是很陌生,我90%的人确信我的编码实践很糟糕,但是我正在竭尽全力来掌握它-但是到目前为止,这个简单的问题超出了我的范围。

请帮助。

附录

文件名:snippets / get_booking_recommended_day.php

(返回会话中当前用户最近3个月预订最多的日期)

 <?php
 if ($mysqli->connect_errno) {
   echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
 }

// Asks the qry: Of the last 90 days, what is the most booked day of the week for the current member in session?
// Min date is CURRENT_DATE() -100 instead of CURRENT_DATE() -30, because the MySQL function CURRENT_DATE() prints the date in an int format (YYYYMMDD) with no date formatting. Thus, to get the date a month ago, we must subtract this int by 100 so as to remove 1 from the 6th number in the series of numbers. Which is the 2nd M number.
 $sql = "SELECT  DATE_FORMAT(tbl_booking.booking_date, '%W'), COUNT(DATE_FORMAT(tbl_booking.booking_date, '%W')) AS mostpopularday
              FROM tbl_booking
              WHERE tbl_booking.member_ID=$_SESSION[member_ID]
                 AND tbl_booking.booking_date <= CURRENT_DATE()
                 AND tbl_booking.booking_date >= CURRENT_DATE() -300
              GROUP BY DATE_FORMAT(tbl_booking.booking_date, '%W')
              ORDER BY mostpopularday DESC
              LIMIT 1";
 $result = $mysqli->query($sql);

 if ($result->num_rows > 0) {
   // output data of each row
   while($row = $result->fetch_assoc()) {
      echo $row["DATE_FORMAT(tbl_booking.booking_date, '%W')"];

   }
 } else {
     // Return Nothing.
 }
 ?>

文件名:pagebooking.php

(这是我的bookbooking.php内的日期选择器,其目的是选择预订日期。我希望将此字段填充为建议的日期,该日期将从2 PHP中生成上面的脚本。)

<input name="new_booking_date" width="276"  placeholder="Date" class="form-control input-md" type="date" max="<?php echo date("Y-m-d", strtotime("+30 day")); ?>" min="<?php echo date("Y-m-d", strtotime("+1 day")); ?>" required="" />    

1 个答案:

答案 0 :(得分:0)

问题已解决。

 <?php
 // If there are any values in the table, display them one at a time.
 if ($mysqli->connect_errno) {
   echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
 }

// Asks the qry: Of the last 90 days, what is the most booked day of the week for the current member in session?
// Min date is CURRENT_DATE() -100 instead of CURRENT_DATE() -30, because the MySQL function CURRENT_DATE() prints the date in an int format (YYYYMMDD) with no date formatting. Thus, to get the date a month ago, we must subtract this int by 100 so as to remove 1 from the 6th number in the series of numbers. Which is the 2nd M number.
 $sql = "SELECT  DATE_FORMAT(tbl_booking.booking_date, '%W'), COUNT(DATE_FORMAT(tbl_booking.booking_date, '%W')) AS mostpopularday
              FROM tbl_booking
              WHERE tbl_booking.member_ID=$_SESSION[member_ID]
                 AND tbl_booking.booking_date <= CURRENT_DATE()
                 AND tbl_booking.booking_date >= CURRENT_DATE() -300
              GROUP BY DATE_FORMAT(tbl_booking.booking_date, '%W')
              ORDER BY mostpopularday DESC
              LIMIT 1";
 $result = $mysqli->query($sql);

 if ($result->num_rows > 0) {
   // output data of each row
   while($row = $result->fetch_assoc()) {
      $recommended_day = $row["DATE_FORMAT(tbl_booking.booking_date, '%W')"];

      $date = new DateTime();
      $date->modify('next '.$recommended_day);
      echo $date->format('Y-m-d');

   }
 } else {

 }
 ?>