Jump to: navigation, search

PHP File Template with MySQL

From w3cyberlearnings

Contents

PHP File Template with MySQL

File template and MySQL is another way you can implement the dynamic template by using the information store in the Database. The template is flexible so do the database.

Note

Please check the feedback tutorial, this tutorial uses the feedback table. PHP Feedback Form

Template File(user_form.txt)

<table cellspacing="1">
 <tr>
   <td>Name:</td><td>{{NAME}}</td>
 </tr>
 <tr>
   <td>Email:</td><td>{{EMAIL}}</td>
 </tr>
 <tr>
   <td>Tel:</td><td>{{PHONE}}</td>
 </tr>
 <tr>
   <td></td><td>Description:</td>
 </tr>
 <tr>
   <td colspan="2">{{DESCRIPTION}}</td> 
 </tr>
</table>

MySQL Table

  CREATE TABLE feedback
  (
     id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
     name VARCHAR(50),
     tel VARCHAR(15),
     email VARCHAR(50),
     description VARCHAR(255)
  );

Insert Records

id name   tel              email          description
------------------------------------------------------------------------------------
1  Pauk   318-444-4444 [email protected]  I like this website.
3  Kinaoma 314-544-2333 [email protected] Great website to start up with!!
2 Yiling  331-444-2222 [email protected]  IT is a great website to share knowledge

Example 1

<?php

define('HOST', 'localhost');
define('USER', 'root');
define('PASS', 'yeething');
define('DBNAME', 'my_db');

$connection = mysql_connect(HOST, USER, PASS);

// define an array template
$array_template = array('{{NAME}}', '{{EMAIL}}', '{{PHONE}}', '{{DESCRIPTION}}');

if (!$connection) {
	die("can not connect to the server!");
} else {
	$result_db = mysql_select_db(DBNAME);

	if (!$result_db) {
		die("The " . DBNAME . "database could not be selected");
	} else {
		$sql = "SELECT * FROM feedback";

		$result_db = @mysql_query($sql);
		if (!$result_db) {
			die('<p>error update!<br />' .
					  'Error: ' . mysql_error() . '</p>');
		} else {

			while ($user = mysql_fetch_array($result_db)) {
				$array_user = array(
					 $user['name'],
					 $user['email'],
					 $user['tel'],
					 $user['description']);

				if (file_exists('user_form.txt')) {
					$filecontent = file_get_contents('user_form.txt');
					$fcontent = str_replace(
					$array_template, $array_user, $filecontent);
					echo ($fcontent);
				}
			}
		}
	}
}
mysql_close($connection);

Output

Name:	Pauk
Email:	[email protected]
Tel:	            318-444-4444      
Description:
I like this website.
Name:	Yiling
Email:	[email protected]
Tel:	            331-444-2222      
Description:
IT is a great website to share knowledge
Name:	Kinaoma
Email:	[email protected]
Tel:	            314-544-2333      
Description:
Great website to start up with!!

Related Links


  1. PHP File Linux vs. Windows
  2. PHP File Information
  3. PHP File Create
  4. PHP File Close
  5. PHP File Read
  6. PHP File Read Reverse
  1. PHP File Write
  2. PHP File Write to end or append to
  3. PHP File Write at Beginning
  4. PHP File Write at specific location
  5. PHP File Truncate
  6. PHP File Replace a specific word
  1. PHP File Replace a specific word with associative array
  2. PHP File Copy File
  3. PHP File Copy Reverse
  4. PHP File Search within file
  5. PHP File Delete File
  6. PHP File Template
  1. PHP File Email Template
  2. PHP File Template with MySQL
  3. PHP File Access with MySQL
Navigation
Web
SQL
MISC
References