Problem with my links

Hi,

i am building a few links e.g home, about ect… and once clicked, it should display an echo statement. Eventually it will be used to display images, but for testing purposes, i just want to make sure they work fine. I am using a switch statement for this but everytime i click on my links i get the following error


Notice: Undefined index: home in C:\\wamp\\www\\Shanghai2010\\index.php on line 13

Notice: Undefined index: shanghaizoo in C:\\wamp\\www\\Shanghai2010\\index.php on line 14


....

   <div id="navigation">
			    <ul>
				   <li><a href="index.php?link=home">Home</a></li>
				   <li><a href="index.php?link=shanghaizoo">Shangai Zoo</a></li>
				   <li><a href="index.php?link=chengtemple">Cheng Huang Temple</a></li>
			    </ul>
		     </div>
...


index.php


$home = $_GET['home']; // error indicating here
$shanghaizoo = $_GET['shanghaizoo']; // error indicating here

switch( isset($_GET['link']) )
{
    case $home:
        echo 'You clicked on you the home link';
        break;
    case $shanghaizoo:
        echo 'Yuo clicked on the shanghai zoo link';
        break;
    
    
}

 

can somebody kindly help me out please because at the moment i simply want to ensure that what ever link is clicked, the appropriate message is displayed.

Thanks

Get rid of these 2 lines (like I said you don’t have ‘home’ and ‘shanghaizoo’ in your query string (and thus in your $_GET array):


$home = $_GET['home']; // error indicating here
$shanghaizoo = $_GET['shanghaizoo']; // error indicating here

And look very carefully at my code and yours, there is a big difference, which is causing the new errors. Can you see it?

you don’t have variables called ‘home’ and ‘shanghaizoo’ in the query string, only ‘link’.
Change your code to


switch( isset($_GET['link']) )
{
    case 'home':
        echo 'You clicked on you the home link';
        break;
    case 'shanghaizoo':
        echo 'Yuo clicked on the shanghai zoo link';
        break;
}

thanks for the quick reply, i actually did try that before but i also got errors,


Notice: Undefined index: home in C:\\wamp\\www\\Shanghai2010\\index.php on line 13

Notice: Undefined index: shanghaizoo in C:\\wamp\\www\\Shanghai2010\\index.php on line 14

Notice: Use of undefined constant home - assumed 'home' in C:\\wamp\\www\\Shanghai2010\\index.php on line 18

Notice: Use of undefined constant shanghaizoo - assumed 'shanghaizoo' in C:\\wamp\\www\\Shanghai2010\\index.php on line 21



switch( isset($_GET['link']) )
{
    case home: // error indicating here
        echo 'You clicked on you the home link';
        break;
    case shanghaizoo: // error indicating here
        echo 'Yuo clicked on the shanghai zoo link';
        break;
    
    
}



I know what was wrong:

switch( isset($_GET[‘link’]) )

i removed the isset which now works.

I have removed the 2 lines you requested, so i now have the following:


switch( isset($_GET['link']) )
{
    case home: // error indicating here
        echo 'You clicked on you the home link';
        break;
    case shanghaizoo: // error indicating here
        echo 'Yuo clicked on the shanghai zoo link';
        break;
    
    
}

which results in getting 2 lines of errors of:


Notice: Use of undefined constant home - assumed 'home' in C:\\wamp\\www\\Shanghai2010\\index.php on line 16

Notice: Use of undefined constant shanghaizoo - assumed 'shanghaizoo' in C:\\wamp\\www\\Shanghai2010\\index.php on line 19

Was the difference that i had the 2 variables declared above and also i had used them in my switch statement, which you did not. Is that what you are referring to?

UPDATE: I know what the difference is, you had ’ ’ in the case statement ie. ‘home’ which i did not, that is the difference right?

Sorry but no cigar i’m afraid, this time when i run it it does not output the desired echo statement in that it seems to always go into the first outputing:


echo 'You clicked on you the home link';

regardless which link i click. I’ve also slightly modified it to this:


if(isset($_GET['link']))
{
   switch( isset($_GET['link']) )
    {
        case "home": 
            echo 'You clicked on you the home link';
            break;
        case "shanghaizoo": 
            echo 'Yuo clicked on the shanghai zoo link';
            break;    
    } 
    
    
}
 

i also tried using single ’ ’ quotes but no luck. Another thing i did was by adding a + print_r($_GET[‘link’]); in my code:


if(isset($_GET['link']))
{
   switch( isset($_GET['link']) )
    {
        case "home": 
            echo 'You clicked on you the home link' + print_r($_GET['link']);
            break;
        case "shanghaizoo": 
            echo 'Yuo clicked on the shanghai zoo link' + print_r($_GET['link']);
            break;    
    } 
    
    
}
 

and when i ran it would output the following e.g shanghaizoo1 or chengtemple1 so it seems there could be something wrong with the case block, but i’m not sure. Any suggestions?

Exactly :slight_smile:

‘home’ is a string
home is a constant (which you didn’t define in your script, that’s what the error tells you)