1

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.

Will WP
  • 1,225
  • 1
  • 9
  • 19
  • 1
    Take a look at: http://stackoverflow.com/q/33874059/3933332 – Rizier123 Feb 28 '16 at 19:16
  • @Rizier123 Thank you! I had no exact idea how to make this into words :) – Will WP Feb 28 '16 at 19:18
  • [Here are some codegolf approaches](http://codegolf.stackexchange.com/questions/66392/condense-these-page-numbers?lq=1) - (no PHP version though). – Kenney Feb 28 '16 at 19:19
  • @WillWP Did it solved the problem for you? – Rizier123 Feb 28 '16 at 19:20
  • @Rizier123 i am trying to get my head around it. My initial values are not in an array, they're individually matched to the key. Not sure if there is a way around it. – Will WP Feb 28 '16 at 19:21
  • @WillWP `$row[0]` holds your values, e.g. `1,2,3,` right? – Rizier123 Feb 28 '16 at 19:24
  • @Rizier123 it holds the one value while inside the while, but I suppose it all goes away at the closing }? Apologies if this sounds trivial - I am really new to arrays so do not really know how to handle and their differences to "regular" variables. – Will WP Feb 28 '16 at 19:26
  • @WillWP Try to use: `while($row=mysql_fetch_row($datarounds)){$arr[] = $row[0];} /* FULL Code from the dupe*/` – Rizier123 Feb 28 '16 at 19:27
  • (Also if you solved the problem here, take a look here: http://stackoverflow.com/q/12859942/3933332) – Rizier123 Feb 28 '16 at 19:34
  • @Rizier123 I am not sure I follow - am I meant to use the code you posted instead of my current `while($row=mysql_fetch_row($datarounds))`? If so, I have no idea where then to be able to use `$rounddisplay = array_search("$roundid",$roundsarray);` before i even get to the other page you suggested. – Will WP Feb 28 '16 at 19:38
  • @WillWP Do: `while($row=mysql_fetch_row($arrayKeys[] = $row[0];}` and then after that code: `$arr = array_intersect_key($roundsarray, array_flip($arrayKeys));` and with: `print_r($arr);` you should see all values which you want in an array. If that is the case you can just append the code from the dupe under it. – Rizier123 Feb 28 '16 at 19:41
  • @Rizier123 Sorry, being slow here: is that meant to replace the code you posted above? And where does that bracket after $row[0]; open? Thank you for your help! – Will WP Feb 28 '16 at 19:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/104804/discussion-between-rizier123-and-will-wp). – Rizier123 Feb 28 '16 at 19:46

0 Answers0