I am developing a PHP/Mysql site.
The site consists of static html templates.
Data is dynamically populated into these templates with php require_once
For example this is simplified version of the Home page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Home</title>
</head>
<body>
<?php require_once $_SERVER['DOCUMENT_ROOT']."/includes/header.php"; ?>
<?php require_once $_SERVER['DOCUMENT_ROOT']."/includes/connection.php"; ?>
<?php require_once $_SERVER['DOCUMENT_ROOT']."/includes/sql.php"; ?>
<?php require_once $_SERVER['DOCUMENT_ROOT']."/includes/content.php"; ?>
<?php require_once $_SERVER['DOCUMENT_ROOT']."/includes/footer.php"; ?>
</body
The Header and Footer are themselves static so are included as is.
However to display the right content I establish a connection(connection.php) to the database, query it(sql.php) and then echo it(content.php).
I repeat this for all other pages on the site each page referencing a modified sql.php.
All this works, But I know it’s not very efficient.
My question is how can I restructure my files/code so that hopefully I will end-up with one file that contains all my sql queries and somehow how “choose” the right query to execute depending on which page that requested it.
Thanks for your help.
Use functions in your files. So you can easely call functions from differend files like
http://php.net/manual/en/language.functions.php