Php 按月分离MySQL查询结果
形势 我有一个MySQL数据库表,其中包含许多日期 现在我需要从这个tableentries创建一个表,如下所示:Php 按月分离MySQL查询结果,php,mysql,arrays,Php,Mysql,Arrays,形势 我有一个MySQL数据库表,其中包含许多日期 现在我需要从这个tableentries创建一个表,如下所示: May | | 22.05.2014 | Person 1 | Person 2 23.05.2014 | Person 1 | Person 2 June | | 02.06.2014 | Person 1 | Person 2 25.06.2014 | Person 1 | Person 2 $entryarra
May | |
22.05.2014 | Person 1 | Person 2
23.05.2014 | Person 1 | Person 2
June | |
02.06.2014 | Person 1 | Person 2
25.06.2014 | Person 1 | Person 2
$entryarray = array();
$stmt="";//you statement here
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
{
$date = $row['pp_date'];
$month = date('m', strtotime($date));
$entryarray[$month][] =$row['data1'];//populate your array by the required data indexed by the month
$entryarray[$month][] =$row['data2'];
$entryarray[$month][] =$row['data3'];
}
foreach ($entryarray as $key => $value) {
print $key; //you will get the key of your array
foreach ($value as $key => $value){print $value; }//the value of the required key
}
等等
代码
我现在正试图通过我的所有查询结果来创建一个循环。就在那里我卡住了
我有这样的经历:
May | |
22.05.2014 | Person 1 | Person 2
23.05.2014 | Person 1 | Person 2
June | |
02.06.2014 | Person 1 | Person 2
25.06.2014 | Person 1 | Person 2
$entryarray = array();
$stmt="";//you statement here
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
{
$date = $row['pp_date'];
$month = date('m', strtotime($date));
$entryarray[$month][] =$row['data1'];//populate your array by the required data indexed by the month
$entryarray[$month][] =$row['data2'];
$entryarray[$month][] =$row['data3'];
}
foreach ($entryarray as $key => $value) {
print $key; //you will get the key of your array
foreach ($value as $key => $value){print $value; }//the value of the required key
}
在“while”(fetch assoc)循环中,我有以下内容:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$date = $row['pp_date'];
$month = date('m', strtotime($date));
$entryarray = array();
}
您可以看到,我首先将日期的月份保存到数组中。现在我不知道如何创建数组来保存数组中每个月的所有行,以便最终将结果输出到表中。我假设您的表报告只保存一年。如果是这样,您可以将月份用作数组上的索引键
$table = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$month = date('m', strtotime($row['pp_date']));
if(!isset($table[$month]))
{
$table[$month] = array();
}
$table[$month][] = $row; //... add a table row
}
现在,您可以通过迭代数组的键来回显$table
数组。它们将按创建顺序分组。这取决于您的SQL查询
foreach($table as $month=>$rows)
{
echo $month;
foreach($rows as $row)
{
print_r($row);
}
}
您可以尝试以下方法:
May | |
22.05.2014 | Person 1 | Person 2
23.05.2014 | Person 1 | Person 2
June | |
02.06.2014 | Person 1 | Person 2
25.06.2014 | Person 1 | Person 2
$entryarray = array();
$stmt="";//you statement here
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
{
$date = $row['pp_date'];
$month = date('m', strtotime($date));
$entryarray[$month][] =$row['data1'];//populate your array by the required data indexed by the month
$entryarray[$month][] =$row['data2'];
$entryarray[$month][] =$row['data3'];
}
foreach ($entryarray as $key => $value) {
print $key; //you will get the key of your array
foreach ($value as $key => $value){print $value; }//the value of the required key
}
然后,要获取数据,您可以执行以下操作:
May | |
22.05.2014 | Person 1 | Person 2
23.05.2014 | Person 1 | Person 2
June | |
02.06.2014 | Person 1 | Person 2
25.06.2014 | Person 1 | Person 2
$entryarray = array();
$stmt="";//you statement here
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
{
$date = $row['pp_date'];
$month = date('m', strtotime($date));
$entryarray[$month][] =$row['data1'];//populate your array by the required data indexed by the month
$entryarray[$month][] =$row['data2'];
$entryarray[$month][] =$row['data3'];
}
foreach ($entryarray as $key => $value) {
print $key; //you will get the key of your array
foreach ($value as $key => $value){print $value; }//the value of the required key
}
显示查询时,您可能需要按天(datecol)、月(datecol)、年(datecol)分组。为什么每个月都需要表?可以使用datepart在查询中直接选择特定月份。