Jump to content

Recommended Posts

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.  :)

Link to comment
https://forums.phpfreaks.com/topic/223172-url-help/
Share on other sites

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>" 
?>

Link to comment
https://forums.phpfreaks.com/topic/223172-url-help/#findComment-1153722
Share on other sites

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>" 
?>

Link to comment
https://forums.phpfreaks.com/topic/223172-url-help/#findComment-1153730
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/223172-url-help/#findComment-1153754
Share on other sites

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>";
?>

Link to comment
https://forums.phpfreaks.com/topic/223172-url-help/#findComment-1153774
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.