need switch statement help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dave416
Forum Newbie
Posts: 6
Joined: Sat May 10, 2008 7:13 am

need switch statement help

Post by dave416 »

hey, so heres my basic idea. i have a website about this team and i want to create a file called roster.php which i already have and that displays all the player information from a mysql database in a nice little neat table.

each player has detailed information about him in the database and what i want to do is create something like where if you click on Joe Smith's name it displays a page directly for Joe Smith with all the relevant information and a photo (all drawn from the database), so that i dont have to create 50 different pages for 50 different players.

i think i can do this with a switch statement but i need a little jumpstart to get me going. thanks for any help you can provide

edit: so the end result would be a page url of roster.php?player=JoeSmith and that page would display all the info i have about Joe in the database.
User avatar
lafever
Forum Commoner
Posts: 99
Joined: Sat Apr 05, 2008 2:03 pm
Location: Taylor, MI

Re: need switch statement help

Post by lafever »

Well you could do it with a switch but you're grabbing information from a database. Here's something simple to help you get started. Although, I would recommend doing it by an ID number in the database so it's easier to make sure the proper input is being inputted (since names can contain odd characters)

Code: Select all

 
function checkID($id) {
   // sql query to check if the ID exists in the database
}
 
 
if (isset($_GET['id']) && checkID((int)$_GET['id'])) {
   include('viewplayer.php');
} else {
   //display list of players
   include('playerlist.php');
}
 
 
Playerlist.php would contain the list of players, and viewplayer would contain info displaying info for that specific player.
dave416
Forum Newbie
Posts: 6
Joined: Sat May 10, 2008 7:13 am

Re: need switch statement help

Post by dave416 »

let me better explain myself, im not sure i did a good job the first time. so lets say i want to view a player page about Joe Smith. i click on his name and i want that to lead me to something like roster.php?player=JoeSmith and my idea for the code would be somewhere along these lines:

Code: Select all

 
switch ($player) {
case "Joe Smith":
     echo $alljoesmithsinfo; //dont need help with this part
break;
}
 
and i want cases for ALL the players in my database (for obvious reasons)
User avatar
lafever
Forum Commoner
Posts: 99
Joined: Sat Apr 05, 2008 2:03 pm
Location: Taylor, MI

Re: need switch statement help

Post by lafever »

I got what you're saying. The same principal I showed will also use roster.php?id=whatever just change $_GET['id'] to $_GET['player'] and add some sanitation.

I don't see as to why you'd want to use a switch since the stuff is stored in the database. Then every time you add a player to the database, you'd have to go in and add another case to your switch in the script. Unless this is how you'd want to do it.
dave416
Forum Newbie
Posts: 6
Joined: Sat May 10, 2008 7:13 am

Re: need switch statement help

Post by dave416 »

i think i was hoping i could put the cases in a loop and create them all through that....which i assume is not possible judging by ur response and some intuition
User avatar
lafever
Forum Commoner
Posts: 99
Joined: Sat Apr 05, 2008 2:03 pm
Location: Taylor, MI

Re: need switch statement help

Post by lafever »

Well you can always create a function that builds an array of the players from the database then use a switch like so. There would be no building cases for each player though.

Code: Select all

 
function playersList() {
  // sql query
 
  // return array of players
}
 
function playerInfo($player) {
  // sql query
 
  // return $player's data
}
 
$array = playersList();
 
 
switch ($_GET['player']) {
 
    case in_array($_GET['player'], $array);
        playerInfo($_GET['player']); //player info
    break;
}
 
Really don't see the advantage of using this way over the other though.
dave416
Forum Newbie
Posts: 6
Joined: Sat May 10, 2008 7:13 am

Re: need switch statement help

Post by dave416 »

terrific help, thanks a ton. i figured it out with ur help
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: need switch statement help

Post by RobertGonzalez »

Why are you switching when you should be selecting?
Post Reply