[英]PHP: Insert database value into different php array
I have 2 datepicker in my page "From" and "To". 我的页面“发件人”和“发件人”中有2个日期选择器。 User will select date for each value: 用户将为每个值选择日期:
FromDate: 01/JUL/2012 ToDate: 31/OCT/2014
And this is my 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");
I want to count all records that range from the user selected date, group it by year and month and store it in php array. 我想计算从用户选择日期开始的所有记录,将其按年和月分组,并将其存储在php数组中。
Example Result: 结果示例:
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")
How can I loop the values from database so I can get those results? 我如何从数据库中循环值,以便获得这些结果?
Try using something like this code snippet to loop through the return values from the db query: 尝试使用类似以下代码片段的代码循环遍历db查询的返回值:
$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']
}
Hope I understood you correctly and that I could help. 希望我能正确理解您,希望能对您有所帮助。
Peace 和平
You need to change a couple of things : 您需要更改几件事:
In result, code will look like this : 结果,代码将如下所示:
$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 will contain : $ values将包含:
Array(
[2012] => Array("52","45","25","62","11","41"}),
[2013] => Array("52","45","25",......,"35"),
[2014] => Array("52","45","25",.......,"47")
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.