开发者

php get, random records, and the back button

My site has a library full of games, nations, game scenarios, etc.

library.php is given a type=___ & id=___

for example library.php?type=scenario&id=ABCD001

library.php saves the id to a session variable and loads an include appropriate for the type

This all works just dandy. Now, I wanted to give my users the option of pulling up a random scenario. To do that, I added a special id to the logic within lib-scenario.php (the include) such that if given library.php?type=scenario&i开发者_Python百科d=random the include knows to run an alternate query for a random record rather than for the actual id

This also works just dandy... unless someone hits the Random Scenario button two+ times in a row, and decides that the previous random scenario was way cooler, I want to go back to that.

Because the http address is always directory/library.php?type=scenario&id=random no matter how many times you click Random Scenario, as soon as you click back you'll be taken to the last page with an alternate address you visited.

So, if you start at the Home page, and hit Random Scenario 35 times, then decide the 34th one was what you wanted and click BACK, you'll be put back onto the Home page.

I must admit this was not a problem I had anticipated. One of my testers was the first to have the urge to back-up in the random scenario stream and here we are.

How can I add back-up functionality to my script?


Make the 'Random Scenario' button simply link to an actual (but random) scenario id. You'll probably have to construct this with an SQL query to get all the id's of your scenarios.

$result = mysql_query("SELECT id FROM scenarios");
while ($row = mysql_fetch_row($result)) {
    $ids[] = $row[0];
}
$randomid = array_rand($ids);

Button:

<a href="directory/library.php?type=scenario&id=<?php echo $randomid; ?>Random Scenario</a>

If your scenario id's are all consecutive numbers you can simply use this instead:

$randomid = rand($min, $max);


you can resolve this by redirecting to the canonical url for the scenario, i.e.: id=random redirects to id=A92831 or whatever was selected. the final url will be stored in the history, rather than the id=random url.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜