在循环中需要帮助来分组数据

时间:2013-05-21 20:19:47

标签: php

需要一些帮助,多年来一直在努力,但似乎无法解决它。我在一个名为“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-201230]Summer - 2012 - 1st 
[2011-201231]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

我需要在该字段中的每一行数据中选择一个单选按钮,我通过粘贴字段的内容并将其作为输入字段吐出来设法完成。我还使用正则表达式来清理输入值的外观 - 即删除第一个排序字段。到现在为止还挺好。我似乎无法解决的问题是我需要在段中分解数据,并根据年份,2010-2011将它们包装在一个名为yearblock的div类中。 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 -->

2 个答案:

答案 0 :(得分:0)

在第一个输出之前开始你的第一个div:

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

然后每当if()条件为真时关闭块并打开一个新块:

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

最后,在完成输出后关闭最后一个div:

echo '</div>';

答案 1 :(得分: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>";
 }