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 to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Create simple Website with PHP

Last accessed pages

  1. The Mastery of Love (7440)
  2. Get Mime Type of file or string content in PHP (6230)
  3. Countdown Timer with starting time added into a form (11533)
  4. Disable button and Enable it after specified time (17533)
  5. JpGraph - Create Graph, Charts, Plots in PHP (3933)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (72)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (64)
  5. CSS3 2D transforms (46)