Php-mysql Course

In this tutorial you can learn how to quickly create a simple website in PHP, with different title, meta description, and content for each page.
We create a file: index.php with PHP instructions to define page data, HTML code, and CSS style for template (the whole code for this file is presented bellow).
- The page URL will have the form:   index.php?pg=page_name
- The content of each page (which can contains HTML code) is stored into a directory (here named "pages"), in ".htm" files, with the name: page_nme.htm

1. First we create the PHP code. To have different Title and Meta-tags in each page, we set an Array with Title, Description, and Keywords associated with the name of each page, "index" for Home page.
$pgdata['page_name'] = array(
 'title'=>'Title of the page',
 'description'=>'Here add the page description',
 'keywords'=>'meta keywords, of the, page'
);
2. Then we set a variable with the name of the page. If there is $_GET['pg'], we get the name of the page from URL, otherwise we set the default name "index"; and get its data from Array.
$pgname = isset($_GET['pg']) ? trim(strip_tags($_GET['pg'])) : 'index';
- We use strip_tags() function to remove posible tags added by visitor in URL, and trim() to delete whitespaces from beginning and end of the name.
3. With this variable we get data for the current /accessed web page, like this for Title.
$title = $pgdata[$pgname]['title'];
4. Then we create the HTML code with CSS styles, a menu and the content (see the whole code bellow). In the HTML code we add the Meta data selected from array, like this for Title:
<title><?php echo $title; ?></title>
And get its content saved in ".htm" file, using:   file_get_contents('directory/page_name.htm');:
<article id="article">
<?php echo file_get_contents('pages/'. $pgname. '.htm'); ?>
</article>

- To can use properly diacritics, or other less common characters, all the files must be saved with UTF-8 encoding. In the PHP code we add this header:
header('Content-type: text/html; charset=utf-8');
And, the HTML code must have this meta:
<meta charset="utf-8" />

Here's the code to create a simple website with PHP.

Code for index.php file

<?php
 /* Create simple Website with PHP - https://coursesweb.net/php-mysql/ */

// create an array with data for title, and meta, for each page
$pgdata = array();
$pgdata['index'] = array(
 'title'=>'Title for Home page',
 'description'=>'Here add the description for Home page',
 'keywords'=>'meta keywords, for, home page'
);
$pgdata['about_me'] = array(
 'title'=>'Title for About Me page',
 'description'=>'Description for About Me, https://coursesweb.net',
 'keywords'=>'about me, https://coursesweb.net'
);
$pgdata['images'] = array(
 'title'=>'Title for Images',
 'description'=>'Here add the description for the page with images',
 'keywords'=>'images, pictures, photo'
);

// set the page name
$pgname = isset($_GET['pg']) ? trim(strip_tags($_GET['pg'])) : 'index';

// get title, and meta data for current /accessed page
$title = $pgdata[$pgname]['title'];
$description = $pgdata[$pgname]['description'];
$keywords = $pgdata[$pgname]['keywords'];

// set header for utf-8 encode
header('Content-type: text/html; charset=utf-8');
?>
<!doctype html>
<html>
<head>
 <meta charset="utf-8" />
 <title><?php echo $title; ?></title>
 <meta name="description" content="<?php echo $description; ?>" />
 <meta name="keywords" content="<?php echo $keywords; ?>" />
 <!--[if IE]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
 <style><!--
 body {
 margin:0;
 text-align:center;
 padding:0 1em;
 }
 header, footer, section, aside, nav, article { display: block; }
 #posts{
 position:relative;
 width:99%;
 margin:0.5em auto;
 background:#fdfefe;
 }
 #menu {
 float:left;
 width:15em;
 margin:0 auto;
 background:#f8f9fe;
 border:1px solid blue;
 text-align:left;
 }
 #menu li a:hover {
 text-decoration:none;
 color:#01da02;
 }
 #article {
 margin:0 1em 0 16em;
 background:#efeffe;
 border:1px solid #01da02;
 padding:0.2em 0.4em;
 }
 #footer {
 clear:both;
 position:relative;
 background:#edfeed;
 border:1px solid #dada01;
 width:99%;
 margin:2em auto 0.5em auto;
 }
 --></style>
</head>
<body>

<header id="header">
 <h1><?php echo $title; ?></h1>
</header>

<section id="posts">
 <nav id="menu">
 <ul>
 <li><a href="index.php" title="Home page">Home</a></li>
 <li><a href="index.php?pg=about_me" title="About Me">About Me</a></li>
 <li><a href="index.php?pg=images" title="Images">Images</a></li>
 </ul>
 </nav>
 <article id="article"><?php echo file_get_contents('pages/'. $pgname. '.htm'); ?></article>
</section>

<footer id="footer">
 <p>From: <a href="https://coursesweb.net/php-mysql/" title="Free PHP-MySQL course">PHP-MySQL Course</a></p>
</footer>

</body>
</html>

- This is just an example for a website with three pages: Home, About Me, Images; and the CSS code only to arrange a little the Menu and content area. The files: "index.htm", "about_me.htm", and "images.htm", with the content you want for each one, must be saved into a directory named "pages".
You can create more pages in the web site by setting $pgdata['page_name'] array with their data, and create the content in "page_name.htm" file, in "pages" directory.

- To Download the files with this example, click: Create simple Website with PHP.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used in <table> to create table header cell?
<thead> <th> <td>
<table><tr>
  <th>Title 1</th>
  <th>Title 2</th>
</tr></table>
Which CSS property sets the distance between lines?
line-height word-spacing margin
.some_class {
  line-height: 150%;
}
Which function opens a new browser window.
alert() confirm() open()
document.getElementById("id_button").onclick = function(){
  window.open("http://coursesweb.net/");
}
Indicate the PHP function that returns an array with names of the files and folders inside a directory.
mkdir() scandir() readdir()
$ar_dir = scandir("dir_name");
var_export($ar_dir);
Create simple Website with PHP

Last accessed pages

  1. JavaScript strip_tags and stripslashes (8825)
  2. Date and Time in ActionScript 3 (10086)
  3. Bind Tool and Control Points (4430)
  4. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142539)
  5. Clear Canvas Context (8116)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (542)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (63)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)