Page 1 of 1

need switch statement help

Posted: Sat May 10, 2008 7:18 am
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.

Re: need switch statement help

Posted: Sat May 10, 2008 7:39 am
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.

Re: need switch statement help

Posted: Sat May 10, 2008 7:53 am
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)

Re: need switch statement help

Posted: Sat May 10, 2008 8:00 am
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.

Re: need switch statement help

Posted: Sat May 10, 2008 8:13 am
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

Re: need switch statement help

Posted: Sat May 10, 2008 8:31 am
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.

Re: need switch statement help

Posted: Sat May 10, 2008 9:28 am
by dave416
terrific help, thanks a ton. i figured it out with ur help

Re: need switch statement help

Posted: Sat May 10, 2008 12:22 pm
by RobertGonzalez
Why are you switching when you should be selecting?