Jump to content

?=page Links


curbspaget
 Share

Recommended Posts

It is all executed on the index.php file. Just think of page as the variable and downloads as the value of the variable page. You might've done something in Javascript that if a user didn't type what they were suppose to then they got an error (maybe an alert box), otherwise you carried on with the rest of the code. In PHP, you will have to use the $_GET global array which is like the page variable from the above example, and give it a value that make the rest of the script work, otherwise the user will have an "error" (give them an error message, redirect them to another page on your site, etc.).

<?php  if ($_GET['page']=='downloads') {    echo 'Downloads Page';  }  elseif ($_GET['page']=='whatever') {    echo 'Whatever page.';  }  else {    echo '<a href="index.php?page=downloads">Go here</a> or <a href="index.php?page=whatever">Here</a>';  }?>

The code above will do whatever the value of page is (just change page from $_GET['page'] to use something else), If the URL is index.php?page=downloads then you will see Downloads Page in the browser, Whatever page if it is index.php?page=whatever, or two links that allow the user to click on to bring them to those pages.

Link to comment
Share on other sites

The code works for me, make sure you entered the correct URL, have apache/php (or XAMPP/WAMP whichever one) to be able to execute PHP, a hosting company that allows for php to be executed.Try viewing the source in your browser, make sure it doesn't have the php code in it.

Link to comment
Share on other sites

Curbspaget - it is working. What you're doing wrong is not stopping the script running after outputting the result if you put ?page=download so it continues to go to the end of the php script and then run all the html afterwards.Try stopping at the end of one of the else statements egelse {echo '<a href="index.php?page=downloads">Go here</a> or <a href="index.php?page=whatever">Here</a>';EXIT;} Then load the page with ?page=download and it will stop after outputting the above echo line.

Link to comment
Share on other sites

No. It does what you want it to do.Downloads is just a value that has been assigned to the variable called page. It could mean anything really. Its what happens when the link is clicked on that matters.For example, suppose you had a shopping website with a database of all your products on the server. You could have a picture of a CD player on the front page and a link to display its details. The link would be something like productdetails.php?prodID=32.When the link is clicked it calls up the page called productdetails.php - that page would then get the value that has been passed to it by the variable prodID (in this case 32). What would happen then is that the script on the page would look up the details in the database for the product with code 32 (in this case the CD player) and display the details on screen.This means that you could pass any value to productdetails.php and it would display the details for the product with that ID. For this to work of course you need to have your pages on a server that can run php scripts.In the case you described you could have more than one link to the same page but sending different values eg<a href="index.php?page=downloads">Downloads</a><a href="index.php?page=help">Help</a>when clicked the page called index php runs and finds what value has been passed to it. What happens then depends on the value.Try starting from scratch with an empty page, and get the hang of it before you try to incorporate it into a bigger page.Say this page is your index.php:<?php$requestedPage = $_GET['page']; //this finds the bit at the end of index.php?page=valueif ($requestedPage=="email") {print "Our email address is xxx@xxx.xxx";} // if ?page=email this bit will runif ($requestedPage=="help") {print "You can find help on this page.";} // if ?page=help this bit will runprint "<br />This is the index page. The options are: ";print "<a href='index.php?page=email'>Email Us</a> or <a href='index.php?page=help'>Get Help</a>";// the two lines above will always run because we have done nothing to stop them?>The first time you call this page there will be no value for page so neither of the two if statements will run. You will only get This is the index page.But if you click on either of the links index.php will be called again but this time with page=email or page=help so the corresponding if statement will run.

Link to comment
Share on other sites

Try these two. I think one of them will give you an error, because I'm not 100% sure which one of them is right. I just wrote them and it's early so

<?php$p = $_GET['page']; // ?page=$ex = "php"; // File extension$folder = "includes"; // Folder where the files you want to include is$main = "main"; // Main page$error = "404"; // 404 error pageif (empty($p)) {include("$folder/$main.$ex");}if (file_exists("$folder/$p.$ex")) {include("$folder/$p.$ex");}if (!$file_exists("$folder/$p.$ex")) {include("$folder/$error.$ex");}?>

<?php$p = $_GET['page']; // ?page=$ex = "php"; // File extension$folder = "includes"; // Folder where the files you want to include is$main = "main"; // Main page$error = "404"; // 404 error pageif (empty($p)) {include("$folder/$main.$ex");}else if (file_exists("$folder/$p.$ex")) {include("$folder/$p.$ex");}else if (!$file_exists("$folder/$p.$ex")) {include("$folder/$error.$ex");}?>

The links would be like this:

<a href="?page=main">Home</a><a href="?page=portfolio">Portfolio</a><a href="?page=about">About</a>

And the files you want to include would be something like this (through an FTP program)

ftp.yoursite.com includes Here are all the files that's being included.
If the URL is ?page=portfolioit would try to include http://yoursite.com/includes/portfolio.phpDo you understand now?Good luck :)
Link to comment
Share on other sites

Alright, none of this works.

<?php$requestedPage = $_GET['page']; //this finds the bit at the end of index.php?page=valueif ($requestedPage=="email") {print "Our email address is xxx@xxx.xxx";} // if ?page=email this bit will runif ($requestedPage=="help") {print "You can find help on this page.";} // if ?page=help this bit will runprint "<br />This is the index page. The options are: ";print "<a href='index.php?page=email'>Email Us</a> or <a href='index.php?page=help'>Get Help</a>";// the two lines above will always run because we have done nothing to stop them?>

That's close, but all it does is show the text 'You can find help on this page.' or 'Our email address is xxx@xxx.xxx'. Is that all its supposed to do?

Link to comment
Share on other sites

Its up to you to decide what you want it do. If you want the user to go to a download page when they click on a link then it would be a lot easier not to bother with php and just have a straightforward link to a new page called downloads.The php and variables and values is only useful when you need to generate different info based on what the user has selected.Have a look at a website I'm working on at the moment www.wooden-heart.co.uk. Down the left hand side you will see two categories called Furniture and Storage. I you move you mouse over both it shows that they both call the same page called stock.php. The page stock.php does not know yet what information it needs to pull from the database. That is where the ?kind=Furniture or ?kind=Storage comes into play. When you click, the page called stock.php finds out whether you want Furniture or Storage and fetches the items from the database to display.

Link to comment
Share on other sites

I think it's easier to keep the same layout on all pages with one indexfile and then include different files depending on the querystring (page=...)It's a bit trickier to have different files include one (or several) file(s) in every file to have same layout on all pages...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...