通过比较他们的时间和场地来避免事件的冲突

时间:2013-11-01 08:56:09

标签: php forms date time

这对你们中的一些人来说可能很容易,但对我来说这是一个很大的麻烦: 我想要做的是通过比较他们的日期和场地来确保两个事件不会发生冲突。 以下是我到目前为止所做的事情:

<?php
    require_once (LIB_PATH.DS.'database.php');
    require_once ('../includes/initialize.php');

    class Event extends DatabaseObject {

        protected static $table_name="event_tbl";

        protected static $db_fields = array('id', 'visible', 'event_title', 'start_date', 'end_date', 'start_time', 'end_time', 'venue', 'event_type', 'event_description', 'event_program', 'reminder_date', 'reminder_time');

        public $id;
        public $event_title;
        public $start_date;
        public $end_date;
        public $start_time;
        public $end_time;
        public $venue;
        public $event_type;
        public $event_description;
        public $event_program;
        public $visible;        
        public $reminder_date;
        public $reminder_time;



        public function avoid_clash($time1, $time2, $database_venue, $time3, $time4, $venues) {
        $timeStart = strtotime("{$time1}");
        $timeEnd   = strtotime("{$time2}");
        $time      = strtotime("{$time3}") - strtotime("{$time4}");

        if ($result =($time > $timeStart && $time < $timeEnd) && $database_venue==$venues) {
            $session->message('Warning: This event clashes with another!');
            redirect_to('event_management.php');
        }
    } 

    }



    /*function process_avoid_clash($time1, $time2, $time3, $time4, $venues) {
        $event = self::find_from_event();
        $row = fetch_array($event);

        foreach ($row as $rows) {
            $time1 = $rows['start_date']." ".$rows['start_time'];
            $time2 = $rows['end_date']." ".$rows['end_time'];
            $database_venue = $rows['venue'];

            if ($time1 && $time2 && $database_venue) {
                avoid_clash($time1, $time2, $time3, $time4, $venues);
            } else {
                 die("find_from_event failed!");
            }
        }
    }*/



?>

然后我通过使用一个表单来测试它,当提交时会提醒用户两个事件是否发生冲突:

<?php

    require_once ('../includes/initialize.php');


    if(!$session->is_logged_in()) {
        redirect_to("login.php");
    }
?>
<?php
    if (isset($_POST['submit'])) {

        $event = new Event();
        $even = Event::find_public();

                $time3  = $_POST['start_date']." ".$_POST['start_time'];
                $time4  = $_POST['end_date']." ".$_POST['end_time'];
                $venues = $_POST['venue'];

         foreach($even as $events):
                $time1  = $events->start_date." ".$events->start_time;
                $time2  = $events->end_date." ".$events->end_time;
                $database_venue = $events->venue;

                if ($time1 && $time2 && $database_venue) {
                 $event->avoid_clash($time1, $time2, $database_venue, $time3, $time4, $venues);
                }       
                endforeach; 



        $event-> event_title       = $_POST['event_title'];

        $event-> start_date        = $_POST['start_date'];

        $event-> end_date          = $_POST['end_date'];

        $event-> start_time        = $_POST['start_time'];

        $event-> end_time          = $_POST['end_time'];

        $event-> venue             = $_POST['venue'];

        $event-> event_type        = $_POST['event_type'];

        $event-> event_description = $_POST['event_description'];

        $event-> event_program     = $_POST['event_program'];

        $event-> visible           = $_POST['visible'];

        $event-> reminder_date     = $_POST['reminder_date'];

        $event-> reminder_time     = $_POST['reminder_time'];

        $event->create();
        $session->message('Event successfully created!');
        redirect_to('event_management.php');


    }



?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />

<title>New Event</title>

<link rel="stylesheet" type="text/css" href="../anytime/anytime.css"/>

<script src="../anytime/jquery.min.js"></script>

<script src="../anytime/jquery-migrate-1.0.0.js"></script>

<script src="../anytime/anytime.js"></script>
<link href="../assets/css/test_project.css" rel="stylesheet" type="text/css" />

<link href="../assets/css/links.css" rel="stylesheet" type="text/css" media="screen" />

</head>



<body>

<div id="container">

  <div id="header">

    <div class="logo"></div>

    <div class="menu">

   <div id="navcontainer">
        <ul id="navlist">
        </ul>

   </div>

 <br class="clear"/>

   </div>

    <br class="clear"/>

  </div> 

<div id="wrapper">

    <div id="sidebar1"> 
    <p><?php echo output_message($message); ?></p>

      <form id="form1" name="form1" method="post" action="new_event.php">

        <table width="578" height="322" border="0" cellpadding="0">

          <tr>

            <td colspan="3" align="right" valign="top">Event Title:</td>

            <td colspan="5" align="left" valign="top"><input type="text" name="event_title" /></td>
          </tr>

          <tr>

             <td width="3" align="center" valign="top">&nbsp;</td>

            <td width="89" align="center" valign="top"><input name="start_date" type="text" size="14" id="start_date" value="START DATE"/></td>

            <td width="60" align="center" valign="top"><input name="start_time" type="text" size="8" id="start_time" value="TIME"/></td>

            <script>

                AnyTime.picker("start_date",

                {format: "%Y-%c-%e"});

                $("#start_time").AnyTime_picker(

                { format: "%H:%i", LabelTitle: "Time",

                labelHour: "Hour", labelMinute: "Minute" });

            </script>

            <td width="54" align="center" valign="top"><strong>TO</strong></td>

            <td width="71" align="center" valign="top"><input name="end_date" type="text" size="14" id="end_date" value="END DATE"/></td>

            <td width="74" align="center" valign="top"><input name="end_time" type="text" size="8" id="end_time" value="TIME"/></td>

            <script>

                AnyTime.picker("end_date",

                {format: "%Y-%c-%e"});

                $("#end_time").AnyTime_picker(

                { format: "%H:%i", LabelTitle: "Time",

                labelHour: "Hour", labelMinute: "Minute" });

            </script>

            <td width="67" align="center" valign="top">&nbsp;</td>

            <td width="142" align="center" valign="top">&nbsp;</td>
          </tr>

          <tr>

            <td colspan="3" align="right" valign="top">Venue:</td>

            <td colspan="5" align="left" valign="top"><input type="text" name="venue" /></td>
          </tr>

          <tr>

            <td colspan="3" align="right" valign="top">Event Type:</td>

            <td colspan="5" align="left" valign="top"><input type="text" name="event_type" /></td>
          </tr>

          <tr>

            <td height="59" colspan="3" align="right" valign="top">Event Description:</td>

            <td colspan="5" align="left" valign="top"><textarea name="event_description"></textarea></td>
          </tr>

          <tr>

            <td height="51" colspan="3" align="right" valign="top">Event Program: </td>

            <td colspan="5" align="left" valign="top"><textarea name="event_program"></textarea></td>
          </tr>
          <tr>
            <td height="24" colspan="3" align="right" valign="top">Visible:</td>
            <td colspan="5" align="left" valign="top"><input name="visible" type="radio" value="0" />
            No &nbsp;
            <input name="visible" type="radio" value="1" />
            Yes</td>
          </tr>

          <tr>

            <td height="40" colspan="3" align="right" valign="top">Reminder:</td>

            <td align="left" valign="top">

              <input name="reminder_date" type="text" size="10" id="reminder_date" /> </td>

            <td align="left" valign="top">

              <input name="reminder_time" type="text" size="10" id="reminder_time"/></td>

            <script>

                AnyTime.picker("reminder_date",

                {format: "%Y-%c-%e"});

                $("#reminder_time").AnyTime_picker(

                { format: "%H:%i", LabelTitle: "Time",

                labelHour: "Hour", labelMinute: "Minute" });

            </script>             

            <td align="left" valign="top">&nbsp;</td>

            <td align="left" valign="top">&nbsp;</td>
          </tr>
          <tr>
            <td height="40" colspan="3" align="right" valign="top"><input type="submit" name="submit" value="Create Event" /></td>
            <td height="40" align="left" valign="top"> <input name="New" type="button" id="New" value="Cancel" onclick="location.href='event_management.php'"/></td>
          </tr>
        </table>

      </form> 
       </div>
  <?php include_layout_template('footer.php'); ?>

欢迎任何信息或建议。

1 个答案:

答案 0 :(得分:0)

我的建议是将此问题推送到您的数据库。例如,PostgreSQL对this blog post中记录的这类问题有很大的支持:

  

12月7日,汤姆·莱恩(Tom Lane)承诺杰夫戴维斯(Jeff Davis)补丁,增加了一般的排除限制:   日志消息:   添加排除约束,它将唯一性的概念概括为   支持任何可索引的可交换运算符,而不仅仅是相等。两排   如果“row1.col OP row2.col”为TRUE,则违反排除约束   约束中的每一列......这可用于确保给定的房间仅由1个用户在任何给定时间点保留。

以及方便的"Temporal" extension(请参阅here了解PgCon 2012的幻灯片,讨论其部分功能。)