include5.php(数据库连接)
<?php
/**
* Created by PhpStorm.
*/
function doDB()
{
global $conn;
$conn = mysqli_connect('localhost','root','','php_project01');
if(mysqli_connect_errno())
{
echo "数据库连接失败!".mysqli_connect_error()."<br>";
exit;
}
}
dateselector.php(日历)
<?php
/**
* Created by PhpStorm.
*/
define("ADAY",(60*60*24));
include ('include5.php');
doDB();
if((!isset($_POST['month'])) || (!isset($_POST['year'])))
{
$now_array = getdate();
$month = $now_array['mon'];
$year = $now_array['year'];
}
else
{
$month = $_POST['month'];
$year = $_POST['year'];
}
$start = mktime(1,0,0,$month,1,$year);
$firstDay_array = getdate($start);
?>
<html>
<head>
<title><?php echo "日历:".$firstDay_array['month']." ".$firstDay_array['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-color: #cccccc;
}
td
{
border: 1px solid black;
padding: 6px;
vertical-align: top;
width: 120px;
}
</style>
<script type="text/javascript">
function eventWindow(url)
{
event_popupWin = window.open(url,'event','resizable-yse,scrollbars-yes,toolbar=no,width=500,height=500');
event_popupWin.opener = self;
}
</script>
</head>
<body>
<h1>选择年/月的组合方式</h1>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<span>
<select name="year" style="width: 100px;height: 24px;">
<?php
for($x=1990; $x<=2020; $x++)
{
echo "<option value='$x'";
if($x == $year)
{
echo " selected='selected' ";
}
echo ">$x</option>";
}
?>
</select>
<select name="month" style="width: 100px;height: 24px;">
<?php
$months = array('一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月');
for($i=1; $i<=count($months); $i++)
{
echo "<option value='$i'";
if($i == $month)
{
echo " selected='selected'";
}
echo ">".$months[$i-1]."</option>";
}
?>
</select>
<button type="submit" name="submit" value="submit" style="width: 80px; height: 24px;">确定</button>
</span>
</form>
<br>
<?php
$days = array("周日","周一","周二","周三","周四","周五","周六");
echo "<table><tr>";
foreach($days as $day)
{
echo "<th>$day</th>";
}
for($count=0; $count<(6 * 7); $count++)
{
$day_array = getdate($start);
if(($count % 7) == 0)
{
if($day_array['mon'] != $month)
{
break;
}
else
{
echo "</tr><tr>";
}
}
if($count < $firstDay_array['wday'] || $day_array['mon'] != $month)
{
echo "<td> </td>";
}
else
{
$chk_event_sql = "select event_title from calendar_events where month(event_start)='$month' and
dayofmonth(event_start)='".$day_array['mday']."' and year(event_start) = '$year' order by event_start";
$chk_event_res = mysqli_query($conn,$chk_event_sql) or die(mysqli_error($conn));
$event_title = "";
if(mysqli_num_rows($chk_event_res) > 0)
{
while ($ev = mysqli_fetch_array($chk_event_res))
{
$event_title .= stripslashes($ev['event_title'])."<br>";
}
}
else
{
$event_title = "";
}
echo "<td><a href=\"javascript:eventWindow('event.php?m=$month&d=$day_array[mday]&y=$year');\">
$day_array[mday] </a><br><br>$event_title</td>";
$start += ADAY;
}
}
echo "</tr></<table>";
?>
</body>
</html>
event.php(日历事件)
<html>
<head>
<title>显示或增加事件</title>
</head>
<body>
<h1>显示或增加事件</h1>
<?php
include ('include5.php');
doDB();
if($_POST)
{
$m = mysqli_real_escape_string($conn,$_POST['m']);
$d = mysqli_real_escape_string($conn,$_POST['d']);
$y = mysqli_real_escape_string($conn,$_POST['y']);
$event_title = mysqli_real_escape_string($conn,$_POST['event_title']);
$event_shortdesc = mysqli_real_escape_string($conn,$_POST['event_shortdesc']);
$event_time_hh = mysqli_real_escape_string($conn,$_POST['event_time_hh']);
$event_time_mm = mysqli_real_escape_string($conn,$_POST['event_time_mm']);
$event_date = "$y-$m-$d $event_time_hh:$event_time_mm:00";
$add_event_sql = "insert into calendar_events(event_title,event_shortdesc,event_start)
values('$event_title','$event_shortdesc','$event_date')";
$add_event_res = mysqli_query($conn,$add_event_sql) or die(mysqli_error($conn));
}
else
{
$m = mysqli_real_escape_string($conn,$_GET['m']);
$d = mysqli_real_escape_string($conn,$_GET['d']);
$y = mysqli_real_escape_string($conn,$_GET['y']);
}
$get_event_sql = "select event_title,event_shortdesc,date_format(event_start,'%l:%i %p') as fmt_date from
calendar_events where month(event_start)='$m' and dayofmonth(event_start)='$d' and year(event_start)='$y'
order by event_start";
$get_event_res = mysqli_query($conn,$get_event_sql) or die(mysqli_error($conn));
if(mysqli_num_rows($get_event_res) > 0)
{
$event_txt = "<ul>";
while($ev = mysqli_fetch_array($get_event_res))
{
$event_title = stripslashes($ev['event_title']);
$event_shortdesc = stripslashes($ev['event_shortdesc']);
$fmt_date = stripslashes($ev['fmt_date']);
$event_txt .="<li><strong>$fmt_date</strong>:$event_title<br>$event_shortdesc</li>";
}
$event_txt .="</ul>";
mysqli_free_result($get_event_res);
}
else
{
$event_txt = "";
}
mysqli_close($conn);
if($event_txt != "")
{
echo "<p><strong>今日的事件:</strong></p> $event_txt <hr>";
}
echo <<< END_T
<form method="post" action="$_SERVER[PHP_SELF]">
<p><strong>是否需要增加事件?</strong><br>
填写以下内容并单击提交按钮。</p>
<p><label for="event_title">事件标题:</label>
<input type="text" id="event_title" name="event_title" size="25" maxlength="25"></p>
<p><label for="event_shortdesc">事件简介:</label>
<input type="text" id="event_shortdesc" name="event_shortdesc" size="25" maxlength="255"></p>
<fieldset>
<legend>事件时间(hh:mm)</legend>
<select name="event_time_hh">
END_T;
for($x=1; $x<24; $x++)
{
echo "<option value='$x'>$x</option>";
}
echo <<< END_OF_T
</select>:
<select name="event_time_mm">
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
</select>
</fieldset>
<input type="hidden" name="m" value="$m">
<input type="hidden" name="d" value="$d">
<input type="hidden" name="y" value="$y">
<button type="submit" name="submit" value="submit">提交</button>
</from>
END_OF_T;
?>
</body>
</html>
数据库文件
-- phpMyAdmin SQL Dump
-- version 4.8.4
-- https://www.phpmyadmin.net/
--
-- 主机: 127.0.0.1
-- 生成日期: 2019-06-05
-- 服务器版本: 10.1.37-MariaDB
-- PHP 版本: 7.3.1
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- 数据库: `php_project01`
--
-- --------------------------------------------------------
--
-- 表的结构 `calendar_events`
--
CREATE TABLE `calendar_events` (
`id` int(11) NOT NULL,
`event_title` varchar(25) DEFAULT NULL,
`event_shortdesc` varchar(25) DEFAULT NULL,
`event_start` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- 转存表中的数据 `calendar_events`
--
INSERT INTO `calendar_events` (`id`, `event_title`, `event_shortdesc`, `event_start`) VALUES
(1, '去动物园', '今天要陪小公主去动物园看狮子', '2019-06-01 00:00:00'),
(20, '肯德基', '带孩子去吃肯德基', '2019-06-01 17:00:00');
--
-- 转储表的索引
--
--
-- 表的索引 `calendar_events`
--
ALTER TABLE `calendar_events`
ADD PRIMARY KEY (`id`);
--
-- 在导出的表使用AUTO_INCREMENT
--
--
-- 使用表AUTO_INCREMENT `calendar_events`
--
ALTER TABLE `calendar_events`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=21;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;