php - Need help within a loop to group data -
need help, been going around ages, cant seem solve it. i've got data in field called "term year prno". data in field below:
[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 i need make each row of data in field radio button selection, i've managed sticking contents of field , spitting out input field. i've used regex clean of input value - i.e. remove first sort field. far good. problem cant seem solve need break data down in segments , wrap them around div class called yearblock based on years, 2010-2011 | 2011-2012 | 2012-2013, can arrange them nicely using css. i've managed figure out how start div class, comparing previous year current year, cant seem figure out how appropriately put end in after echoing out input. hope makes sense, can @ code , point me in right direction please?
<?php $arraytermdates = array(); foreach($termsresult->getrecords() $key => $term) { $arraytermdates[] = $term->getfield('term year prno'); } $arraytermdates = array_unique($arraytermdates); sort($arraytermdates, sort_string | sort_flag_case); foreach($arraytermdates $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 regular expressions, can character doesn't appear in regular expression \[ - match opening parenthesis [^)]+ - match 1 or more character not closing parenthesis \] - match 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; ?> <!-- need echo </div> div class yearblock -->
you're there. i'm going make little simplification code rid of regexp, however.
first off, going initiate year tracker. allow keep track of year in.
$mycurrentyear = false; from there, we'll loop through rows. first check check if year different. if is, , current year not false, we'll output </div>. then, if different, we'll print headers new year.
foreach ($arraytermdates $date) { $newdate = split("-",substr($date,strpos($date,"]")+1)); // gotta remember 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>:"; } // processing here } // note: last 1 won't closed. we'll close here if ($mycurrentyear !== false) { echo "</div>"; }
Comments
Post a Comment