我试图显示 jquery 日历上特定日期的记录总数,如下所示:
例如,6月1日在日历上显示为6,这意味着mysql表中有6条记录为6月1日
这就是我所拥有的
索引.php
<div id='calendar'></div>
<script>
$(function() { // document ready
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: false,
events: 'test.php',
eventSources: [
// your event source
{
events: [ // put the array in the `events` property
{
title : 'Happy Thanksgiving',
start : '2016-11-25',
end : '2016-11-25',
imageurl:'img/holiday-icon.png'
},
],
color: 'transparent', // an option!
textColor: 'black' // an option!
}
// any other event sources...
],
eventRender: function(event, eventElement) {
if (event.imageurl) {
eventElement.find("div.fc-content").prepend("<img src='" + event.imageurl +"' width='15' height='15'>");
}
},
});
});
</script>
测试.php
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$username = "root_jdc";
$password = "password";
$hostname = "localhost";
$database = "table_customers";
$conn = mysql_connect($hostname, $username, $password)
or die("Connecting to MySQL failed");
mysql_close($conn);
if ($conn) {
if (isset($_GET["end"])) {
//this is calendar query.
//form an array of events
$arr = array();
$from_date = htmlspecialchars($_GET["end"]);
$to_date = htmlspecialchars($_GET["end"]);
$sql_string = "SELECT count(DISTINCT id) as title, date as ShipDate FROM players3 group by date";
$result = mysql_fetch_assoc($sql_string);
//odbc_result_all($result);
// Fetch rows:
while(mysql_fetch_array($result))
{
if (!mysql_result($result,2)) continue;
//collect results
$title=mysql_result($result,1);
$date=mysql_result($result,2);
$arr[] = array(
'title' => $title,
'url' => 'myfeed.php?dt='.substr($date, 0, 10),
'start' => substr($date, 0, 10)
);
}
echo json_encode($arr);
}
}
?>
最佳答案
首先,您应该知道 DATE 是 MySQL 中的关键字,也可能是其他数据库中的关键字,您最好避免在列定义中使用关键字,因为它可能会导致不必要的麻烦,或者至少使用反引号 (`)。
您需要做的是将记录分组,如下所示:
SELECT
COUNT(id),
YEAR(`date`),
MONTH(`date`),
DAY(`date`)
FROM players3
GROUP BY
YEAR(`date`),
MONTH(`date`),
DAY(`date`);
这样您就可以获得每个日期的记录数。
至 mod:可能与 MySQL Query GROUP BY day / month / year 重复
关于PHP/MySQL - 显示 jquery 日历上每个日期的记录总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37471199/