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-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 -->
4

2 回答 2

0

在第一个输出之前启动你的第一个 div:

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

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

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

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

echo '</div>';
于 2013-05-21T20:32:40.820 回答
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>";
 }
于 2013-05-21T20:33:04.283 回答