编辑:
我认为这可以在不重写表格以使您将结果分组在一起的情况下在代码中解决。目前您只有 time_group_id 作为某种分隔符。第一段和第二段代码将以这种方式对结果进行分组,但最后一段根本没有对它们进行分组。找到一个规则,您可以在表格中拆分结果,然后很容易根据需要显示它们。
第一的:
1) 停止使用 mysql_ 内置函数,它们将从较新版本的 PHP 中删除。
试试这样的脏程序代码:
$day_times = array();
$days_map = array(
'monday' => 'Mon',
'tuesday' => 'Tue',
'wednesday' => 'Wed',
'thursday' => 'Thu',
'friday' => 'Fri',
'saturday' => 'Sat',
'sunday' => 'Sun'
);
$query = mysql_query("SELECT distinct time_group_id, start_time, end_time
FROM times WHERE place_id = '80' ORDER BY id ASC");
while ($row=mysql_fetch_assoc($query)) {
$start_time = stripslashes($row['start_time']);
$end_time = stripslashes($row['end_time']);
$start_time = strtotime("$start_time");
$end_time = strtotime("$end_time");
$day = strtolower($row['day']);
$day_times[$row['time_group_id']][$day][] = $start_time;
$day_times[$row['time_group_id']][$day][] = $end_time;
}
这应该给你一个数组:
[time_group_id] => Array(
'monday' => Array(
'1123323444',
'4443543644',
'1123323444',
'2332133242',
'9782122333',
'3246556444'
),
'tuesday' => Array(
'1123323444',
'4443543644',
'1123323444',
'2332133242',
'9782122333',
'3246556444'
// more days and times
[time_group_id] => Array(
'monday' => Array(
'1123323444',
'4443543644',
'1123323444',
'2332133242',
'9782122333',
'3246556444'
),
'tuesday' => Array(
'1123323444',
'4443543644',
'1123323444',
'2332133242',
'9782122333',
'3246556444'
// more days and times
foreach ($day_times as $timegroup => $days) {
foreach ($days as $day => $times) {
echo $day .': ';
echo date('g:ia', min($times));
echo ' to ';
echo date('g:ia', max($times));
echo '<br />';
}
}
这将为您提供每一天的开始和结束时间。因为您希望它们按连续天数分组:
// within your query while loop
$day_times[$row['time_group_id']]['times'][] = $start_time
$day_times[$row['time_group_id']]['times'][] = $end_time;
$day_times[$row['time_group_id']]['days'][] = $day;
foreach ($day_times as $timegroup) {
$first_day = array_shift($timegroup['days']);
$last_day = array_pop($timegroup['days']);
$times = $timegroup['times'];
echo $days_map[$first_day] . ' - ' . $days_map[$last_day] . ': ';
echo date('g:ia', min($times));
echo ' to ';
echo date('g:ia', max($times));
echo '<br />';
}
这假设您按连续顺序提取日期,例如周一、周二、周三、周四。
更新:
这整个块应该是你所需要的:
$day_times = array();
$days_map = array(
'monday' => 'Mon',
'tuesday' => 'Tue',
'wednesday' => 'Wed',
'thursday' => 'Thu',
'friday' => 'Fri',
'saturday' => 'Sat',
'sunday' => 'Sun'
);
$query = mysql_query("SELECT distinct time_group_id, start_time, end_time
FROM times WHERE place_id = '80' ORDER BY id ASC");
while ($row=mysql_fetch_assoc($query)) {
$start_time = stripslashes($row['start_time']);
$end_time = stripslashes($row['end_time']);
$start_time = strtotime("$start_time");
$end_time = strtotime("$end_time");
$day = strtolower($row['day']);
$day_times[$row['time_group_id']]['times'][] = $start_time
$day_times[$row['time_group_id']]['times'][] = $end_time;
$day_times[$row['time_group_id']]['days'][] = $day;
}
foreach ($day_times as $timegroup) {
$first_day = array_shift($timegroup['days']);
$last_day = array_pop($timegroup['days']);
$times = $timegroup['times'];
echo $days_map[$first_day] . ' - ' . $days_map[$last_day] . ': ';
echo date('g:ia', min($times));
echo ' to ';
echo date('g:ia', max($times));
echo '<br />';
}
更新 2:
$day_times = array();
$days_map = array(
'monday' => 'Mon',
'tuesday' => 'Tue',
'wednesday' => 'Wed',
'thursday' => 'Thu',
'friday' => 'Fri',
'saturday' => 'Sat',
'sunday' => 'Sun'
);
$query = mysql_query("SELECT distinct time_group_id, start_time, end_time, day
FROM times WHERE place_id = '80' ORDER BY id ASC");
while ($row=mysql_fetch_assoc($query)) {
$start_time = stripslashes($row['start_time']);
$end_time = stripslashes($row['end_time']);
$start_time = strtotime("$start_time");
$end_time = strtotime("$end_time");
$day = strtolower($row['day']);
$day_times[] = $start_time
$day_times[] = $end_time;
$days[] = $day;
}
$first_day = array_shift($days);
$last_day = array_pop($days);
echo $days_map[$first_day] . ' - ' . $days_map[$last_day] . ': ';
echo date('g:ia', min($day_times));
echo ' to ';
echo date('g:ia', max($day_times));
echo '<br />';