jsk1gcc Posted January 2, 2011 Share Posted January 2, 2011 Hi, I've been researching and all I can find is information on how to change URLS not how to redirect a user if statement matches input. I don't think this makes any difference but the $result comes from a drop down box. (if that code is needed, let me know) I would like something like this: if $result="Swinging Ship" GO TO PAGE <a href="Swing.php">Ultra Rides</a> if $result="Roller Coaster" GOT TO PAGE <a href="Roller.php">Ultra Rides</a> if $result="Ice Blast" GO TO PAGE <a href="Ice.php">Ultra Rides</a> I need the syntax for this, should I use a loop, if else, while maybe? not sure what to use. thanks for any help on this. Quote Link to comment https://forums.phpfreaks.com/topic/223172-url-help/ Share on other sites More sharing options...
QuickOldCar Posted January 2, 2011 Share Posted January 2, 2011 Firstly I saw you had a got to page there mistake. I'm guessing you would like the href link displayed? This should work for you. <?php //added these to test them, uncomment one //$result = "Swinging Ship"; //$result = "Roller Coaster"; //$result = "Ice Blast"; if ($result == "Swinging Ship") { $go_to_page = "Swing.php"; } elseif ($result == "Roller Coaster") { $go_to_page = "Roller.php"; } elseif ($result == "Ice Blast") { $go_to_page = "Ice.php"; } else { $go_to_page = "./"; //a default location to go? $result = "HOME"; } echo "<a href='$go_to_page'>$result</a>" ?> Quote Link to comment https://forums.phpfreaks.com/topic/223172-url-help/#findComment-1153722 Share on other sites More sharing options...
JD* Posted January 2, 2011 Share Posted January 2, 2011 You can also use a switch conditional which makes things a little cleaner: <?php //added these to test them, uncomment one //$result = "Swinging Ship"; //$result = "Roller Coaster"; //$result = "Ice Blast"; switch($result) { case "Swinging Ship": $go_to_page = "Swing.php"; break; case "Roller Coaster": $go_to_page = "Roller.php"; break; case "Ice Blast": $go_to_page = "Ice.php"; break; default: $go_to_page = "./"; //a default location to go? break; } echo "<a href='$go_to_page'>$result</a>" ?> Quote Link to comment https://forums.phpfreaks.com/topic/223172-url-help/#findComment-1153730 Share on other sites More sharing options...
kenrbnsn Posted January 2, 2011 Share Posted January 2, 2011 And, yet another way of doing this ... Use an array: <?php //added these to test them, uncomment one //$result = "Swinging Ship"; //$result = "Roller Coaster"; //$result = "Ice Blast"; $urls = array('Swinging Ship'=>'Swing','Roller Coaster'=>'Roller','Ice Blast'=>'Ice'); $go_to_page = (array_key_exists($result,$urls))?$urls[$result] . '.php':'default.php'; echo "<a href='$go_to_page'>$result</a>"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/223172-url-help/#findComment-1153754 Share on other sites More sharing options...
QuickOldCar Posted January 2, 2011 Share Posted January 2, 2011 Yeah is many ways, the funny thing is I missed my semicolon for the echo on the href, but it still worked in this simple example. Here's the fixed. <?php //added these to test them, uncomment one //$result = "Swinging Ship"; //$result = "Roller Coaster"; //$result = "Ice Blast"; if ($result == "Swinging Ship") { $go_to_page = "Swing.php"; } elseif ($result == "Roller Coaster") { $go_to_page = "Roller.php"; } elseif ($result == "Ice Blast") { $go_to_page = "Ice.php"; } else { $go_to_page = "./"; //a default location to go? $result = "HOME"; } echo "<a href='$go_to_page'>$result</a>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/223172-url-help/#findComment-1153774 Share on other sites More sharing options...
Anti-Moronic Posted January 2, 2011 Share Posted January 2, 2011 I vote ken. Quote Link to comment https://forums.phpfreaks.com/topic/223172-url-help/#findComment-1153785 Share on other sites More sharing options...
jsk1gcc Posted January 2, 2011 Author Share Posted January 2, 2011 Thankyou x Quote Link to comment https://forums.phpfreaks.com/topic/223172-url-help/#findComment-1153810 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.