Jump to content

Random Choice -- Redirect


jackmcnally

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.