我的页面“From”和“To”中有2个datepicker。用户将为每个值选择日期:
FromDate: 01/JUL/2012 ToDate: 31/OCT/2014
这是我的疑问:
$query = mssql_query("SELECT count(startdate) as start
FROM user
WHERE startdate between '01 JUL 2012' and '31 OCT 2014'
GROUP BY month(startdate), year(startdate)
ORDER BY month(startdate) ASC, year(startdate) ASC");
我想计算从用户选择的日期开始的所有记录,按年和月分组并将其存储在php数组中。
示例结果:
JUL AUG SEP OCT NOV DEC
Array2012 = ("52","45","25","62","11","41")
JAN FEB MAR DEC
Array2013 = ("52","45","25",......,"35")
JAN FEB MAR OCT
Array2014 = ("52","45","25",.......,"47")
如何循环数据库中的值以便获得这些结果?
答案 0 :(得分:0)
尝试使用类似代码段的内容来循环遍历db query中的返回值:
$query = mssql_query("SELECT count(startdate) as start
FROM user
WHERE startdate between '01 JUL 2012' and '31 OCT 2014'
GROUP BY month(startdate), year(startdate)
ORDER BY month(startdate) ASC, year(startdate) ASC");
if($query == FALSE){
die(mysql_error());
}
while($row = mysql_fetch_array($query)){
echo $row['startdate']
}
希望我理解正确,并且我可以提供帮助。
和平
答案 1 :(得分:0)
你需要改变一些事情:
结果,代码如下所示:
$values = array();
$lastYear = 0;
$query = mssql_query("SELECT count(startdate) as start,
year(startdate) AS year
FROM user
WHERE startdate between '01 JUL 2012' and '31 OCT 2014'
GROUP BY month(startdate), year(startdate)
ORDER BY year(startdate) ASC, month(startdate) ASC");
while ($data = mssql_fetch_array($query)) {
if ($data['year'] != $lastYear) {
$lastYear = $data['year'];
$values[$data['year']] = array();
}
array_push($values[$data['year']], $data['start']);
}
print_r($values);
$ values将包含:
Array(
[2012] => Array("52","45","25","62","11","41"}),
[2013] => Array("52","45","25",......,"35"),
[2014] => Array("52","45","25",.......,"47")
)