Search code examples
phphtmlhref

Changing links by priority - PHP / HTML


I am currently working on a school project and appreciate your help.

HTML Code:

<a href="change-1.php">LINK 1</a>
<a href="change-2.php">LINK 2</a>
<a href="change-3.php">LINK 3</a>

Now as I am relatively new to PHP, I need clue wehere to start or which topics are relevant for my solution. I have already searched the Forum here and with google, but unforutenly, didn't find anything, that fits my needs.

What I need, is a PHP-Code, that contains 6 href Links and every Link has a priority. So every time somebody clicks on "LINK 1" on the html file, he gets directed to the PHP-File "change-1.php", where one of the six href Links get activated.

PHP CODE "change-1.php":

<?php 

if (condition) {
    echo "http://www.someotherwebsite-1.com";
}
elseif (condition) {
    echo "http://www.someotherwebsite-2.com";
}
elseif (condition) {
    echo "http://www.someotherwebsite-3.com";
}
elseif (condition) {
    echo "http://www.someotherwebsite-4.com";
}
elseif (condition) {
    echo "http://www.someotherwebsite-5.com";
}
elseif (condition) {
    echo "http://www.someotherwebsite-6.com";
}

?>

Solution

  • Perhaps this should help

    <a href="change.php?change=1">LINK 1</a>
    <a href="change.php?change=2">LINK 2</a>
    <a href="change.php?change=3">LINK 3</a>
    

    Just create one change.php file

    //to get change
    
    <?php 
    
    if ($_GET['change'] == 1) {
        echo "http://www.someotherwebsite-1.com";
    }
    elseif ($_GET['change'] == 2) {
        echo "http://www.someotherwebsite-2.com";
    }
    elseif ($_GET['change'] == 3) {
        echo "http://www.someotherwebsite-3.com";
    }
    elseif ($_GET['change'] == 4) {
        echo "http://www.someotherwebsite-4.com";
    }
    elseif ($_GET['change'] == 5) {
        echo "http://www.someotherwebsite-5.com";
    }
    elseif ($_GET['change'] == 6) {
        echo "http://www.someotherwebsite-6.com";
    }
    
    ?>
    

    Now this is the basic idea. You can manipulate the data however you want.

    I hope you got the concept.