lyall Posted February 9, 2008 Share Posted February 9, 2008 I am trying to use the these functions to create a PHP navigation system, I am using ajax to load the content instantly without having to refresh the whole page but I don't think that is casuing any problems... This is the piece of script I am using: <?php switch($id) { default: include('index.php'); break; case "1": include('contact.php'); break; case "2": include('blah2.php'); break; case "3": include('blah3.php'); break; case "4": include('blah4.php'); break; case "5": include('blah5.php'); break; case "6": include('blah6.php'); break; case "7": include('blah7.php'); } ?> And I am using index.php?id=contact to set the variable so that the page loads. This is the error message I get when attempting to run my script: Notice: Undefined variable: id in /home/fhlinux159/v/vyb3.co.uk/user/htdocs/vyb308/index.php on line 200 Notice: Undefined variable: id in /home/fhlinux159/v/vyb3.co.uk/user/htdocs/vyb308/index.php on line 201 Notice: Undefined variable: id in /home/fhlinux159/v/vyb3.co.uk/user/htdocs/vyb308/index.php on line 202 Notice: Undefined variable: id in /home/fhlinux159/v/vyb3.co.uk/user/htdocs/vyb308/index.php on line 203 Notice: Undefined variable: id in /home/fhlinux159/v/vyb3.co.uk/user/htdocs/vyb308/index.php on line 204 Notice: Undefined variable: id in /home/fhlinux159/v/vyb3.co.uk/user/htdocs/vyb308/index.php on line 205 Notice: Undefined variable: id in /home/fhlinux159/v/vyb3.co.uk/user/htdocs/vyb308/index.php on line 206 I don't understand. I know this is problem a pretty noobish problem to you coders, im still trying to grasp PHP and I apppreciate your help. Quote Link to comment Share on other sites More sharing options...
nethnet Posted February 9, 2008 Share Posted February 9, 2008 Where is $id being set? Quote Link to comment Share on other sites More sharing options...
lyall Posted February 9, 2008 Author Share Posted February 9, 2008 to the url I presume Quote Link to comment Share on other sites More sharing options...
MishieMoo Posted February 9, 2008 Share Posted February 9, 2008 Okay well the script doesn't know what $id is. That's the problem. Before the switch($id) put this: if(isset($_GET['id'])) $id=$_GET['id']; else $id=''; $_GET will grab the id from the url. The next problem is with your cases. The case definition has to match what is set for $id. If you have the cases as numbers (1, 2, 3..etc) and then finds 'contact' in the id then it won't know what to do. Either change id= to set as a number, or change the cases to reflect what will be in the url. If they don't match then the default will print. This is the most basic thing, though. You probably want to check what is being passed through the url and filter it, though. Quote Link to comment Share on other sites More sharing options...
resago Posted February 9, 2008 Share Posted February 9, 2008 never assume or use global variables. Quote Link to comment Share on other sites More sharing options...
lyall Posted February 9, 2008 Author Share Posted February 9, 2008 Thanks everyone, and thanks to you MishieMoo your method has worked :-) many thanks. Quote Link to comment Share on other sites More sharing options...
ypkumar Posted February 9, 2008 Share Posted February 9, 2008 I dont know if this is helpful or not.. but here is my code. index.php <html> <title>Switch Demo</title> <body> <div style="margin: 25px auto auto 365px;"> <a href="index.php">Home</a> <a href="index.php?page=news">News</a> <a href="index.php?page=misc">Misc</a> <a href="index.php?page=download">Downloads</a> <a href="index.php?page=contact">Contact</a> </div> </body> </html> <?php if (isset($_GET['page'])) { $pg = $_GET['page']; if (file_exists($pg.'.php')) { echo"<div style=\"margin: 100px auto auto 400px;\">"; include ($pg.'.php'); } elseif (!file_exists($pg.'.php')) { echo"<div style=\"margin: 100px auto auto 400px;\">"; echo 'Page you are requesting doesn´t exist'; } } ?> news.php <?php echo "This Is News Page (2)"; ?> misc.php <?php echo "This Is Misc Page (3)"; ?> download.php <?php echo "This Is Download Page (4)"; ?> contact.php <?php echo "This Is Contact Page (5)"; ?> these 5 pages must be placed in same directory or their paths must be edited to reflect the change. Its almost like ajax but without any xmlhttpreq.. or js involved, just a simple include trick This worked for me. Hope it works for you :-) with best regards, kumar. Quote Link to comment Share on other sites More sharing options...
Aureole Posted February 9, 2008 Share Posted February 9, 2008 I'd do this... <?php $id = ( isset( $_GET['id'] ) && is_numeric( $_GET['id'] ) && !empty( $_GET['id'] ) ) ? $_GET['id'] : 1; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.