I have written a code that matches the values in an array with values from a query, and returns the array key for each.
This is the array:
$roundstotal=mysql_query("SELECT id FROM calendar WHERE eventDate BETWEEN '$season_start' AND '$season_end' AND competition='$compId' AND tag!='T' ORDER BY eventDate ASC", $CONNECTW);
$roundamount=mysql_num_rows($roundstotal);
while($row = mysql_fetch_assoc($roundstotal)) {
$roundsarray[]=$row['id']; }
It returns an array of values (598,602,607...) which are the id of a series of events held over one year.
The values from the query are always matching some of the array values, so I do a search to match them for the respective key. This tells us that a specific individual attended the, say, 1st, 2nd, 4th, 7th, event in the series.
$roundinterold=0;
$roundinter=0;
$datarounds=mysql_query("SELECT DISTINCT eventID FROM results WHERE (eventID BETWEEN '$firstevent' AND '$lastevent') AND compId='$compId' AND teamId='$teamid' AND (eventSession='R' OR eventSession='R1' OR eventSession='R2' OR eventSession='R3') AND driverId='$driversource' ORDER BY eventID", $CONNECTW);
while($row=mysql_fetch_row($datarounds))
{
$roundinterold=$roundinter;
$roundid=$row[0];
$rounddisplay = array_search("$roundid",$roundsarray);
$roundinter = $rounddisplay+1;
$roundfinal = "$roundinter";
if (($roundinterold!=0)&&($roundinter==$roundinterold+1)) { $roundfinal=""; }
if (($roundinterold!=0)&&($roundinter!=$roundinterold+1)) { $roundfinal="-$roundinterold, $roundinter"; }
if (($roundinter!=$roundinterold+1)&&($roundinterolder==$roundinterold)) { $roundfinal="$roundinter"; }
if (($roundinterold==0)&&($roundinter!=$roundinterold+1)) { $roundfinal="$roundinter"; }
$roundpack .= "$roundfinal";
}
The rest of the code is designed to not display $roundfinal
if the key is immediately the next integer after the previous one. This is because the aim would be to avoid having this:
Events attended: 1,2,3,4,5,6,9,10,11,15,16
But to have this:
Events attended: 1-6,9-11,15-16
Is there an elegant way to do it? I am currently having to figure out all possible cases and permutations, but it's not easy in case of isolated events and to indicate the final event on the series, as there is no further event I can use as a marker.