注意:有关亚当·肖的Fullcalendar插件有很多问题;我无法使用现有讨论来解决此问题,但请指出一个相关线程。
使用php/mysql
我想在两种情况下显示一个日历:
- 当用户检查自己的信息时
- 当用户搜索另一个用户的信息时
案例1工作正常。案例2显示日历,而不是事件信息,尽管我认为使用了相同的逻辑。
每种情况都有一个数据库连接/查询文件,该文件以回声的JSON数组结束。这些文件都可以工作 - 我从两者中获得的输出都复制了下面:
案例1:
[{"title":"100","start":"2015-04-01"},{"title":"200","start":"2015-04-02"},{"title":"300","start":"2015-04-03"},{"title":"-2","start":"2014-04-04"},{"title":"-3","start":"2015-04-05"},{"title":"-1","start":"2015-04-04"},{"title":"-1","start":"2015-04-06"},{"title":"-300","start":"2015-04-07"},{"title":"-10","start":"2015-04-12"},{"title":"-10","start":"2015-04-13"},{"title":"-10","start":"2015-04-14"}]
案例2:
[{"title":"800","start":"2015-04-08"},{"title":"900","start":"2015-04-09"},{"title":"1000","start":"2015-04-10"},{"title":"5000","start":"2015-04-15"}]
每种情况也有自己的配置文件:
案例1 (当前工作):
<!--FullCalendar things -->
<link rel='stylesheet' href='fullcalendar/fullcalendar.css' />
<link rel='stylesheet' href='fullcalendar/fullcalendar.print.css' media="print" />
<script src='fullcalendar/lib/moment.min.js'></script>
<script src='fullcalendar/lib/jquery.min.js'></script>
<script src='fullcalendar/fullcalendar.js'></script>
<script>
$(document).ready(function() {
$('#calendar').fullCalendar({
weekNumbers: true,
allDayDefault: true,
editable: true,
eventLimit: true, // allow "more" link when too many events
events: 'db_connect.php'
});
});
</script>
案例2:
<!--FullCalendar things -->
<link rel='stylesheet' href='fullcalendar/fullcalendar.css' />
<link rel='stylesheet' href='fullcalendar/fullcalendar.print.css' media="print" />
<script src='fullcalendar/lib/moment.min.js'></script>
<script src='fullcalendar/lib/jquery.min.js'></script>
<script src='fullcalendar/fullcalendar.js'></script>
<script>
$(document).ready(function() {
$('#search_calendar').fullCalendar({
weekNumbers: true,
allDayDefault: true,
editable: true,
eventLimit: true, // allow "more" link when too many events
events: 'search_results_db_connect.php'
});
});
</script>
再次,在情况2中,日历本身出现,但没有事件。如果我从案例2中删除" Weeknumbers"选项,那么Wemnumbers从正确的页面中消失了,因此一切似乎都可以进行适当的交流。我已经通过了四倍体检查的拼写和文件结构,我很困惑。感谢任何帮助 - 谢谢!
编辑:我还应该注意,当我将案例2 JSON数组直接复制到配置文件中时,日历可工作。即:
<script>
$(document).ready(function() {
$('#search_calendar').fullCalendar({
weekNumbers: true,
allDayDefault: true,
editable: true,
eventLimit: true, // allow "more" link when too many events
events: //'search_results_db_connect.php'
[{"title":"800","start":"2015-04-08"},{"title":"900","start":"2015-04-09"},{"title":"1000","start":"2015-04-10"},{"title":"5000","start":"2015-04-15"}]
});
});
</script>
编辑2:search_results_db_connect.php (逻辑目前尚未涵盖所有情况。)
<?php
//Creates variable $step_connect = mysqli_connect($servername, $username, $password, $database);
include('/includes/step_dbc.php');
// Check connection
if (!$step_connect) {
die("Connection failed: " . mysqli_connect_error());
}
if (isset($_GET['query'])) {
$search_id = trim(htmlspecialchars($_GET['query']));
$q_user = "SELECT * FROM users WHERE first_name LIKE '%$search_id%'
OR last_name LIKE '%$search_id%' OR email LIKE '%$search_id%'";
$r_user = mysqli_query($step_connect, $q_user);
}
if (mysqli_num_rows($r_user) == 1) {
$row = mysqli_fetch_assoc($r_user);
$q_user_id = $row['id'];
$q_steps = "SELECT steps, `date` FROM steps WHERE user_id = '$q_user_id'";
$r_steps = mysqli_query($step_connect, $q_steps);
if (mysqli_num_rows($r_steps) >0) {
while ($row = mysqli_fetch_assoc($r_steps)) {
$step_array[] = array(
'title' => $row['steps'],
'start' => $row['date'],
);
}
} else {
echo "No recorded steps";
}
echo json_encode($step_array);
}
?>
上述代码输出上面显示的情况2 JSON。
您发布的代码似乎没有任何问题。我知道您说它有效,但是您可能想发布search_results_db_connect.php
代码。
最后,您可能需要对JSON数据检索进行更多调试。首先,您可以包含一个错误回调,以查看AJAX调用是否会导致错误响应。
$('#search_calendar').fullCalendar({
weekNumbers: true,
allDayDefault: true,
editable: true,
eventLimit: true, // allow "more" link when too many events
events: {
url: 'search_results_db_connect.php',
error: function(jqXHR jqXHR, String textStatus, String errorThrown) {
alert('Error: jqXHR.responseText: ' + jqXHR.responseText
+ 'textStatus: ' + textStatus
+ 'errorThrown: ' + errorThrown);
}
}
});
我认为您还可以添加success
回调,以查看JSON是否确实被返回。
events: {
url: 'search_results_db_connect.php',
error: function(jqXHR jqXHR, String textStatus, String errorThrown) {
//...
},
success: function(result) {
alert('type: ' + (typeof result) + 'result: ' + result);
}
}