Dynamic pages in PHP. PHP. Dynamic page creation Index php creation
We only talked about static pages, that is, those that, no matter how the user interacts with them, always remain unchanged, and in order for their content or design to change, the page owner needs to edit the code manually.
Dynamic pages and what they need
In addition to static, there are also dynamic pages. Most of them are on the Internet now. Information in them is loaded from external sources, such as a database or other files. The content and formatting of such pages may change depending on the user's actions. To edit dynamic sites, it is not necessary to interfere with their code - it is enough to change the content in a specially designed file or database, which, by the way, is also a file, only structured in a certain way.
To create dynamic websites, just HTML and CSS is not enough. It also uses programming languages, as well as databases and query languages for them. Most often, modern dynamic sites use HTML, CSS, PHP, JavaScript, SQL in their work. The first two abbreviations are already familiar to you firsthand, SQL is used to access databases, JavaScript is a client-side language whose commands are processed by the browser and are often used to show you all sorts of beauty like curtains or smoothly opening photos, but PHP is a server-side programming language , which works, among other things, with the content of the site and makes it dynamic, we will come into contact with it today.
An example of using the include command
In the previous article, I talked about the block layout of the site and cited the simplest page as an example (document index.html and the file associated with it style.css).
Now we will split the document index.html into several files, each of which will contain its own part of the page, which will help to further separate the code, improve the structure of the template and, in fact, make the page dynamic. For this purpose, we will use PHP language, or rather, only one of its directives - the function include(), which includes one file within another.
1. Change the permission of the file created in the article about block layout index With .html on .php to name the document index.php. File type .PHP indicates to the server that the document was written or uses inserts in the programming language of the same name.
2. In the folder with the page, create a directory blocks.
3. All supporting information (top, bottom, navigation and sidebar site) we will take out into separate files, which we will place in the folder blocks.
So, create four files in the blocks directory: header.php, navigation.php, sidebar.php And footer.php. Fill in the files with code.
header (site header)
4. Check the template folder structure. The files must be in the root index.php, style.css and directory blocks.
Folder structure blocks should be like this.
5. In file index.php delete the existing code and write a new one:
Main page content
In the browser, the index.php file looks exactly the same as before, but the structure of the template has completely changed. We will talk about what happened later, and now we will answer the question about the mysterious commands of the form .
Like HTML code, PHP code also has its own start and end designation. So, you need to start a PHP insert with the command , and end with the line ?> . Between these commands, the main code is written. In our case, this is just one command - include.
Function include() inserts code from another file into the file, making it possible to store different parts of the page in different documents, thereby reliably separating them from each other.
As a result of the actions performed, we received dynamic page index.php, parts of which are loaded from different files. Thanks to this, you can create other pages by loading auxiliary elements from the folder files in the same way. blocks.
This approach is good because if you want to change, say, the name of a menu item on a site of 20-30 pages, then in a template with a newly created structure, you will need to make changes to only one file - blocks/navigation.php, and the menu will change immediately on all pages in which it is included. If the site were static, then to change the name of one menu item, you would have to make changes to every from 20-30 pages. The difference is obvious.
Precautions must be taken. Because the resource can turn out to be extremely dynamic. That and look, bite!
PHP and HTML
Now these two disciplines are almost connected together. Their tandem is the basis on which the "life" of most of the Internet is built. Of course, other server-side languages (Perl, ASP.NET) are also used in conjunction with html. But their prevalence on the World Wide Web, compared to PHP, is purely episodic.
According to statistics, the architecture of most resources on the Internet is built on the basis of php and html.
For many beginners, the relationship between these technologies seems incomprehensible. On the one hand, static html , which is interpreted by browsers on the client side. On the other hand, it is a programming language that is processed on the server. Therefore, before writing a site in php from scratch, we will give a simple example of the interaction of these two technologies:
- Let's create a new file with php extension;
- Let's put the following code there:
- Let's save the file on the local server and open it in the browser. Then click on the right mouse button and context menu choose the item " View html page».
As you can see in the screenshot, the lineis not displayed in the html code of the page. Both in the browser and in the code, only the returned date and time are visible. This means that the script was processed on the server side. Therefore, creating a site in php is fundamentally different from writing simple html pages.
Dynamic site
There are two main types of websites today:
- Static - created only on the basis of html . Such sites do not change their content in response to user actions. Of course, a static resource can respond to events and user actions. But the client-side implementation of page dynamism has a narrow scope, limited by the capabilities of Java Script.
The Java Script code runs in the browser on the client side.
- Dynamic - capable of changing their state and content - html pages of dynamic sites are formed on the fly at the time of code execution in response to a user request sent by him from the browser to the server. Most often, page generation on the server side occurs using code written in php .
A dynamic php site consists of the following files:
- index.php is the main project file;
- Templates - include the structure of a particular part of the page ( caps, basement, main body);
- CSS files - store all the style descriptions of the resource.
In addition, the site project may consist of files that store the code of php functions and methods. And also include a database.
In most CMS, the content source for filling pages dynamically generated on the server side is the database. The most commonly used database is MySQL.
How to write a website in PHP
To understand how a site is created in php, let's look at a practical example. Of course, much of it is simplified, but the entire mechanism of work and the stages of creation are preserved.
I have an html site with the following structure and design:
His code: