shmeeg Posted June 6, 2010 Share Posted June 6, 2010 ya, its me again... basically, im using $_GET so that players can bookmark pages for easy access, for example, stats.php?type=player&player=shmeeg (where obviously it only displays my statistics) however, my code works up to a point. im using a switch case for type=team&team=$team heres the code (it's working fine) <?php $team = $_GET['team']; switch ($team){ case "$team": $result = mysql_query("SELECT * FROM players WHERE team='$team' ORDER BY total DESC") or die(mysql_error()); echo "<table>"; echo "<tr> <th>player name</th> <th>appearances</th> <th>goals</th> <th>assists</th> <th>total</th> <th>points/match</th> <th># motm</th> </tr>"; while($row = mysql_fetch_array( $result )) { echo "<tr><td>"; echo $row['name']; echo "</td><td><center>"; echo $row['apps']; echo "</center></td><td><center>"; echo $row['goals']; echo "</center></td><td><center>"; echo $row['assists']; echo "</center></td><td><center>"; echo $row['total']; echo "</center></td><td><center>"; echo $row['ppm']; echo "</center></td><td><center>"; echo $row['motm']; echo "</center></td></tr>"; } echo "</table>"; break; } ?> the next step would be to $player = $_GET['player']; switch ($player){ firstly is this even possible? would i need to use another method or would this actually work? Quote Link to comment https://forums.phpfreaks.com/topic/204015-switch-case-with-2-switches/ Share on other sites More sharing options...
dabaR Posted June 6, 2010 Share Posted June 6, 2010 Yes, it is possible, write it out, and if it does not work, post again. BTW, switch($team) { case "$team": is like saying "if (true) { ... }" Quote Link to comment https://forums.phpfreaks.com/topic/204015-switch-case-with-2-switches/#findComment-1068544 Share on other sites More sharing options...
shmeeg Posted June 6, 2010 Author Share Posted June 6, 2010 i now have a whole host of errors...i'm a little bit lost here's the whole piece of code: <?php $team = $_GET['team']; switch ($team){ case "$team": $result = mysql_query("SELECT * FROM players WHERE team='$team' ORDER BY total DESC") or die(mysql_error()); echo "<table>"; echo "<tr> <th>player name</th> <th>appearances</th> <th>goals</th> <th>assists</th> <th>total</th> <th>points/match</th> <th># motm</th> </tr>"; while($row = mysql_fetch_array( $result )) { echo "<tr><td>"; echo $row['name']; echo "</td><td><center>"; echo $row['apps']; echo "</center></td><td><center>"; echo $row['goals']; echo "</center></td><td><center>"; echo $row['assists']; echo "</center></td><td><center>"; echo $row['total']; echo "</center></td><td><center>"; echo $row['ppm']; echo "</center></td><td><center>"; echo $row['motm']; echo "</center></td></tr>"; } echo "</table>"; break; } $player = $_GET['name']; switch ($player){ case "$player": $result2 = mysql_query("SELECT * FROM players WHERE name='$player'") or die(mysql_error()); echo "name:"; echo $_GET["name"]; echo "<br />"; echo "nation:"; echo $_GET["team"]; echo "<br />"; break; } ?> i really am lost Quote Link to comment https://forums.phpfreaks.com/topic/204015-switch-case-with-2-switches/#findComment-1068556 Share on other sites More sharing options...
dezkit Posted June 6, 2010 Share Posted June 6, 2010 <?php $team = $_GET['team']; switch ($team){ case "$team": $result = mysql_query("SELECT * FROM players WHERE team='$team' ORDER BY total DESC") or die(mysql_error()); echo "<table>"; echo "<tr> <th>player name</th> <th>appearances</th> <th>goals</th> <th>assists</th> <th>total</th> <th>points/match</th> <th># motm</th> </tr>"; while($row = mysql_fetch_array( $result )) { echo "<tr><td>"; echo $row['name']; echo "</td><td><center>"; echo $row['apps']; echo "</center></td><td><center>"; echo $row['goals']; echo "</center></td><td><center>"; echo $row['assists']; echo "</center></td><td><center>"; echo $row['total']; echo "</center></td><td><center>"; echo $row['ppm']; echo "</center></td><td><center>"; echo $row['motm']; echo "</center></td></tr>"; } echo "</table>"; break; $player = $_GET['name']; switch ($player){ case "$player": $result2 = mysql_query("SELECT * FROM players WHERE name='$player'") or die(mysql_error()); echo "name:"; echo $_GET["name"]; echo "<br />"; echo "nation:"; echo $_GET["team"]; echo "<br />"; break; } } ?>[/code]try that Quote Link to comment https://forums.phpfreaks.com/topic/204015-switch-case-with-2-switches/#findComment-1068559 Share on other sites More sharing options...
dabaR Posted June 6, 2010 Share Posted June 6, 2010 I don't think you understand what a switch is for, based on your code. What functionality are you trying to implement? Quote Link to comment https://forums.phpfreaks.com/topic/204015-switch-case-with-2-switches/#findComment-1068562 Share on other sites More sharing options...
shmeeg Posted June 6, 2010 Author Share Posted June 6, 2010 basically, what I'm trying to do is make it so that the URL reads /stats.php?type=team&team=UK or /stats.php?type=player&player=shmeeg depending on "type", different information will be displayed. if type=player, then player statistics will be displayed, eg. name: shmeeg nation: UK position: defender tournament apps goals assists total ppm motm world cup 3 1 6 7 7/3 2 matches time/date home away result stats time/date UK FI 1-2 0+1 match report if type=team, then team stats will be displayed eg. nation: UK statistics: played 5, won 3, lost 1, drawn 1 players: defenders: list of defenders from team selection midfielders: list of midfielders from team selection attackers: list of attackers from team selection tournament played goals for goals against +/- world cup 3 11 4 +7 friendly 2 6 1 +6 recent matches time/date tournament home away result time/date world cup UK FI 1-2 match report Quote Link to comment https://forums.phpfreaks.com/topic/204015-switch-case-with-2-switches/#findComment-1068565 Share on other sites More sharing options...
ngoweb Posted June 6, 2010 Share Posted June 6, 2010 Your code is funky! Ok lets break this done a bit for you $team = $_GET['team']; //so $team will be a value like player switch ($team){ //case "$team": // This is wrong - you are looking for a value here - not a variable case "player": //So if the URL variable is player you will output this code (I havent checked it!) $result = mysql_query("SELECT * FROM players WHERE team='$team' ORDER BY total DESC") or die(mysql_error()); echo "<table>"; echo "<tr> <th>player name</th> <th>appearances</th> <th>goals</th> <th>assists</th> <th>total</th> <th>points/match</th> <th># motm</th> </tr>"; while($row = mysql_fetch_array( $result )) { echo "<tr><td>"; echo $row['name']; echo "</td><td><center>"; echo $row['apps']; echo "</center></td><td><center>"; echo $row['goals']; echo "</center></td><td><center>"; echo $row['assists']; echo "</center></td><td><center>"; echo $row['total']; echo "</center></td><td><center>"; echo $row['ppm']; echo "</center></td><td><center>"; echo $row['motm']; echo "</center></td></tr>"; } echo "</table>"; echo "</table>"; break; // Ok now second case - I used player2 as an example case "player2": $result2 = mysql_query("SELECT * FROM players WHERE name='$player'") or die(mysql_error()); echo "name:"; echo $_GET["name"]; echo "<br />"; echo "nation:"; echo $_GET["team"]; echo "<br />"; break; //dont forget a default case incase nothing matches default: echo "this is the default option"; } //ending the switch } ?> I hope that helps! Quote Link to comment https://forums.phpfreaks.com/topic/204015-switch-case-with-2-switches/#findComment-1068573 Share on other sites More sharing options...
shmeeg Posted June 6, 2010 Author Share Posted June 6, 2010 I dont need two cases. I'm using 1 generic case. $_GET is defining what the case is. So if I search for /stats.php?type=team&team=UK, it will display all team statistics for the team UK, and the same for any other team. What I need to do now is to be able to change type to player, and for it to do the same for the player chosen. Quote Link to comment https://forums.phpfreaks.com/topic/204015-switch-case-with-2-switches/#findComment-1068589 Share on other sites More sharing options...
ngoweb Posted June 6, 2010 Share Posted June 6, 2010 The Case statement will allow you to get the Team for example, then using mysql you can get the player - is that what you mean? You can also call a function within the case - so pass a parameter for the player to output the relevant stats accourding to URL parameter. Quote Link to comment https://forums.phpfreaks.com/topic/204015-switch-case-with-2-switches/#findComment-1068590 Share on other sites More sharing options...
dabaR Posted June 6, 2010 Share Posted June 6, 2010 Funkeh! $type = $_GET['type']; if ('team' == $type) { $team = $_GET['team']; $stats = get_team_stats($team); display_team_stats($stats); } elseif ('player' == $type) { $player = $_GET['player']; $stats = get_player_stats($player); display_player_stats($stats); } else { die('You can only look up stats for a team or a player'); } function get_team_stats($team_name) { //somehow get the stats, make sure you use mysql_real_escape_string() on the $team_name, otherwise people can erase all your data, reference http://en.wikipedia.org/wiki/SQL_injection } function get_player_stats($player_name) { //get the stats for the player, again use mysql_real_escape_string() on $player_name for same reason as above } function display_team_stats($stats) { //turn the stats array into HTML } function display_player_stats($stats) { //turn the stats array into HTML } Quote Link to comment https://forums.phpfreaks.com/topic/204015-switch-case-with-2-switches/#findComment-1068592 Share on other sites More sharing options...
shmeeg Posted June 6, 2010 Author Share Posted June 6, 2010 this is confusing me a lot to clear it up, I could post some examples of what I'm trying to do http://ohl-liiga.net/index.php?sivu=joukkueet&id=32 (sivu=page and joukkueet=team) http://www.kiekkoliiga.net/tilastot/?tilasto=pelaaja&id=934 (tilastot=statistics, pelaaja=player) Quote Link to comment https://forums.phpfreaks.com/topic/204015-switch-case-with-2-switches/#findComment-1068604 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.