PHP Multi-page website building guide: Create a clear folder structure to separate different pages. Use an HTML template (template.php) as the basis for each page. Add content to the homepage (index.php). Create corresponding files and customize content for other pages such as contact.php and about.php. Configure a web server (such as Apache) as needed to handle .php files. Use PHP's database connection function to obtain data from the database and display it on the page.
PHP is a popular server-side programming language that can be used to build interactive websites for the web. This article will guide you step-by-step through creating a multi-page website using PHP.
Create the following folder structure:
- index.php - contact.php - about.php
Every page will use the same HTML template as a base. Create a file called template.php
and add the following code:
<!DOCTYPE html> <html> <head> <title>多页面 PHP 网站</title> </head> <body> <!-- 页面内容 --> </body> </html>
in the index.php
file Add the following code:
<?php include "template.php"; ?> <h1>主页</h1> <!-- 页面内容 -->
Create something like this for other pages like contact.php
and about.php
file and customize the page content as needed.
<?php include "template.php"; ?> <h1>联系我们</h1> <!-- 联系信息 -->
Configure your web server (such as Apache) to handle .php
files. For example, add the following content to the Apache configuration file:
<Directory /var/www/my_website> AllowOverride All </Directory> <FilesMatch "\.php$"> SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://php" </FilesMatch>
Create a page named user_data.php
, which displays a set of Table of user data. Use the following code:
<?php include "template.php"; ?> <h1>用户数据</h1> <?php // 从数据库获取用户数据 $users = getUsers(); // 创建 HTML 表格 echo "<table border='1'>"; echo "<tr><th>ID</th><th>名称</th><th>电子邮件</th></tr>"; foreach ($users as $user) { echo "<tr><td>{$user['id']}</td><td>{$user['name']}</td><td>{$user['email']}</td></tr>"; } echo "</table>"; ?>
Now you know how to build a multi-page website using PHP. By leveraging PHP's server-side capabilities, you can create interactive, information-rich websites that meet a variety of needs.
The above is the detailed content of How to build a multi-page website using PHP. For more information, please follow other related articles on the PHP Chinese website!