日历未显示正确的成员事件

时间:2013-10-07 22:13:29

标签: javascript php ajax mysqli

我有一个包含多个成员的网站,如果他们拥有自己的日历我想要每个人。 我发现了一个构建教程和事件日历巫婆工作正常,但我无法在正确的日历上显示事件,要么每个人都可以看到数据库中的所有事件或没有人可以看到任何事件。

我的代码如下

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="calender_wrap">';

echo '<div class="title_bar">';
echo '<div class="previouse_month"><input name="myBtn" type="submit" value="Previouse Month" onclick="javascript:last_month();"></div';
echo '<div class="show_month">' . $showmonth . '/' . $showyear . '</div>';
echo '<div class="next_month"><input name="myBtn" type="submit" value="Next Month" onclick="javascript:next_month();"></div>';
echo '</div>';

echo '<div class="week_days">';
echo '<div class="days_of_week">Sunday</div>';
echo '<div class="days_of_week">Monday</div>';
echo '<div class="days_of_week">Tuesday</div>';
echo '<div class="days_of_week">Wednesday</div>';
echo '<div class="days_of_week">Thursday</div>';
echo '<div class="days_of_week">Friday</div>';
echo '<div class="days_of_week">saturday</div>';
echo '<div class="clear"></div>';
echo '</div>';

/* Previouse Month filler Days */
if ($pre_days != 0) {
    for($i = 1; $i<=$pre_days; $i++) {
        echo '<div class="non_cal_day"></div>';
    }
}

/*Current Month filler Days */
include("scripts/connect_to_mysql.php");
for($i = 1; $i<= $day_count; $i++) {
    // get event logic

    $date = $i . '/' . $showmonth . '/' . $showyear;
    $query = "SELECT * FROM occurrences WHERE cid ='$squadid' AND start_date ='$date' ";
    $cal = mysqli_query($db_conx, $query);
    $num_rows = mysqli_num_rows($cal);
        if($num_rows > 0) {
            $event= '<a href="view_events.php"><h3 class="event_here">Events Here</h3></a>';
            }
        //end get event logic

    echo '<div class="cal_day">';
    echo '<div class="day_heading">' . $i . ' <a href="add_event.php"><div class="add_event"></div></a></div><br/>';
    if($num_rows != 0) { echo $event;}
    echo '</div>';
}

/* Next month filler days */
if ($post_days != 0) {
    for($i = 1; $i<=$post_days; $i++) {
        echo '<div class="non_cal_day"></div>';
    }
}

echo '</div>';

?>

show_calendar.php

<?php 
// Start_session, check if user is logged in or not, and connect to the database all in one included file
include_once("scripts/checkuserlog.php");
// Include the class files for auto making links out of full URLs and for Time Ago date formatting
include_once("wi_class_files/autoMakeLinks.php");
include_once ("wi_class_files/agoTimeFormat.php");
// Create the two new objects before we can use them below in this script
$activeLinkObject = new autoActiveLink;
$myObject = new convertToAgo;

// ------- ESTABLISH THE PAGE ID ACCORDING TO CONDITIONS ---------
$squadid = "";
if (isset($_GET['pid'])){ $squadid = $_GET['pid'];};
// ------- END ESTABLISH THE PAGE ID ACCORDING TO CONDITIONS ---------

//-------- GET EVENTS FROM TABLE-------------------------------------

?>

<!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=UTF-8" />
<title>Calender</title>
<link href="style/calCss.css" rel="stylesheet" type="text/css" media="all" />
<link href="style/main.css" rel="stylesheet" type="text/css" />
<link rel="icon" href="logoicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="logoicon.ico" type="image/x-icon" />
<script language="javascript" type="text/javascript">
function initialCalendar() {
    var hr = new XMLHttpRequest();
    var url = "calender_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 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 = "calender_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 = "calender_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();">
<?php include_once "header_template.php"; ?>
<table class="mainBodyTable" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="740" valign="top"><div><?php include_once "leaderBoardAd.php"; ?></div><br />
      <table width="90%" align="center" cellpadding="6">
        <tr>
          <td>
          <div id="showCalendar"></div>
          </td>
        </tr>
      </table></td>
    <td width="160" valign="top"><?php include_once "right_AD_template.php"; ?></td>
  </tr>
</table>
<?php include_once "footer_template.php"; ?>

</body>
</html>

目前,如果我从calendar_start.php中的cid ='$squadid'删除$query,您可以看到数据库中的所有事件,但网站上的每个成员也可以看到它们。

在我的表格中,cid应代表日历ID,即。成员拥有日历,但我无法解决我出错的地方。

我希望我已经解释得这么好,但如果你需要更多,请告诉我。

非常感谢

1 个答案:

答案 0 :(得分:0)

在你的calendar_start.php(即你的ajax请求所牵制的文件)中,var $ squadid为空。你需要从show_calendar.php发送$ squadid到calendar_start.php抛出你的ajax请求。

以下是一个例子:

  //Ajax request example from show_calendar.php
    var hr = new XMLHttpRequest();
    var url = "calender_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+"&squadid=<?= $squadid; ?>";
    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...";


    //Get the var in calendar_start.php add this line
    $squadid = $_POST['squadid'];

希望这有助于你m8。 干杯!