需要一些幫助,一直在這個問題上好幾年,但似乎無法解決它。我在一個名爲「Term Year PrNo」的字段中有一些數據。在這一領域中的數據如下圖所示:在一個循環內需要幫助來分組數據
[2010-201110]Winter - 2010 - 1st
[2010-201111]Winter - 2010 - 2nd
[2010-201120]Spring - 2011 - 1st
[2010-201121]Spring - 2011 - 2nd
[2010-201130]Summer - 2011 - 1st
[2010-201131]Summer - 2011 - 2nd
[2011-201210]Winter - 2011 - 1st
[2011-201211]Winter - 2011 - 2nd
[2011-201220]Spring - 2012 - 1st
[2011-201221]Spring - 2012 - 2nd
[2011-2]Summer - 2012 - 1st
[2011-2]Summer - 2012 - 2nd
[2012-201310]Winter - 2012 - 1st
[2012-201311]Winter - 2012 - 2nd
[2012-201320]Spring - 2013 - 1st
[2012-201321]Spring - 2013 - 2nd
[2012-201330]Summer - 2013 - 1st
我需要做的每一行數據在該領域的一個單選按鈕選擇,我已經成功地做到通過粘貼的字段的內容和隨地吐痰出來作爲輸入字段。我也使用正則表達式來清理輸入值的外觀 - 即移除第一個排序字段。到現在爲止還挺好。我似乎無法解決的問題是我需要分段細分數據,並根據年份將其包裝在一個名爲yearblock的div類中,2010-2011 | 2011-2012 | 2012-2013,所以我可以使用CSS很好地安排它們。我已經設法弄清楚如何開始div類,通過比較去年到今年,但我似乎無法弄清楚如何適當地結束後迴應我的意見。希望這是有道理的,請有人看看我的代碼,並指出我在正確的方向嗎?
<?php
$arrayTermdates = array();
foreach($termsResult->getRecords() as $key => $term)
{
$arrayTermdates[] = $term->getField('Term Year PrNo');
}
$arrayTermdates = array_unique($arrayTermdates);
sort($arrayTermdates, SORT_STRING | SORT_FLAG_CASE);
foreach($arrayTermdates as $termdate)
{
preg_match_all("/\[[^)]+\]/",$termdate,$matches);
$year = str_replace('[', '', $matches[0][0]);
$year = str_replace(']', '', $year);
$year = str_replace(' ', '', substr($year, 0, -2));
$termdate = preg_replace("/\[[^)]+\]/","",$termdate);
/*
/- opening delimiter (necessary for regular expressions, can be any character that doesn't appear in the regular expression
\[ - Match an opening parenthesis
[^)]+ - Match 1 or more character that is not a closing parenthesis
\] - Match a closing parenthesis
/- Closing delimiter
*/
if ($previousYear != $year || $previousYear == '')
{
echo '<div class="yearblock">';
echo '<strong>'.$year.'</strong><br />';
}
?>
<div class="term_value_list">
<input name="Termdate[]" type="radio" value="<?php echo $termdate; ?>">
<?php
$TermdatesArray = explode('-',$termdate);
$Termdisplay = $TermdatesArray[0].' '.$TermdatesArray[1].' ['.str_replace(' ', '', $TermdatesArray[2]).' PR]';
echo $Termdisplay;
?>
<!-- I need to echo an </div> for div class yearblock -->