基本上,我正在创建一个事件日历,其中在事件输入数据库的那一天生成了提交按钮。点击提交按钮后,它会打开一个新页面(events.php),显示当天活动的详细信息。
但是,我的问题是我不知道如何将日历(月份的日期)提交按钮的日期输入到从我的数据库获取数据的查询中。目前我只是硬编码了一个日期,看它是否有效。
如果我不清楚我很高兴再次讨论它,但它必须是动态的,所以它可以在任何一个月的任何一天工作
代码: Calendar_start.php(创建日历)
?php
$showmonth = $_POST['showmonth'];
$showyear = $_POST['showyear'];
$showmonth= preg_replace('#[^0-9]#i', '', $showmonth);
$showyear= preg_replace('#[^0-9]#i', '', $showyear);
$day_count = cal_days_in_month(CAL_GREGORIAN, $showmonth, $showyear);
$pre_days = date('w', mktime(0,0,0, $showmonth, 1, $showyear));
$post_days = (6-(date('w', mktime(0,0,0, $showmonth, $day_count, $showyear))));
echo '<div id="calendar_wrap">';
echo '<div class="title_bar">';
echo '<div class="previous_month"><input name="button" type="submit" value="Previous Month" onClick="javascript:last_month();"></div>';
echo '<div class="show_month">' . date('F', mktime(0, 0, 0, $showmonth)) . ' ' . $showyear . '</div>';
echo '<div class="next_month"><input name="button" type="submit" value="Next Month" onClick="javascript:next_month();"></div>';
echo '</div>';
echo '<div class="week_days">';
echo '<div class="days_of_the_week">Sun</div>';
echo '<div class="days_of_the_week">Mon</div>';
echo '<div class="days_of_the_week">Tues</div>';
echo '<div class="days_of_the_week">Wed</div>';
echo '<div class="days_of_the_week">Thur</div>';
echo '<div class="days_of_the_week">Fri</div>';
echo '<div class="days_of_the_week">Sat</div>';
echo '<div class="clear"></div>';
echo '</div>';
if ($pre_days != 0) {
for($i=1; $i<=$pre_days; $i++) {
echo '<div class="non_cal_day"></div>';
}
}
$con=mysql_connect("localhost","pytsuemg_brodie","brodie");
mysql_select_db("pytsuemg_brodie", $con);
for ($i=1; $i<= $day_count; $i++) {
$date = $i.'/'.$showmonth.'/'.$showyear;
//echo $date;
$query = "Select EventId FROM Events WHERE DateStart = '$date'";
$num_rows = 0;
// check if the query returns anything
$result = mysql_query($query,$con) or die(mysql_error());
if ($result)
{
$num_rows = mysql_num_rows($result);
}
echo '<div class="cal_day">';
echo '<div class="day_heading">' . $i . '</div>';
if($num_rows > 0)
{
echo "<input type=\"submit\"value=\"View Event\" onClick=\"window.open('events.php')\">";
}
echo '</div>';
} //end of for loop
if ($post_days !=0) {
for($i=1; $i<=$post_days; $i++) {
echo '<div class="non_cal_day"></div>';
}
}
echo '</div>';
?>
Events.php(从数据库中获取信息)
<?php
$con=mysqli_connect("localhost","pytsuemg_brodie","brodie","pytsuemg_brodie");
$query=("SELECT DateStart, description FROM Events WHERE DateStart = '2/2/2014'");
$num_rows = 0;
// check if the query returns anything
$result = mysqli_query($con,$query) or die(mysql_error());
if ($result)
{
$num_rows = mysqli_num_rows($result);
}
if($num_rows > 0) {
while ($newArray = mysqli_fetch_array($result)) {
$evdate = $newArray['DateStart'];
$description = $newArray['description'];
echo "The Date is $evdate and the description is $description <br>";
}
}
?>
show_calendar.php
<html>
<head>
<link href="calstyle.css" rel="stylesheet" type="text/css" media="all" />
<link href="styles.css" rel="stylesheet" type="text/css" media="all"/>
<script language="JavaScript" type="text/javascript">
function initialCalendar(){
var hr = new XMLHttpRequest();
var url = "calendar_start.php";
var currentTime = new Date ();
var month = currentTime.getMonth() + 1;
var year = currentTime.getFullYear();
showmonth = month;
showyear = year;
var vars= "showmonth="+showmonth+"&showyear="+showyear;
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if (hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("showCalendar").innerHTML = return_data;
}
}
hr.send(vars);
document.getElementById("showCalendar"). innerHTML = "processing...";
}
</script>
<script
<script language="JavaScript" type="text/javascript">
function next_month() {
var nextmonth = showmonth + 1;
if(nextmonth > 12) {
nextmonth = 1;
showyear = showyear+1;
}
showmonth = nextmonth;
var hr = new XMLHttpRequest();
var url = "calendar_start.php";
var vars= "showmonth="+showmonth+"&showyear="+showyear;
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if (hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("showCalendar").innerHTML = return_data;
}
}
hr.send(vars);
document.getElementById("showCalendar"). innerHTML = "processing...";
}
</script>
<script language="JavaScript" type="text/javascript">
function last_month() {
var lastmonth = showmonth - 1;
if(lastmonth < 1 ) {
lastmonth = 12;
showyear = showyear-1;
}
showmonth = lastmonth;
var hr = new XMLHttpRequest();
var url = "calendar_start.php";
var vars= "showmonth="+showmonth+"&showyear="+showyear;
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if (hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("showCalendar").innerHTML = return_data;
}
}
hr.send(vars);
document.getElementById("showCalendar"). innerHTML = "processing...";
}
</script>
</head>
<body onload="initialCalendar();">
<div id="showCalendar"> </div>
<div id="overlay">
<div id="events"></div>
</div>
</body>
</html>
答案 0 :(得分:0)
我不确定这是你想要的,但我认为就是这样:
<input type=\"button\"value=\"View Event\" onClick=\"window.open('events.php?date=".$date."')\">
然后在events.php
$date=mysqli_real_escape_string($con, $_GET['date']);
$query=("SELECT DateStart, description FROM Events WHERE DateStart = '$date'");