首先是数据:
CREATE TABLE IF NOT EXISTS `calendar_events` (
`event_id` int(5) unsigned NOT NULL AUTO_INCREMENT,
`event_day` date DEFAULT NULL,
`event_day_to` date DEFAULT NULL,
`event_year` int(4) NOT NULL DEFAULT '0',
`event_time` varchar(7) NOT NULL DEFAULT '',
`event_time_to` varchar(7) DEFAULT NULL,
`event_title` varchar(200) NOT NULL DEFAULT '',
`event_desc` text NOT NULL,
`event_type` varchar(2) DEFAULT NULL,
PRIMARY KEY (`event_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
--
-- Dumping data for table `calendar_events`
--
INSERT INTO `calendar_events` (`event_id`, `event_day`, `event_day_to`, `event_year`, `event_time`, `event_time_to`, `event_title`, `event_desc`, `event_type`)
VALUES
(8, '2012-02-10', '2012-02-10', 0, '', NULL, 'Feb 10', '', NULL),
(5, '2012-02-21', '2012-02-23', 0, '', NULL, '21-23 range addition', '', NULL),
(4, '2012-02-15', '2012-02-18', 0, '', NULL, 'Febbb 18', 'Some stuff here as a description', NULL),
(7, '2012-02-12', '2012-02-12', 0, '', NULL, 'single date 12', '', NULL),
(9, '2012-02-28', '2012-02-28', 0, '', NULL, 'test 28Date', '', NULL),
(10, '2012-02-01', '2012-02-01', 0, '', NULL, 'feb1', '', NULL);
使值可用的查询:
mysql_select_db($database_MySQLconn, $MySQLconn);
$query_rsDates = "SELECT calendar_events.event_day, calendar_events.event_id, calendar_events.event_title, calendar_events.event_desc, calendar_events.event_day_to FROM calendar_events";
$rsDates = mysql_query($query_rsDates, $MySQLconn) or die(mysql_error());
$row_rsDates = mysql_fetch_assoc($rsDates);
$totalRows_rsDates = mysql_num_rows($rsDates);
接下来,代码将值设置为数组:
$dates = array();
while ($row_rsDates = mysql_fetch_assoc($rsDates))
{
for($start_date = strtotime($row_rsDates['event_day']);
$start_date <= strtotime($row_rsDates['event_day_to']);
$start_date = $start_date + 24*3600)
{
$row_rsDates['event_day'] = date("Y-n-j", $start_date);
$dates[] = $row_rsDates;
}
}
echo json_encode($dates);
结果JSON:
[{"event_day":"2012-2-21","event_id":"5","event_title":"21-23 range addition","event_desc":"","event_day_to":"2012-02-23"},{"event_day":"2012-2-22","event_id":"5","event_title":"21-23 range addition","event_desc":"","event_day_to":"2012-02-23"},{"event_day":"2012-2-23","event_id":"5","event_title":"21-23 range addition","event_desc":"","event_day_to":"2012-02-23"},{"event_day":"2012-2-15","event_id":"4","event_title":"Febbb 18","event_desc":"Some stuff here as a description","event_day_to":"2012-02-18"},{"event_day":"2012-2-16","event_id":"4","event_title":"Febbb 18","event_desc":"Some stuff here as a description","event_day_to":"2012-02-18"},{"event_day":"2012-2-17","event_id":"4","event_title":"Febbb 18","event_desc":"Some stuff here as a description","event_day_to":"2012-02-18"},{"event_day":"2012-2-18","event_id":"4","event_title":"Febbb 18","event_desc":"Some stuff here as a description","event_day_to":"2012-02-18"},{"event_day":"2012-2-12","event_id":"7","event_title":"single date 12","event_desc":"","event_day_to":"2012-02-12"},{"event_day":"2012-2-28","event_id":"9","event_title":"test 28Date","event_desc":"","event_day_to":"2012-02-28"},{"event_day":"2012-2-1","event_id":"10","event_title":"feb1","event_desc":"","event_day_to":"2012-02-01"}]
2月10日没有显示...... 我可以添加一个新的单个日期(相同的开始和结束日期),这些工作完美。我可以删除2月10日并重新插入它作为新记录,但它仍然无法显示...
现在让print_r为当前数组:
Array
(
[0] => Array
(
[event_day] => 2012-2-21
[event_id] => 5
[event_title] => 21-23 range addition
[event_desc] =>
[event_day_to] => 2012-02-23
)
[1] => Array
(
[event_day] => 2012-2-22
[event_id] => 5
[event_title] => 21-23 range addition
[event_desc] =>
[event_day_to] => 2012-02-23
)
[2] => Array
(
[event_day] => 2012-2-23
[event_id] => 5
[event_title] => 21-23 range addition
[event_desc] =>
[event_day_to] => 2012-02-23
)
[3] => Array
(
[event_day] => 2012-2-15
[event_id] => 4
[event_title] => Febbb 18
[event_desc] => Some stuff here as a description
[event_day_to] => 2012-02-18
)
[4] => Array
(
[event_day] => 2012-2-16
[event_id] => 4
[event_title] => Febbb 18
[event_desc] => Some stuff here as a description
[event_day_to] => 2012-02-18
)
[5] => Array
(
[event_day] => 2012-2-17
[event_id] => 4
[event_title] => Febbb 18
[event_desc] => Some stuff here as a description
[event_day_to] => 2012-02-18
)
[6] => Array
(
[event_day] => 2012-2-18
[event_id] => 4
[event_title] => Feb 18 single date
[event_desc] => Some stuff here as a description
[event_day_to] => 2012-02-18
)
[7] => Array
(
[event_day] => 2012-2-12
[event_id] => 7
[event_title] => single date 12
[event_desc] =>
[event_day_to] => 2012-02-12
)
[8] => Array
(
[event_day] => 2012-2-28
[event_id] => 9
[event_title] => test 28Date
[event_desc] =>
[event_day_to] => 2012-02-28
)
[9] => Array
(
[event_day] => 2012-2-1
[event_id] => 10
[event_title] => feb1
[event_desc] =>
[event_day_to] => 2012-02-01
)
)
2月的神秘十月无处可寻......
为什么这个日期不能像其他日期那样有效?还有其他单日期事件,例如2月1日到2月1日,所以这不是问题。并且,如果将2月10日日期再次添加为第二条记录(当然是新的event_id),则此新记录将毫无问题地添加到数组中。
为什么包含2月10日的第一条记录不会被处理到数组?
答案 0 :(得分:1)
致电
$row_rsDates = mysql_fetch_assoc($rsDates);
在while循环之前,删除第一行。只要放弃那条线就行了。
答案 1 :(得分:0)
原因很简单,
根据您的代码,
你已经调用了两次mysql_fetch_assoc,并且结果游标(在第一次调用之后)被提前到第二行,因此第二次调用从第二行开始,因此$ date不包含第一条记录的值
$row_rsDates = mysql_fetch_assoc($rsDates);
while ($row_rsDates = mysql_fetch_assoc($rsDates))
答案 2 :(得分:0)
你在while循环之前调用了一次mysql_fetch_assoc。因此,您在此处找到的记录集将不包含第一个结果。与2012-02-10日期的人。所以你应该消除/评论这些行:
$ row_rsDates = mysql_fetch_assoc($ rsDates);
$ totalRows_rsDates = mysql_num_rows($ rsDates);