Jump to content

PHP navigation help Switch method


lyall

Recommended Posts

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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