Jump to content

URL HELP


jsk1gcc

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.