PHP日历:事件似乎重复了一遍

时间:2014-07-08 16:39:00

标签: php

我正在忙于编写一个网站,您可以从存储在数据库中的事件列表中将事件添加到日历中,然后日历将显示该事件的持续天数事件。< / p>

所以例如我有15天的“网球比赛”(在数据库中指定),然后我所做的只是点击当天并选择活动,它意味着显示当天的活动和接下来的15天。

如果有人可以帮我解决接下来的15天事情,因为我已经不知道如何做到这一点,如果我将事件添加到日历中只是为了添加它15次,或者添加一次,让日历完成工作。

我遇到的另一个问题是,如果我为第14个添加一个事件而在第15个事件中添加另一个事件,则这两个事件将出现在15和其他月份的事件中。

这是我的代码,如果被问到,我会更具体地说明某些事情:

event.php:

<!DOCTYPE html>
<html>
<head>
        <title>Nkoka Courses: Admin Panel - Add Calendar Event</title>
        <a href="../style.css" rel= "stylesheet" type="text/css"></a>
</head>

<body>
<h1>Show/Add Courses:</h1>
<?php
$mysql = mysql_connect('localhost', 'root', '');
        if (!$mysql){
            die("Database Connection Failed" . mysql_error());
        }
        $select_db = mysql_select_db('nkoka');
        if (!$select_db){
            die("Database Selection Failed" . mysql_error());
        }

//add any new event
if ($_POST) {

    //create database-safe strings
    $m = $_POST['m'];
    $d = $_POST['d'];
    $y = $_POST['y'];
    $safe_event = mysql_real_escape_string($_POST['event']);

    $startdate = $y."-".$m."-".$d;

    $insEvent_sql = "INSERT INTO calendar (event, startdate) VALUES('".$safe_event."', '".$startdate."')";
    $insEvent_res = mysql_query($insEvent_sql) or die(mysql_error($mysql));

} else {

    //create database-safe strings
    $m = mysql_real_escape_string($_GET['m']);
    $d = mysql_real_escape_string($_GET['d']);
    $y = mysql_real_escape_string($_GET['y']);
}

//show events for this day
$getEvent_sql = "SELECT event, date_format(startdate, '%l:%i %p') as fmt_date FROM calendar WHERE month(startdate) = '".$m."' AND dayofmonth(startdate) = '".$d."' AND year(startdate) = '".$y."' ORDER BY startdate";
$getEvent_res = mysql_query($getEvent_sql) or die(mysql_error($mysql));

if (mysql_num_rows($getEvent_res) > 0) {
    $event_txt = "<ul>";
    while ($ev = @mysql_fetch_array($getEvent_res)) {
        $event = stripslashes($ev['event']);
        $fmt_date = $ev['fmt_date'];

        $event_txt .= "<li>".$event."</li>";
    }
    $event_txt .= "</ul>";
    mysql_free_result($getEvent_res);
} else {
    $event_txt = "";
}

// close connection to MySQL
mysql_close();

if ($event_txt != "") {
    echo "<p><strong>Courses:</strong></p>
    $event_txt
    <hr/>";
}

// show form for adding an event

    require('../connect.php');
    $sql = "SELECT * FROM `course`";
    $result = mysql_query($sql) or die(mysql_error());
    $count = mysql_num_rows($result);

?>

<form method="post" action="event.php">

            <p><label>Select Course: </label>
            <select name="event" id="event" style="position: relative;left: 95px; width: 175px;">
            <?php 
            while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
            ?> 
            <option value="<?php echo $line['name'];?>"> <?php echo $line['name'];?> </option>
            <?php }
            ?>
            </select></p>

            <input type="hidden" name="m" value="<?php echo $m; ?>"></input>
            <input type="hidden" name="d" value="<?php echo $d; ?>"></input>
            <input type="hidden" name="y" value="<?php echo $y; ?>"></input>

<button type="submit" name="submit" value="submit">Add Course</button>
</form>

</body>
</html>
<?php
// close connection to MySQL
mysql_close();
?>

calendar.php

<?php
define("ADAY", (60*60*24));
if ((!isset($_POST['month'])) || (!isset($_POST['year']))) {
    $nowArray = getdate();
    $month = $nowArray['mon'];
    $year = $nowArray['year'];
} else {
    $month = $_POST['month'];
    $year = $_POST['year'];
}

$start = mktime (12, 0, 0, $month, 1, $year);
$firstDayArray = getdate($start);
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo "Calendar: ".$firstDayArray['month']." ".$firstDayArray['year']; ?></title>
<style type="text/css">
    table {
        border: 1px solid black;
        border-collapse: collapse;
    }
    th {
        border: 1px solid black;
        padding: 6px;
        font-weight: bold;
        background: #ccc;
    }
    td {
        border: 1px solid black;
        padding: 6px;
        vertical-align: top;
        width: 100px;
    }
</style>
<script type="text/javascript">
function eventWindow(url) {
    event_popupWin = window.open(url, 'event', 'resizable=yes, scrollbars=yes, toolbar=no,width=400,height=150');
    event_popupWin.opener = self;
}
</script>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="month">
<?php
$months = Array("January", "February", "March", "April", "May",  "June", "July", "August", "September", "October", "November", "December");
for ($x=1; $x <= count($months); $x++) {
    echo"<option value=\"$x\"";
    if ($x == $month) {
        echo " selected";
    }
    echo ">".$months[$x-1]."</option>";
}
?>
</select>
<select name="year">
<?php
for ($x=2014; $x<=2024; $x++) {
    echo "<option";
    if ($x == $year) {
        echo " selected";
    }
    echo ">$x</option>";
}
?>
</select>
<button type="submit" name="submit" value="submit">Go!</button>
</form>
<br/>
<?php
$days = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
echo "<table><tr>\n";
foreach ($days as $day) {
    echo "<th>".$day."</th>\n";
}
for ($count=0; $count < (6*7); $count++) {
    $dayArray = getdate($start);
    if (($count % 7) == 0) {
        if ($dayArray['mon'] != $month) {
            break;
        } else {
            echo "</tr><tr>\n";
        }
    }
    if ($count < $firstDayArray['wday'] || $dayArray['mon'] != $month) {
        echo "<td>&nbsp;</td>\n";
    } else {

        require('../connect.php');
         $chkEvent_sql = "SELECT event FROM calendar WHERE
                          month(startdate) = '".$month."' AND
                          dayofmonth(startdate) = '".$dayArray['mday']."'
                          AND year(startdate) = '".$year."' ORDER BY startdate";
         $chkEvent_res = mysql_query($chkEvent_sql)
                         or die(mysql_error($mysql));

         if (mysql_num_rows($chkEvent_res) > 0) {
              while ($ev = mysql_fetch_array($chkEvent_res)) {
                   $event .= stripslashes($ev['event'])."<br/>";
              }
         } else {
              $event = "";
         }

        echo "<td><a href=\"javascript:eventWindow('event.php?m=".$month.
         "&amp;d=".$dayArray['mday']."&amp;y=$year');\">".$dayArray['mday']."</a>
         <br/><br/>".$event."</td>\n";
         $start += ADAY;
    }
}
echo "</tr></table>";

//close connection to MySQL
mysql_close();
?>
</body>
</html>

任何帮助将不胜感激, 感谢

0 个答案:

没有答案