tags:

views:

29

answers:

1

I have 5 game sections that will use the same template sheet, but they will all pull data from a different table in mysql in the same database. However, I am incorporating this: http://stackoverflow.com/questions/1712973/how-to-include-config-php-efficiently/1713000#1713000

Okay, so here's how I was planning to tackle it. For example, let's say I go to index.php?page=pokemon then I was planning to set $game_name to $_GET['page'] and then use $game_name to select the table (assuming I made the table name pokemon) and then all the data would be pulled from mysql properly. Is there a better way I should do this?

Next, in the situation where someone tries to go to an non-existent game then I want it to redirect to the homepage. For example, let's say they misspelled pokemon to poekmon and went to index.php?page=poekmon then it would properly show a blank template without data right? Instead, how can I make it just redirect to homepage if it was a game that doesn't exist?

This is kind of similar to how wordpress is setup. It uses Single.php as a template and then grabs data from the database.

+1  A: 

Perform your logic before printing anything to the page itself, that way, if you decide the user has requested a game that doesn't exist, you can simply redirect with the header();

header("Location: homepage.php");

As for finding out if the requested game exists, you can have a function that gets game-data, or returns false. That way, you can do something like this:

if (!$data = game_data("pokemon")) header("Location: no-exist.php");

If the requested game does exist, then the script continues, and you use $data to access the game data. Of course you'll replace "pokemon" in the above example with your variable, after it's been sanitized and prepped to be handled.

Remember to do this before you output anything to the page itself, or else the call to header() will raise a "headers already sent" message.

Jonathan Sampson
Making a function is definitely a good idea. I think I will incorporate that.
Doug
Have the function return `false` if no records are found matching the request.
Jonathan Sampson