Welcome to WebmasterWorld Guest from 54.147.134.218

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Array "Streaks"

     
10:12 am on Sep 17, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:June 7, 2007
posts:82
votes: 0


At the moment I'm working on a basketball website and thought a neat feature would be to have the current "streak" that a team is on displayed on their page. For instance a "3 game winning streak" or a "2 game losing streak". Streaks can be only 1 game, that's fine.

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?

10:41 am on Sept 17, 2008 (gmt 0)

Full Member

10+ Year Member

joined:Feb 24, 2005
posts:225
votes: 0


Try this function on for size,

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;
}
}
}
10:43 am on Sept 17, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:July 12, 2007
posts:766
votes: 0



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);

Could maybe do with an additional check to make sure that it is an array being send to the function. If not an array then you could use explode to make an array.

<edit>
Beaten by 2 mins ;)

[edited by: PHP_Chimp at 10:45 am (utc) on Sep. 17, 2008]

11:29 am on Sept 17, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:June 7, 2007
posts:82
votes: 0


Thanks for your quick responses!

I tried Sekka's approach and it worked fine and dandy... except it got wins and losses the wrong way round! So I just changed the case "L" to case "W" and vice versa and now it's perfecto.

Thanks again

11:41 am on Sept 17, 2008 (gmt 0)

Full Member

10+ Year Member

joined:Feb 24, 2005
posts:225
votes: 0


Balls! It's doesn't get them the wrong way round, there is an error in my code.

Replace,

switch ($game)

with

switch ($type)

Cheers!

11:43 am on Sept 17, 2008 (gmt 0)

Junior Member

5+ Year Member

joined:June 7, 2007
posts:82
votes: 0


Okay, question number two!

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?

11:52 am on Sept 17, 2008 (gmt 0)

Full Member

10+ Year Member

joined:Feb 24, 2005
posts:225
votes: 0


I'm bit busy at work now so I can't do a function.

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.