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.
need switch statement help
Moderator: General Moderators
Re: need switch statement help
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)
Playerlist.php would contain the list of players, and viewplayer would contain info displaying info for that specific player.
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');
}
Re: need switch statement help
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:
and i want cases for ALL the players in my database (for obvious reasons)
Code: Select all
switch ($player) {
case "Joe Smith":
echo $alljoesmithsinfo; //dont need help with this part
break;
}
Re: need switch statement help
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.
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
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
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.
Really don't see the advantage of using this way over the other 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;
}
Re: need switch statement help
terrific help, thanks a ton. i figured it out with ur help
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: need switch statement help
Why are you switching when you should be selecting?