A little PHP question

Associate
Joined
3 Aug 2006
Posts
610
Location
London, UK
Is there any way of creating a hyperlink that simply changes a variable in a php file?

I am trying to make a site which just loads in the required content when a link is clicked but it just displays the one mentioned last in the code

My current code:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">

<html>

   <head> 
      <title>London Dude</title>
      <link rel="stylesheet" type="text/css" href="mainstyles.css" />
<?php
	$index = "http://127.0.0.1/london-dude%201.1/frontpage.php";				//Index Page
	$about = "http://127.0.0.1/london-dude%201.1/about.php";					//About Page
	$page = $index								//Default Page
?>
   </head>

   <body>
		<div class="topbar">
			<img alt="London Dude" src="images/logo.jpg" />
		</div>
		<div class="menubar">
		<a href="<?php
	$page = $index
?>"><img alt="Home |" src="images/home.jpg" /></a>
		<a href="<?php
	$page = $about
?>"><img alt="Home |" src="images/about.jpg" /></a>
		</div>
		<center>
			<div class="body">
				<?php
	include $page;
?>
			</div>
		</center>
   </body>
</html>
 
seems to be a step in the right direction but all it seems to be doing is displaying the page url, not the content of the page
 
now that I have gone back to "include($page);" it will display the contents of the page, but now it doesn't switch between pages when I click the link
 
<a href="<?php
$page = $index
?>"><img alt="Home |" src="images/home.jpg" /></a>

what the heck is that supposed to do?

this should work

PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">

<html>

   <head> 
      <title>London Dude</title>
      <link rel="stylesheet" type="text/css" href="mainstyles.css" />
<?php
	$index = "http://127.0.0.1/london-dude%201.1/frontpage.php";				//Index Page
	$about = "http://127.0.0.1/london-dude%201.1/about.php";					//About Page
	
if(!isset($_GET['p']) || $_GET['p'] == 'home') { 
$page = $index;		//Default Page
} else if($_GET['p'] == 'about') {
$page = $about;
}
?>
   </head>

   <body>
		<div class="topbar">
			<img alt="London Dude" src="images/logo.jpg" />
		</div>
		<div class="menubar">
		<a href="index.php?p=home"><img alt="Home |" src="images/home.jpg" /></a>
		<a href="index.php?p=about"><img alt="Home |" src="images/about.jpg" /></a>
		</div>
		<center>
			<div class="body">
				<?php
	include($page);
?>
			</div>
		</center>
   </body>
</html>
 
Clarkey said:
<a href="<?php
$page = $index
?>"><img alt="Home |" src="images/home.jpg" /></a>

what the heck is that supposed to do?

that's searching for an image and linking to the homepage, but it's doing it wrong. there's a couple of things you can do here...have all your calls in a conditional statement:
Code:
$page = stripslashes($_GET['page']);
$page .= ".htm";

if ($page == '.htm')
{
require_once('main.htm')
}
else if (!file_exists($page))
{
require_once('404.htm')
}
else
{
require_once("$page");
}

something like that should work...just put it where you want the page to be included and you'll be fine...then have all your links as <a href="something.php?page=blah"> and you'll be set.

hope that's what you were after
 
Last edited:
Cheers for the help. Using Clarkey's method now, works perfectly.

Sic's method lookes a little complicated but thanks for the help anyway
 
Back
Top Bottom