編集:
結果をグループ化できるようにテーブルを書き直さなくても、これをコードで解決できるとは思いません。現在、ある種の区切り文字としてtime_group_idしかありません。最初と2番目のコードはそのように結果をグループ化しますが、最後のコードは結果をまったくグループ化しません。テーブル内で結果を分割できるルールを見つけてください。そうすれば、必要に応じて分割して表示するのが簡単になります。
最初:
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 />';
}
これは、日を連続した順序で引き出すことを前提としています。たとえば、mon、tue、wed、thuです。
アップデート:
このブロック全体が必要なすべてである必要があります。
$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 />';