一些帮助“同步"到服务器上.PHP中的两个循环 [英] Some help "syncing" two loops in PHP
问题描述
我已经实现了@Darragh的以下解决方案,以查找连续的日期并将其转换为日期范围:
I've implemented following solution by @Darragh to find consecutive dates and transforming them into date ranges:
现在,我正在尝试将一些其他信息打印到此循环的输出中.这是我根据@Darragh的答案创建的错误脚本:
Now, I'm trying to print some additional information to the output of this loop. This is the erroneous script I've created, based on @Darragh 's answer:
<?php
$output = '';
$dates = array();
$query = 'SELECT name, company, date FROM project GROUP BY date ORDER BY date';
$sth = $dbh->prepare($query);
$sth->execute();
if($sth->rowCount() > 0) {
$output .= '<ul>';
while($row = $sth->fetch()) {
array_push($dates,new DateTime($row['date']));
$name = $row['name'];
$company = $row['company'];
}
$lastDate = null;
$ranges = array();
$currentRange = array();
foreach ($dates as $date) {
if (null === $lastDate) {
$currentRange[] = $date;
} else {
$interval = $date->diff($lastDate);
if ($interval->days === 1) {
$currentRange[] = $date;
} else {
$ranges[] = $currentRange;
$currentRange = array($date);
}
}
$lastDate = $date;
}
$ranges[] = $currentRange;
foreach ($ranges as $range) {
$saverange = array();
foreach($range as $entry) {
array_push($saverange,$entry->format('Y-m-d'));
}
$startDate = array_shift($range);
$str = sprintf('%s', $startDate->format('d/m/Y'));
if (count($range)) {
$endDate = array_pop($range);
$str .= sprintf(' tot %s', $endDate->format('d/m/Y'));
}
$output .= '<li>'.$name.', '.$company.' - '.$str.'</li>';
}
$output .= '</ul>';
// Show me what you got
echo $output;
}
?>
很明显,遍历数据库输出的 while
循环与输出日期范围的 foreach
循环不同步.
Obviously, the while
loop that loops over the DB output is out of sync with the foreach
loop that outputs the dateranges.
原始DB输出如下:
+------------------+-------------+------------+
| name | company | date |
+------------------+-------------+------------+
| EBU | Belgacom sa | 2014-09-12 |
| Mosquito Replica | Mosquito nv | 2014-09-17 |
| Mosquito Replica | Mosquito nv | 2014-09-19 |
| Mosquito Replica | Mosquito nv | 2014-09-20 |
+------------------+-------------+------------+
我的脚本输出如下:
<ul>
<li>Mosquito Replica, Mosquito nv - 12/09/2014</li>
<li>Mosquito Replica, Mosquito nv - 17/09/2014</li>
<li>Mosquito Replica, Mosquito nv - 19/09/2014 tot 20/09/2014</li>
</ul>
我需要它输出的内容如下:
What I need it to output is as follows:
<ul>
<li>EBU, Belgacom sa - 12/09/2014</li>
<li>Mosquito Replica, Mosquito nv - 17/09/2014</li>
<li>Mosquito Replica, Mosquito nv - 19/09/2014 tot 20/09/2014</li>
</ul>
这对你们来说可能很明显,但是我无法一辈子弄清楚.谁来给我指路?提前致谢!
This might be obvious to you guys, but I cannot for the life of me figure it out. Who will show me the way? Thanks in advance!
推荐答案
您的脚本未打印 EBU,Belgacum sa
的原因在于:
The reason why your script is not printing EBU, Belgacum sa
lies in this part:
while($row = $sth->fetch()) {
array_push($dates,new DateTime($row['date']));
$name = $row['name'];
$company = $row['company'];
}
将日期值推入数组时, $ name
和 $ company
被每行替换.
While the date values are pushed to an array, $name
and $company
are overwritten by each new line.
此外,我认为您的脚本使问题变得更加复杂.这个怎么样?
Further, I think your script overcomplicates the issue. How about this?
$output = '';
$list = array();
$lastitem = $lastdate = null;
$query = 'SELECT name, company, date FROM project GROUP BY date ORDER BY date, name, company';
$sth = $dbh->prepare($query);
$sth->execute();
if($sth->rowCount() > 0) {
$i = 0;
while($row = $sth->fetch()) {
$date = new DateTime($row['date']);
$item = array(
'date' => array(),
'name' => $row['name'],
'company' => $row['company'],
);
if ($item === $lastitem && $date->diff($lastdate)->days === 1) {
$list[$i-1]['date']['end'] = $date;
}
else {
$list[$i] = $item;
$list[$i]['date']['start'] = $date;
$lastitem = $item;
$lastdate = $date;
$i++;
}
}
}
if (count($list)) {
$output .= '<ul>';
}
foreach ($list AS $item) {
$output .= '<li>' . $item['name'] . ', ' . $item['company'] . ' - ' . $item['date']['start']->format('d/m/Y');
$output .= isset($item['date']['end']) ? ' tot ' . $item['date']['end']->format('d/m/Y') : '';
$output .= '</li>';
}
if (count($list)) {
$output .= '</ul>';
}
这篇关于一些帮助“同步"到服务器上.PHP中的两个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!