我目前有一个看起来像这样的函数:
function getEvents($weekNumStart, $weekNumEnd){
$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
if (!$mysqli) {
die('There was a problem connecting to the database.');
}
else {
if ($weekNumEnd == '') {
$weekNumEnd = $weekNumStart;
}
$Group = $_SESSION['Group'];
$query = $mysqli->prepare("SELECT EventID, DAYOFWEEK(Start) AS wday, Events.Start, HOUR(Start) AS sHour, HOUR(End) AS eHour, Events.End, Events.Group, Events.Unit, Type, Room, Lecturer, Cancelled, StartName FROM Events, Week WHERE StartName >= '$weekNumStart' AND StartName <= '$weekNumEnd' AND Events.Start >= StartWeek AND Events.Start <=EndWeek AND (Events.Group = '$Group' OR Events.Group = '');");
$query->execute();
$query->bind_result($eventID, $dayofweek, $startDateTime, $startHour, $endHour, $endDateTime, $group, $unit, $type, $room, $lecturer, $cancelled, $weekName);
$data_arr = array();
while ($query->fetch()){
$data_arr[] = array(
$eventID, $dayofweek, $startDateTime, $startHour, $endHour, $endDateTime, $group, $unit, $type, $room, $lecturer, $cancelled, $weekName
);
}
return $data_arr; //
}
$mysqli->close();
}
此功能按我的意愿工作。我目前这样操作它:
if (!isset($weekNumEnd)){
$weekNumEnd = '';
}
$data = getEvents($weekNumStart, $weekNumEnd);
foreach($data as $day) {
for($i=0;$i<7;$i++){
for($j=9;$j<=18;$j++){
if ($day[1] == $i && $day[3] == $j){
$unit = $day[7];
$type = $day[8];
$room = $day[9];
if ($i == 2){
?>
<script>
var mon<?=$j?>unit = '<?=$unit?>';
var mon<?=$j?>type = '<?=$type?>';
var mon<?=$j?>room = '<?=$room?>';
$('#mon-<?=$j?>').append(
"<p>"+mon<?=$j?>unit+"<br>"+mon<?=$j?>type+"<br>"+mon<?=$j?>room+"</p>"
);
</script>
<?php
if (($day[4] - $day[3]) > 1) {
for($q=($day[3]+1);$q<$day[4];$q++){
?>
<script>
var mon<?=$q?>unit = '<?=$unit?>';
var mon<?=$q?>type = '<?=$type?>';
var mon<?=$q?>room = '<?=$room?>';
$('#mon-<?=$q?>').append(
"<p>"+mon<?=$q?>unit+"<br>"+mon<?=$q?>type+"<br>"+mon<?=$q?>room+"</p>"
);
</script>
<?php
}
}
}
问题是。我显然无法实时运行它以在每周或任何更改时不断更新事件列表。
所以为了解决这个问题,我决定尝试使用 Ajax。到目前为止,我有这个:
function getEvents() {
var data = 'Events=Yes';
$.ajax({
type: "POST",
url: "functions/updateWeek.php",
data: data,
cache: false,
dataType: 'json',
success: function(html) {
alert(html.returned_val);
}
});
}
我有这个要返回:
if($_POST['Events'] == "Yes"){
$weekNumStart = $_SESSION['startWeek'];
$weekNumEnd = $_SESSION['endWeek'];
$data = getEvents($weekNumStart, $weekNumEnd);
echo json_encode(array('returned_val' => $data));
}
这......有点作品。至少也就是说它以一个丑陋的大块返回数据。
问题是我不能再像上面那样操作它:
data = getEvents($weekNumStart, $weekNumEnd);
foreach($data as $day) {
for($i=0;$i<7;$i++){
for($j=9;$j<=18;$j++){
if ($day[1] == $i && $day[3] == $j){
$unit = $day[7];
$type = $day[8];
$room = $day[9];
if ($i == 2){
?>
<script>
var mon<?=$j?>unit = '<?=$unit?>';
var mon<?=$j?>type = '<?=$type?>';
var mon<?=$j?>room = '<?=$room?>';
$('#mon-<?=$j?>').append(
"<p>"+mon<?=$j?>unit+"<br>"+mon<?=$j?>type+"<br>"+mon<?=$j?>room+"</p>"
);
</script>
也就是说-当我通过ajax调用信息时,我不知道如何重新操作它。我会以完全 javascript 的方式重写以上所有内容吗?有没有我不知道/理解的更合乎逻辑的方法?或者有没有一种方法可以让代码在那个不同的窗口中工作(用php和javascript完成),并让它全部发回,通过ajax更正,自动在页面上工作,而不必在信息后重写它是通过ajax拉回来的?