curbspaget Posted April 27, 2007 Share Posted April 27, 2007 I searched the forum for this, but couldn't find it. How do you make links like:'http://www.ultimatepronoun.com/index.php?page=downloads'I'm mostly looking at the index.php?page=downloads part. How do you make a link do that, and do o that page? Link to comment Share on other sites More sharing options...
zppblood Posted April 28, 2007 Share Posted April 28, 2007 You will need to use the $_GET global array.An example would be: <?php if ($_GET['page']=='downloads') { echo 'This is my page for downloads.'; }?> Link to comment Share on other sites More sharing options...
curbspaget Posted April 28, 2007 Author Share Posted April 28, 2007 <?php if ($_GET['page']=='downloads') { echo 'This is my page for downloads.'; }?> so something like:<a href='<?php if ($_GET['page']=='downloads') {echo 'This is my page for downloads.'; }'?>>link</a>Or what? Link to comment Share on other sites More sharing options...
zppblood Posted April 28, 2007 Share Posted April 28, 2007 If you wanted a page inside an anchor tag like that, just enter the regular address of it.<a href="http://www.ultimatepronoun.com/index.php?page=downloads">This is the text user will see for the link.</a>If it's your site that you wanted to link, just use index.php?page=downloads. Link to comment Share on other sites More sharing options...
curbspaget Posted April 28, 2007 Author Share Posted April 28, 2007 But what is downloads? A directory? What type of file? php? html? css? Link to comment Share on other sites More sharing options...
zppblood Posted April 28, 2007 Share Posted April 28, 2007 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 More sharing options...
curbspaget Posted April 28, 2007 Author Share Posted April 28, 2007 Doesn't work.It changes the address bar to index.php?page=downloads, but the page won't budge. Link to comment Share on other sites More sharing options...
zppblood Posted April 28, 2007 Share Posted April 28, 2007 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 More sharing options...
murfitUK Posted April 28, 2007 Share Posted April 28, 2007 Are your pages on a server that can run php scripts? If so, the above will work. Link to comment Share on other sites More sharing options...
curbspaget Posted April 28, 2007 Author Share Posted April 28, 2007 There's no PHP code.Look at the .phps file and try and see what I'm doing wrong.http://rl6.org/~curbsy/times/index.phps Link to comment Share on other sites More sharing options...
zppblood Posted April 28, 2007 Share Posted April 28, 2007 Try changing the extension .phps to .phpIs one of your scripts using the highlight_file() or show_source()? I can see your source code in the browser itself. Link to comment Share on other sites More sharing options...
curbspaget Posted April 28, 2007 Author Share Posted April 28, 2007 I know. That's what .phps is. It's so you can see the source code to debug it.Real page is here: http://rl6.org/~curbsy/times/index.php Link to comment Share on other sites More sharing options...
zppblood Posted April 28, 2007 Share Posted April 28, 2007 I know. That's what .phps is. It's so you can see the source code to debug it.Real page is here: http://rl6.org/~curbsy/times/index.php Wow never knew that, you learn something everyday. *Edit* Doesn't seem to work with me. Link to comment Share on other sites More sharing options...
zppblood Posted April 28, 2007 Share Posted April 28, 2007 You might be using an old version of PHP, and might have to use $HTTP_GET_VARS instead of $_GET. Link to comment Share on other sites More sharing options...
murfitUK Posted April 28, 2007 Share Posted April 28, 2007 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 More sharing options...
curbspaget Posted April 28, 2007 Author Share Posted April 28, 2007 Is this supposed to actually sent you to a different page, or something else? Link to comment Share on other sites More sharing options...
murfitUK Posted April 28, 2007 Share Posted April 28, 2007 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 More sharing options...
Anders Moen Posted April 28, 2007 Share Posted April 28, 2007 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 More sharing options...
curbspaget Posted April 28, 2007 Author Share Posted April 28, 2007 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 More sharing options...
Mr_CHISOL Posted April 28, 2007 Share Posted April 28, 2007 Yup.. that's all that that code do... nothing more, nothing less. Link to comment Share on other sites More sharing options...
murfitUK Posted April 28, 2007 Share Posted April 28, 2007 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 More sharing options...
curbspaget Posted April 28, 2007 Author Share Posted April 28, 2007 Ok, I see. I just think it makes cool links and that's why I want to find out how to do it. Link to comment Share on other sites More sharing options...
Mr_CHISOL Posted April 28, 2007 Share Posted April 28, 2007 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.