Forum Moderators: coopster
So all of the results are in a database, so let's say I run through and add either "W" or "L" to an array for each one which for example would give me an array like
W, L, L, W, L, W, W, W
How would I then get this to tell me that the last three are wins so the team must be on a 3 game winning streak?
function getStreak($streak)
{
$count = 0;
while ($game = array_pop($streak))
{
if (!isset($type))
{
$type = $game;
$count++;
continue;
}
if ($game == $type)
{
$count++;
continue;
}
else
{
switch ($game)
{
case "W":
$type = "Win" . ($count != 1 ? "s" : "");
break;
case "L":
$type = "Loss" . ($count != 1 ? "es" : "");
break;
default:
return NULL;
break;
}
return $count . " " . $type;
}
}
}
function streaks($inArray) {
$length = count($inArray);
$win = 0;
$loose = 0;
$lastResult = NULL;
for ($i=$length-1; $i>-1; $i--) { // count backwards
if ($lastResult === NULL ¦¦ $inArray[$i] == $lastResult) {
switch ($inArray[$i]) {
case 'W':
$win++;
break;
case 'L':
$loose++;
break;
}
}
else {
break;
}
$lastResult = $inArray[$i];
}
$win ? $out = "The winning streak is: $win" : $out = "The loosing streak is: $loose";
echo $out;
}
$test = array('W', 'W', 'L', 'L', 'L', 'W', 'W');
streaks($test);
<edit>
Beaten by 2 mins ;)
[edited by: PHP_Chimp at 10:45 am (utc) on Sep. 17, 2008]
Sorry, just thought of this, but I'm trying to build up a list of neato features!
Let's say we take the streak that PHP_Chimp suggested:
W, W, L, L, L, W, W
I was wondering if it would be possible to have a feature that would be like "Longest ever winning streak" and "Longest ever losing streak", so in this case it would be 2 wins and 3 losses if these were all the results ever. Bigger picture I guess it would have to cycle through the seasons like
...
for each season {
find longest winning streak
find longest losing streak
check to see if these streaks are longer than other seasons
if they are {
longest winning streak season = this season
}
}
...
So you could output like:
Longest Winning Streak: 5 wins (05/06 season)
Longest Losing Streak: 4 losses (02/03 season)
Is this possible? I'm kind of just brainstorming... not sure if it's possible to pull streaks out of a middle of an array?
However, what I will say is that the function for this could potentially do a largish DB call and then have to run through all the results.
If this is being displayed on a regular basis it could add load to the server. I would suggest working this out once and storing as part of the DB record, and whenever another result is added, update the figure in the DB record.