Forum Moderators: coopster
What I need is a small table on my front page that shows 5 teams but team1 is the featured team. By this I mean that if team1 is in 3rd through 8th position they will appear in the middle like this example where they are in 5th:
3. Team2
4. Team5
5. Team1
6. Team3
7. Team9
But if they are in 1st, 2nd, 9th or 10th I need the positioning to be different as I still want to show 5 teams like this when they are in 2nd:
1. Team4
2. Team1
3. Team2
4. Team5
5. Team9
What I would like to know is how to set up this query so I can get the correct ranking position and how to echo it to display team1 and the other 4 teams surrounding them.
select team1's rank and then issue a select that gets the ones around it
a better option, since there are only 10 teams is to select them all and figure out where in your array team1 is and show the appropriate teams around them.
I see a 10 part switch statement involved probably. It is pretty messy no matter how you do it.
feature team = team1
Rank, team
1, team7
2, team9
3, team 6
4, team5
5, team10
6, team1
7, team2
8, team4
9, team3
10, team8
Then, would the front page be this (took me a second to get that ahaha):
4, team5
5, team10
6, team1
7, team2
8, team4
--Nick
There are five possibilities.
1st - they are the top team and 4 are below.
2nd - they are the second team and 3 are below.
3rd through 8th - they are in the middle.
9th - they are second from the bottom and 3 above.
10th - they are bottom and 4 teams above.
so basically I've started with:
SELECT "teamid, teamname,wins,ties,losses, (wins * 3) + ties AS pts
FROM standings
ORDER BY pts DESC"
/sorry, pretty new to PHP.
[edited by: mooger35 at 7:24 pm (utc) on Feb. 14, 2006]
$query_standings = "SELECT teamid, teamname,wins + losses + ties AS gp, wins,ties,losses,gf,ga, wins * 3 + ties AS pts, SUM(IF(gf='0',0,gf)) / SUM(IF(ga='0',1,ga)) AS gr
FROM standings
GROUP BY teamname
ORDER BY pts DESC, gr DESC, teamname ASC";
$standings = mysql_query($query_standings, $connectDB) or die(mysql_error());
$arraycount = 1;
while($standings_row = mysql_fetch_row($standings)) {
$myArray[$arraycount++] = $standings_row[1];
}
//display results
while(list($key,$value) = each($myArray)) {
echo "$key : $value <BR>";
} echo "?";
that gives me my list:
1 : Team3
2 : Team4
3 : Team7
4 : Team1
5 : Team2
6 : Team5
7 : Team10
8 : Team9
9 : Team6
10 : Team8
How do I pull $key where $value = "team1"?
Should I have it the other way around where rank is the $value?
Here's the working code:
$arraycount = 1;
while($standings_row = mysql_fetch_row($standings)) {
$myArray[$arraycount++] = $standings_row[1];
if ($standings_row[1] == 'team1') $therank = $arraycount-1;
}
switch (TRUE) {
case ($therank <= 8 AND $therank >= 3):
$pos1 = $therank-2;
$pos2 = $therank-1;
$pos3 = $therank;
$pos4 = $therank+1;
$pos5 = $therank+2;
break;
case ($therank = 1):
$pos1 = $therank;
$pos2 = $therank+1;
$pos3 = $therank+2;
$pos4 = $therank+3;
$pos5 = $therank+4;
break;
case ($therank = 2):
$pos1 = $therank-1;
$pos2 = $therank;
$pos3 = $therank+1;
$pos4 = $therank+2;
$pos5 = $therank+3;
break;
case ($therank = 9):
$pos1 = $therank-3;
$pos2 = $therank-2;
$pos3 = $therank-1;
$pos4 = $therank;
$pos5 = $therank+1;
break;
case ($therank = 10):
$pos1 = $therank-4;
$pos2 = $therank-3;
$pos3 = $therank-2;
$pos4 = $therank-1;
$pos5 = $therank;
break;
}
echo $pos1.". ".$myArray[$pos1]."<br>";
echo $pos2.". ".$myArray[$pos2]."<br>";
echo $pos3.". ".$myArray[$pos3]."<br>";
echo $pos4.". ".$myArray[$pos4]."<br>";
echo $pos5.". ".$myArray[$pos5]."<br>";
nice chops on the true switch, I didn't think of that. Just for the heck of it this is how I would write it, can't really test it so might have errors
$arraycount = 1;
while($standings_row = mysql_fetch_row($standings)) {
$myArray[$arraycount++] = $standings_row[1];
if ($standings_row[1] == 'team1') $therank = $arraycount-1;
}
$min = 0;
$max = 0;
switch (TRUE) {
case ($therank <= 7 AND $therank >= 4):
$min = $therank-2;
$max = $therank+2;
break;
case ($therank <= 3):
$min = 1;
$max = 5;
break;
case ($therank >= 8):
$min = 5;
$max = 10;
break;
}
$arraycount = 1;
while (isset($myArray[${'pos' . $arraycount}])) {
if ($arraycount >= $min && $arraycount <= $max) echo ${'pos' . $arraycount}.". ".$myArray[${'pos' . $arraycount}]."<br>";
$arraycount++;
}
$arraycount = 1;
while($standings_row = mysql_fetch_row($standings)) {
$myArray[$arraycount++] = $standings_row[1];
if ($standings_row[1] == 'team1') $therank = $arraycount-1;
}
$min = $therank-2;
$max = $therank+2;
if ($min <= 0) $min = 1;
if ($max > 10) $max = 10;
$arraycount = 1;
while (isset($myArray[${'pos' . $arraycount}])) {
if ($arraycount >= $min && $arraycount <= $max) echo ${'pos' . $arraycount}.". ".$myArray[${'pos' . $arraycount}]."<br>";
$arraycount++;
}
;)