This series of the videos explains how to create Photo Gallery using PHP, MySQL, ImageMagic and jQuery. Using PHP you will be able to add, modify and populate data stored on the MySQL Database and display records of all images on the page. ImageMagic will help us to resize and crop images to the relevant dimentions, while jQuery will provide the support in the presentation of the gallery. The last two chapters are dedicated to adding the earlier created gallery to our Content Management System. Over 4 hours of videos!!!

PHP tutorials

 
  • PHP url parameter and page include
  • PHP url parameter and page include

I´ve been asked several times how to solve this problem.
Let´s say you have folder which stores files with the content of the pages.
You also have an index.php page from which you send requests via url to display a relevant page. See demo.

This tutorial explains one of the approaches this can be done.

Within your site´s root folder create a folder called pages. Inside of this folder create five pages and provide them with some content:

home.php
about_us.php
services.php
testimonials.php
contact_us.php

In the site´s root folder create file and give it a name of index.php.
Open index.php in any text editor and copy and paste the following content:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Url parameter</title>
<style type="text/css">
<!--
body {
font-family:Arial, Helvetica, sans-serif;
font-size:10pt;
}
-->
</style>
</head>
<body>
<p style="display:block;padding:0px 0px 20px 0px;"><a href="?pg=home" title="Home">Home</a> | <a href="?pg=about_us" title="About us">About us</a> | <a href="?pg=services" title="Services">Services</a> | <a href="?pg=testimonials" title="Testimonials">Testimonials</a> | <a href="?pg=contact_us" title="Contact us">Contact us</a></p>

</body>
</html>

This piece of code contains the standard html page structure, some styling and navigation which will send a parameter via url. After the closing </p> tag type:

<?php

if (isset($_GET['pg']) && $_GET['pg'] != "") {

$pg = $_GET['pg'];

if (file_exists('pages/'.$pg.'.php')) {

@include ('pages/'.$pg.'.php');

} elseif (!file_exists('pages/'.$pg.'.php')) {

echo 'Page you are requesting doesn´t exist';

}

} else {

@include ('pages/home.php');

}

?>

This simple code does the entire work.
First condition checks if there is a pg parameter passed via url and if it is not empty by using:

if (isset($_GET['pg']) && $_GET['pg'] !="") {

if condition isn´t true (else) then it includes the default, home page:

} else {

@include ('pages/home.php');

}

If condition is true than parameter is assigned to a variable:

$pg = $_GET['pg'];

and another condition checks whether the file with the value of the parameter exists in the pages folder:

if (file_exists('pages/'.$pg.'.php')) {

if it does then this file is included on the page by using:

@include ('pages/'.$pg.'.php');

if file doesn´t exist then the message is displayed

} elseif (!file_exists('pages/'.$pg.'.php')) {

echo 'Page you are requesting doesn´t exist';

}

This is one of many ways you can use include function to populate information from external files by selecting them using url parameter.

 
  • Thank you for subscribing to our newsletter.
  • To complete registration please click on the link in the email which has been sent to you.
  • Invalid first name
  • Invalid last name
  • Invalid email address

Website Design Company | Website Design Company Sussex | Website Design Company Chichester | Website Design Packages | Tutorials Resources | Articles | Contact
© Web Design Tutorials – Sebastian Sulinski