2013-05-21 51 views
0

需要一些幫助,一直在這個問題上好幾年,但似乎無法解決它。我在一個名爲「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 --> 

回答

0

你快到了。不過,我將簡化代碼來擺脫正則表達式。

首先,我們將啓動一年跟蹤器。這將使我們能夠跟蹤我們在哪一年。

$myCurrentYear = false; 

從那裏,我們將遍歷您的行。我們要做的第一個檢查是檢查年份是否有所不同。如果是,並且當年不是錯誤的,我們將輸出一個</div>。然後,如果不同,我們將打印新年的標題。

foreach ($arrayTermdates as $date) { 
    $newdate = split("-",substr($date,strpos($date,"]")+1)); 
    // Gotta remember to trim stuff 
    $year = (int)trim($newdate[1]); 
    if ($year != $myCurrentYear) { 
     if ($myCurrentYear !== false) { 
      echo "</div>"; 
     } 
     $myCurrentYear = $year; 
     echo "<div class='yearblock'><strong>Year ".$myCurrentYear."-".($myCurrentYear+1)."</strong>:"; 
    } 
    // Do your processing here 
} 
// NOTE: the last one won't be closed. We'll close it here 
if ($myCurrentYear !== false) { 
    echo "</div>"; 
} 
0

第一輸出之前,開始你的第一個div:

echo '<div class="yearblock">'; 

然後關閉塊,並打開一個新的每次你if()條件爲真:

if ($previousYear != $year || $previousYear == '') 
{ 
    echo '</div><div class="yearblock">'; 
    echo '<strong>'.$year.'</strong><br />';       
} 

而且最後,在完成創建輸出後關閉最終的div:

echo '</div>';