开发者

Dynamic Template Sheet?

开发者 https://www.devze.com 2022-12-15 05:40 出处:网络
I have 5 game sections that will use the same template sheet, but they will all pull data from a different ta开发者_Go百科ble in mysql in the same database. However, I am incorporating this: How to in

I have 5 game sections that will use the same template sheet, but they will all pull data from a different ta开发者_Go百科ble in mysql in the same database. However, I am incorporating this: How to include config.php efficiently?

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消