Create "dynamic" pages in PHP

Status
Not open for further replies.

Jake

Paragon
Joined
Nov 14, 2010
Messages
2,264
Reaction score
2
FP$
246
Many have asked me via Skype how to create "dynamic" pages in PHP. What I'm talking about is a URL such as: "www.example.com/post.php?id=47294729" or "www.example.info/page.php?name=aboutme".

Doing this is very simple. Here's how.


Firstly, you'll want to check to make sure the GET variable is "set". We'll use a PHP function called "isset" (see? isset -> is set) to check this.

Code:
<?php
  if (isset($_GET['page'])) {
    //do something
  } else {
    echo "Sorry, there was no page provided. I have no clue what you're looking for!";
  }
?>

So, what this will do is look in the GET array (basically what's IN the URL) for that variable. So, if there is a page variable set in the URL, then that will go to "//do something" and otherwise it will show that message.

Now, to make a "dynamic" page with this, you'll want to check what page they're looking for.

This is how you'd do that:
Code:
<?php
  if (isset($_GET['page'])) {
    if ($_GET['page'] == "about") {
      echo "Some info all about me!";
    } else {
      echo "This is the home page!";
    }
  } else {
    echo "Sorry, there was no page provided. I have no clue what you're looking for!";
  }
?>

Now, what the code does after the above is head inside of the isset condition and check what that variable is actually set to. If it's set to "about" (http://example.info/page.php?page=about), then it will show "Some info all about me!", and if there is nothing set or something unrecognized set (http://example.info/page.php?page.php or http://example.info/page.php?page=4gpreds), then it will show "This is the home page!"

Thanks for reading and I hope this helped out in some way 🙂 :great:
 
Great tutorial Jake.

I'm working on learning PHP at the moment so I'll be sure to have this bookmarked. 🙂
 
This is useful although a much better way to do this would be to use switch statements.

Code:
switch ($_GET['page']) {

    case 'about':
	
	case 'About':
	
        //Do Stuff
		
    break;
	
    case 'contact':
	
	case 'Contact':
	
		//Do Stuff
		
    break;
	
	default:
	
		//Do Stuff
		
	break;
	
}
 
PixelBanter said:
This is useful although a much better way to do this would be to use switch statements.

Code:
switch ($_GET['page']) {

 case 'about':
 
 case 'About':
 
 //Do Stuff
 
 break;
 
 case 'contact':
 
 case 'Contact':
 
 //Do Stuff
 
 break;
 
 default:
 
 //Do Stuff
 
 break;
 
}

Indeed. I made this tutorial for newbies. The way I would much rather do it would be someway like this:

Code:
<?php
    include "pages.php";
    $page = new pages();
    switch($page->getPage()) {
        case 'home':
            $page->renderHome();
            break;
        default:
            $page->render404();
    }
?>

I haven't used PHP in awhile, so excuse an syntactical errors.
 
Jake said:
PixelBanter said:
This is useful although a much better way to do this would be to use switch statements.

Code:
switch ($_GET['page']) {

 case 'about':
 
 case 'About':
 
 //Do Stuff
 
 break;
 
 case 'contact':
 
 case 'Contact':
 
 //Do Stuff
 
 break;
 
 default:
 
 //Do Stuff
 
 break;
 
}

Indeed. I made this tutorial for newbies. The way I would much rather do it would be someway like this:

Code:
<?php
 include "pages.php";
 $page = new pages();
 switch($page->getPage()) {
 case 'home':
 $page->renderHome();
 break;
 default:
 $page->render404();
 }
?>

I haven't used PHP in awhile, so excuse an syntactical errors.

Or use a controller.

Code:
<?php



$actions = array(

    'login' => 'Login',

    'logout' => 'Login',

    'create' => 'CreateAd',

    'viewads' => 'ViewAds',

    'delete' => 'Delete',

    'edit' => 'Edit',

    'usercp' => 'UserCP',

    'register' => 'Register',

    'view' => 'ViewAd',

    'vote' => 'Vote'

);

$action_model_name = 'Main';

if (isset($_GET['action']) && array_key_exists($_GET['action'], $actions))

    $action_model_name = $actions[$_GET['action']];



require_once('classes/Model.php');

require_once('classes/View.php');

require_once('classes/ViewRegistry.php');

require_once('models/Toplist.php');

require_once('models/' . $action_model_name . '.php');



$view_registry = new ViewRegistry();

$view = new View('toplist', $view_registry);

$model = new Toplist($view);

$model->handleAction();

$view->setView($model->getViewFile());



$action_view = new View($action_model_name, new ViewRegistry());

$action_model = new $action_model_name($action_view);

$action_model->handleAction();

$action_view->setView($action_model->getViewFile());



$view_registry->setVariable('action', $action_view);

$view->export();



?>
 
its better to include header and footer in php to create a dynamic , attractive and easily updatable 😛<br /><br />-- 30 Mar 2014, 12:29 --<br /><br />mean to say header.php and footer. php
 
20CentsPayout said:
its better to include header and footer in php to create a dynamic , attractive and easily updatable 😛

-- 30 Mar 2014, 12:29 --

mean to say header.php and footer. php

Yes, but thats a totally different subject.
 
Okay, well as I said earlier: this is for newbies. Let's not confuse them with MVC concepts or advanced PHP. This isn't meant for production or extremely professional websites. Please make your own thread if you want to contribute those ideas.
 
Why teach a noobie the wrong way? It only forms a habit that will, in the future, need to be broken.
 
PixelBanter said:
Why teach a noobie the wrong way? It only forms a habit that will, in the future, need to be broken.

A newbie will not be worried about learning advanced concepts or philosophies such as MVC or how they should use a framework. This simply turns them away from the language and from programming in total. I am simply teaching simple control flow by using basic conditionals to form a basic GET array check and show different content based on what is returned. Now, if you please, stop on my thread. If you truly feel that newbies MUST know the super-advanced techniques, make your own thread.
 
Status
Not open for further replies.
Back
Top Bottom