topflight Posted December 28, 2008 Share Posted December 28, 2008 I have been wondering how can I create pages using the GET METHOD. I have made a content template in Photoshop and now I want to use that same template for all my pages. All I want it to do is get the content and put it on the template. Example: 127.0.0./?page=contactus 127.0.0./?page=jointus 127.0.0./?page=news and I want to have all that on the same temaplate but only display the proper page. For instance when the user click contact us I want it to direct the user to 127.0.0./?page=contactus and show the contact page. I have seen this done on a lot of sites but still confused on how to do it. I have use the GET METHOD for creating users profiles. But now I would like to use it for creating content pages. Also another question is does the links above must have.php at the end?(i.e 127.0.0./?page=news.php or I can leave it like it is above? I know it sounds confusing but please provide as much detail and example as possible thanks. Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted December 28, 2008 Share Posted December 28, 2008 Okay, I'll explain it a bit. Basically you have a singular page which has all the template and navigational links. You'd call this page index.php, obviously. Now, where the content goes you throw in some PHP. Something like this: if(!isset($_GET['page'])){ //This line checks if the URL has ?page=something in it $page = "home"; //If it doesn't then we set $page to a default page, in this case, home }else{ $page = $_GET['page']; //If it is set then we set $page to whatever is in the URL. } if(file_exists("pages/".$page.".php")){ //This line here will check to see if a file in the folder pages exists. The file is the variable $page include("pages/".$page.".php"); //If the file does it exist it means the user requested a page that you have. So we include that page into the content area. }else{ include("pages/404.php"); //If it doesn't exist we show them a 404 page or even the home page. } Then basically, all you do is create your pages in the 'pages' directory. Say you want a Contact Us page and you called it contactus.php in the 'pages' directory. In your browser you'd type: www.mysite.com/?page=contactus Then it would include the file contactus.php. So any page that is included ONLY has content, you don't need the layout or anything. Hope that makes some sense. Good luck. Quote Link to comment Share on other sites More sharing options...
topflight Posted December 29, 2008 Author Share Posted December 29, 2008 Thanks any more advice Quote Link to comment Share on other sites More sharing options...
Loki88 Posted December 29, 2008 Share Posted December 29, 2008 <?php if (isset($_GET['p'])) { if (!in_array($_GET['p'], array('index', 'test', 'other'))) { $p = 'index'; } else { $p = $_GET['p']; } } else { $p = 'index'; } switch($p) { case 'index': default: //main directory that includes links to other pages break; case 'test': //another page break; case 'other': //another page break; } ?> Quote Link to comment Share on other sites More sharing options...
topflight Posted January 5, 2009 Author Share Posted January 5, 2009 I am still a little confused. So all of the pages will be running from the index.php. ProjectFear and Loki88 have given me a basic understanding of it. But may somebody please got in a step by step process please or some more advise becuase I am lost in creating this I tried and nothing happens. Also I understand the following.... 1st I mus create an index.php page which will hold the news system,welcome message,newest member,login center and etc.... 2nd I am confused (but I do know that if a user click join the url should say mysite.com/index.php?p=join) once again much help is appreicated Also do I have to create a whole new template in photoshop becuase the index.php template is going to be way different then my regular pages templates. Quote Link to comment Share on other sites More sharing options...
T-Bird Posted January 5, 2009 Share Posted January 5, 2009 I would strongly caution you against using a single page for an entire site worth of content. It makes it much more difficult for google, msn, and yahoo to accurately crawl your site, keeping you from gaining much ground in search engines. Plus it's over complicating the situation. If you're simply looking to make a template system I'd recommend making your content the base page and the html the linked part. Quote Link to comment Share on other sites More sharing options...
ram4nd Posted January 5, 2009 Share Posted January 5, 2009 Basically you import content to index.php file from other file. Witch file that you have to _GET from url. And I would do it with funcion. I could upload some example code if necessary. Quote Link to comment Share on other sites More sharing options...
topflight Posted January 5, 2009 Author Share Posted January 5, 2009 Thanks! but may somebody please upload or show me some more code that does that? Quote Link to comment Share on other sites More sharing options...
Maq Posted January 5, 2009 Share Posted January 5, 2009 Basically you have your "main" page that calls your other pages depending on what the user clicked on. So you have main.php And like ProjectFear already suggested, you have 3 other pages: contactus.php jointus.php news.php So you would grab the page by doing: $page = (isset($_GET['page'])) ? $_GET['page'] . "php" : "main.php"; Then proceed with the ifelse statements... Quote Link to comment Share on other sites More sharing options...
ram4nd Posted January 5, 2009 Share Posted January 5, 2009 You include other file in this file - index.php <?php function content() { if(is_numeric($_GET['id'])) switch($_GET['id']) { case 1: require_once "link1.php"; break; case 2: require_once "link2.php"; break; } else require_once "link1.php"; } ?><html><body><table...... <?php content(); ?> ......</body></html> And in the other files you can put any content you want. Then you can make links to import that content. Like this: <a href="index.php?id=1">Main Page</a> <a href="index.php?id=2">Something</a> Quote Link to comment Share on other sites More sharing options...
topflight Posted January 6, 2009 Author Share Posted January 6, 2009 So I have added this line at the top of my index code. <?php $page = (isset($_GET['page'])) ? $_GET['page'] . "php" : "main.php"; ?> and I have created a hyper link with the following: <p><a href="?page=roster.php">Click Here for Roster</a></p> and nothing happens all I get is the same page again. please help thanks Quote Link to comment Share on other sites More sharing options...
Maq Posted January 6, 2009 Share Posted January 6, 2009 A switch may be better but w/e. Where I echo out those variables you should be calling your external files (require_once). You can get the file names by doing: $call_page = $page . ".php"; So if the user clicks on the link "?page=roster" you would do: And all you have to do is require $call_page. if($page == "main") { echo "main"; } elseif($page == "contactus") { echo "contact"; } elseif($page == "joinus") { echo "joinus"; } elseif($page == "news") { echo "news"; } Quote Link to comment Share on other sites More sharing options...
topflight Posted January 6, 2009 Author Share Posted January 6, 2009 So Now the top of my index page says this: <?php $call_page = $page . ".php"; if($page == "main") { echo "main"; } elseif($page == "contactus") { echo "contact"; } elseif($page == "joinus") { echo "joinus"; } elseif($page == "news") { echo "news"; } ?> and I click roster and nothing happens it just keep reloading the index.php instead of the roster page I wonder what is wrong? Quote Link to comment Share on other sites More sharing options...
Maq Posted January 6, 2009 Share Posted January 6, 2009 $page = $_GET['page']; $call_page = $page . ".php"; echo "This is the page you should call! " . $call_page; if($page == "main") { require_once("$call_page"); } elseif($page == "contactus") { require_once("$call_page"); } elseif($page == "joinus") { require("$call_page"); } elseif($page == "news") { require_once("$call_page"); } ?> Main Roster Join Us News Quote Link to comment Share on other sites More sharing options...
topflight Posted January 6, 2009 Author Share Posted January 6, 2009 Thanks Maq, I am still confused when you say call the page how should I do that? Please provide a little bit more detail thanks. Quote Link to comment Share on other sites More sharing options...
Maq Posted January 6, 2009 Share Posted January 6, 2009 I thought the whole point of this was to call in different pages? I have use the GET METHOD for creating users profiles. But now I would like to use it for creating content pages. It's almost the same as the user profile pages but instead you're going to require a page. So you have main.php (the one we've been coding), contactus.php, joinus.php, and news.php. So if the user clicks on the link with the variables ?page=contactus, we will know what page they are trying to get to. Then we take the page variable from the URL and use that to figure which page the user wants to see. In this case, they want to see contactus.php. So you should have another PHP file names contactus.php that we can call (require_once) that will show the contact content. Does this make any sense? Quote Link to comment Share on other sites More sharing options...
topflight Posted January 6, 2009 Author Share Posted January 6, 2009 yes makes a lot if since but when will I be require_once? The main.php? Quote Link to comment Share on other sites More sharing options...
Maq Posted January 6, 2009 Share Posted January 6, 2009 The require_once belongs in the main.php. So main.php is sort of a template. Depending on what the user clicks on it will load in another page. require_once() will load whatever page you want in that spot. Try it out. Quote Link to comment Share on other sites More sharing options...
topflight Posted January 6, 2009 Author Share Posted January 6, 2009 So what should go in the parenthesis in the require once code? Quote Link to comment Share on other sites More sharing options...
Maq Posted January 6, 2009 Share Posted January 6, 2009 I already have it in there for you. It's the files you want to call in for the content. You need to make 3 more files in the same directory: contactus.php joinus.php news.php Each contains specific content. So when the user clicks the link it dynamically calls these pages and it will load in main.php (the file we have been working on). Quote Link to comment Share on other sites More sharing options...
topflight Posted January 6, 2009 Author Share Posted January 6, 2009 So where do I put this code at and how come it is not displaying the roster when I click on roster. thanks. If possible I know I have ask this before but may you please give me a step by step process and put it in dummy language for me please thanks. Because I am a little confused. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted January 6, 2009 Share Posted January 6, 2009 So where do I put this code at and how come it is not displaying the roster when I click on roster. thanks. If possible I know I have ask this before but may you please give me a step by step process and put it in dummy language for me please thanks. Because I am a little confused. Your index page should look something like... index.php: <?php $page = strtolower($_GET['page']); // <-- this is the name of the page, forced to be all lowercase, but now you need to append '.php' to the end $importedPage = $page . '.php'; // <-- we just added the '.php' extension to the end if($page == "main" || $page == "contactus" || $page == "joinus" || $page == "news") { require_once($importedPage); // <-- and now we just imported that file } ?> To break down the process further, consider the following URL: www.somesite.com/index.php?page=contactus The part we're interested in is the 'contactus' portion of the link. That's supposed to serve up the Contact Us content to the user. So, that little chunk of text is obtained by the first line of code: $page = strtolower($_GET['page']); $page is a variable - a named container that can hold any kind of value. In this case, it holds the text 'contactus' (without the quotes). We turn it all into lowercase letters by using the strtolower() function. That way, we don't have to check if someone accidentally put a capital letter in the link. Next, we create another variable - $importedPage - and store within it the actual file name that the script should look for: $importedPage = $page . '.php'; The . between $page and '.php' is an operator, much like + or -. In PHP, the . appends (adds) text, so we're literally adding the file extension to the page name. Why did we create a new variable for this? We could've simply added the file extension to $page by writing: $page = $page . '.php'; /* This is the same as */ $page .= '.php'; But, we need to make sure that whatever values for the content that comes into the script are legit, so we need to test the incoming page names with what we expect them to be. This is most easily done by keeping $page clean of the file extension and testing it against the names of the acceptable pages. This is done here: if($page == "main" || $page == "contactus" || $page == "joinus" || $page == "news") { /* ... */ } This is a conditional. It literally says "If $page equals main, OR $page equals contactus, OR...." then execute the code between the brackets. That code is to deliver the content of the appropriate file to the user, and is done by simply writing: require_once($importedPage); That's about as step-by-step as it gets. Quote Link to comment Share on other sites More sharing options...
Maq Posted January 6, 2009 Share Posted January 6, 2009 Nightslyr has broke down the basics of the entire concept of how to create a web page with the get method. He has optimized some of the things I said along with some little tricks to make things easier. If you have more questions don't hesitate to post. Quote Link to comment Share on other sites More sharing options...
topflight Posted January 7, 2009 Author Share Posted January 7, 2009 So I would put all of the code on the index.php page if so where at? at the top bottom middle or etc...? And also when it says if page == contactus it is looking for contactus.php? Also another thing I plan to create a whole new template for the contact us page and join us and etc... will that template mess up the site. And do I need to put any code on the contactus.php page? Thanks I apologizes for the lack of knowledge of php lol but thanks. Quote Link to comment Share on other sites More sharing options...
topflight Posted January 7, 2009 Author Share Posted January 7, 2009 Also I have tried that code on my index.php page and nothing happens when I click on http://127.0.0.1/?page=roster.php it just take me back to my index page and doesn't displays the content on the roster page. 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.