Jump to content

[SOLVED] Creating Webpages with the PHP GET Method


topflight

Recommended Posts

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.

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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;
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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";
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.