Page 1 of 1

Help with IF (Control Structure) in simple function...

Posted: Fri Dec 31, 2004 6:02 pm
by bonovox
I use this bit to mask my links, passing the id=1 variable from a previous page to this one in a link go.php?id=1, but everytime no matter the value, it goes directly to the last IF... taking me (in this example) to google...

<?php

if ($id = "1"){$link = "http://altavista.com";}
if ($id = "2"){$link = "http://yahoo.com";}
if ($id = "3"){$link = "http://google.com";}

header("Location: $link");
exit();

?>

any light? I know it might be a stupid question, but I am "no php guru..." so please... HELP!

Posted: Fri Dec 31, 2004 6:20 pm
by potsed

Code: Select all

if ($id=='1'){ $link = "http://altavista.com";}
elseif ($id == "2"){$link = "http://yahoo.com";}
elseif ($id == "3"){$link = "http://google.com";}
although it may b better to use a switch...

Code: Select all

switch($id){
    case '1':
    $link = "http://altavista.com";
    break;

    case '2':
    $link = "http://yahoo.com";
    break;

    case '3':
    $link = "http://google.com";
    break;
}
or you could use an array to hold the links..

Code: Select all

$links = array("http://altavista.com", "http://yahoo.com", "http://google.com");
$link = (array_key_exists($id, $links)) ? $links[$id] : "http://point/to/default/link";

Posted: Fri Dec 31, 2004 6:48 pm
by bonovox
Thanks and a Happy New Year... I'll work on your code, but so far I'm getting undefined variable errors... Thanx again...

Posted: Fri Dec 31, 2004 7:00 pm
by rehfeld
you prob wanna use something like this

Code: Select all

if (isset($_GET['id'])) {
    if ($_GET['id'] == '1') $link = 'foo';
    // etc...
}
have a read up on "register globals"

Posted: Fri Dec 31, 2004 7:05 pm
by bonovox
Thanks a lot... and Happy New Year... It works fine... As I said I don't know a whole lot of PHP... just getting the hang of it... Thanks again.

Posted: Sat Jan 01, 2005 12:54 am
by n00b Saibot
ur original code was just fine. only that u used single = instead of ==. single = is meant for assigning a value so in effect ur code was doing right thing. just changing = to == will do what u wanted it to do in first place

Posted: Sat Jan 01, 2005 3:02 am
by potsed
n00b Saibot wrote:ur original code was just fine. only that u used single = instead of ==. single = is meant for assigning a value so in effect ur code was doing right thing. just changing = to == will do what u wanted it to do in first place
While this is true... the code was also putting an extra load on the server (not much on 3 lines i know, but every bit helps... ) by having to process every one of the if statements, which is why an if-elseif or switch control structure is a better option than just if's in this type of situation

Posted: Sat Jan 01, 2005 4:54 am
by n00b Saibot
ya, right. i only wanted to tell him exactly where he missed it. thats it. if even i'm starting out a new thing i wont like to ehar that. i would only like to know where i went wrong:)
u r right bt that info and thinking comes later on when we r able to put together a working code.

Posted: Sat Jan 01, 2005 6:01 am
by timvw
ah well, here is how i would write it ;)

Code: Select all

$link = array();
$link[1] = "http://altavista.com";
$link[2] = "http://yahoo.com";
$link[3] = "http://google.com";

$id = isset($_GET['id']) ? $_GET['id'] : 1; 
header('Location: ' . $link[$id]);

Posted: Sat Jan 01, 2005 6:46 am
by n00b Saibot
shortest route home. eh! :)