jackmcnally Posted September 28, 2011 Share Posted September 28, 2011 Hi Guys, What I'm aiming for is a random choice generator from a list of variables, and when that choice is chosen, it redirects to a new page. Eg. Random Choice Generator spits out "John Smith" -- Browser redirects to "John Smith's" character card. Below I have the PHP script for the random choice (I think, I'm a total PHP noob!), I'm just having trouble with the redirect part. Any light you could shed would be much appreciated! And also, I do believe this code is just for one choice, would I just need to duplicate it x amount of times? <?php $stats[1] = 0; $stats[2] = 0; $stats[3] = 0; for ($i = 0; $i < 1000; $i++){ $choice = rand(1,3); if (!$i){ echo "First random choice: $choice<BR>\n"; } $stats[$choice]++; } reset($stats); while (list($num, $count) = each($stats)){ echo "$num: $count<BR>\n"; } ?> Thanks, Jack Quote Link to comment https://forums.phpfreaks.com/topic/247993-random-choice-redirect/ Share on other sites More sharing options...
gizmola Posted September 28, 2011 Share Posted September 28, 2011 header("Location: url_to_goto"); exit(); However, you can't redirect once you've sent output, because at that point the page is complete. You can either have a link or use some javascript to redirect, but you might want to think through your design better. Quote Link to comment https://forums.phpfreaks.com/topic/247993-random-choice-redirect/#findComment-1273382 Share on other sites More sharing options...
michaelburt Posted September 28, 2011 Share Posted September 28, 2011 To be a little more exact, you'd have to use: <?php header("Location: ..."); ?> Before the <html> tag, or before anything is outputted with PHP. Quote Link to comment https://forums.phpfreaks.com/topic/247993-random-choice-redirect/#findComment-1273383 Share on other sites More sharing options...
jackmcnally Posted September 28, 2011 Author Share Posted September 28, 2011 Hmmm.. Seems like I'm stuck! Would anyone be able to show me the modifications I would need to make on the above script in order to get it to work the way I want it to? Eg. I end up putting in 10 random choices. If the script selects number 8, the page is automatically redirected to numbereight.php Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/247993-random-choice-redirect/#findComment-1273388 Share on other sites More sharing options...
michaelburt Posted September 28, 2011 Share Posted September 28, 2011 Try: <?php //this section must be before anything is outputted to the page $random = array("rand1", "John", "Paul", "test", "foo", "michael", "Jon", "flower", "book", "wall", "floor", "another_test"); $blank = array(); if (isset($_POST["choice"])) { header("Location: ".$blank[$_POST["choice"]]); } $i = 0; while (count($blank) < { $val = $random[$i]; if (!in_array($val, $blank)) array_push($blank, $val); else break; $i++; } ?><form action="<?php echo basename(__FILE__); ?>" method="post"> <select name="choice"> <?php for ($i = 0; $i < 8; $i++) { echo " <option>$i</option>\n"; } ?> </select> <input type="submit" value="Send" /> </form> Note that this is completely untested. Quote Link to comment https://forums.phpfreaks.com/topic/247993-random-choice-redirect/#findComment-1273392 Share on other sites More sharing options...
jackmcnally Posted September 28, 2011 Author Share Posted September 28, 2011 Thank you so much, Michael, that looks as though it may work! Quick question, where would I put the redirect for each individual random choice? Thanks, Jack **EDIT** Just tried it, quick question: what is the purpose of the 1-7 dropdown. When I select one and click send nothing happens. I really do thank you - all - so much for your hope and ask you to excuse my PHP ignorance, I have just started a course, but want to finish this part soon! You have all helped me so much and I excuse myself if I have come across as needy! Quote Link to comment https://forums.phpfreaks.com/topic/247993-random-choice-redirect/#findComment-1273401 Share on other sites More sharing options...
michaelburt Posted September 28, 2011 Share Posted September 28, 2011 Sorry, it was a late night when I coded this. Try: <?php //this section must be before anything is outputted to the page $random = array("rand1", "John", "Paul", "test", "foo", "michael", "Jon", "flower", "book", "wall", "floor", "another_test"); $blank = array(); if (isset($_POST["choice"])) { while (count($blank) < { $val = $random[rand(0, count($random)-1)]; if (!in_array($val, $blank)) array_push($blank, $val); else break; } header("Location: ".$blank[$_POST["choice"]]); } ?><form action="<?php echo basename(__FILE__); ?>" method="post"> <select name="choice"> <?php for ($i = 0; $i < 8; $i++) { echo " <option>$i</option>\n"; } ?> </select> <input type="submit" value="Send" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/247993-random-choice-redirect/#findComment-1273495 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.