PHP(Hypertext Preprocessor)是一种通用开源脚本语言,主要用于服务器端开发。PHP可以嵌入HTML代码中,使得网页具有动态交互性。它广泛应用于Web开发,如网站、Web应用、API接口等。
blog/
├── index.php
├── config.php
├── db.php
├── models/
│ └── post.php
├── controllers/
│ └── postController.php
├── views/
│ ├── index.php
│ └── post.php
└── public/
└── css/
└── style.css
index.php
<?php
require 'config.php';
require 'db.php';
require 'controllers/postController.php';
$postController = new PostController($db);
$posts = $postController->getAllPosts();
include 'views/index.php';
?>
config.php
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'blog');
?>
db.php
<?php
require 'config.php';
try {
$pdo = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
return $pdo;
?>
models/post.php
<?php
class Post {
private $db;
private $id;
private $title;
private $content;
public function __construct($db, $id = null) {
$this->db = $db;
if ($id) {
$this->load($id);
}
}
private function load($id) {
$stmt = $this->db->prepare("SELECT * FROM posts WHERE id = :id");
$stmt->execute([':id' => $id]);
$data = $stmt->fetch(PDO::FETCH_ASSOC);
$this->id = $data['id'];
$this->title = $data['title'];
$this->content = $data['content'];
}
public function save() {
if ($this->id) {
$stmt = $this->db->prepare("UPDATE posts SET title = :title, content = :content WHERE id = :id");
} else {
$stmt = $this->db->prepare("INSERT INTO posts (title, content) VALUES (:title, :content)");
}
$stmt->execute([
':title' => $this->title,
':content' => $this->content,
':id' => $this->id
]);
}
public function delete() {
$stmt = $this->db->prepare("DELETE FROM posts WHERE id = :id");
$stmt->execute([':id' => $this->id]);
}
public function toArray() {
return [
'id' => $this->id,
'title' => $this->title,
'content' => $this->content
];
}
}
?>
controllers/postController.php
<?php
class PostController {
private $db;
public function __construct($db) {
$this->db = $db;
}
public function getAllPosts() {
$stmt = $this->db->query("SELECT * FROM posts");
$posts = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$posts[] = new Post($this->db, $row['id']);
}
return $posts;
}
public function getPostById($id) {
return new Post($this->db, $id);
}
public function createPost($title, $content) {
$post = new Post($this->db);
$post->title = $title;
$post->content = $content;
$post->save();
return $post;
}
public function updatePost($id, $title, $content) {
$post = new Post($this->db, $id);
$post->title = $title;
$post->content = $content;
$post->save();
return $post;
}
public function deletePost($id) {
$post = new Post($this->db, $id);
$post->delete();
return $post;
}
}
?>
views/index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Blog</title>
<link rel="stylesheet" href="public/css/style.css">
</head>
<body>
<h1>Blog</h1>
<a href="index.php?action=new">New Post</a>
<ul>
<?php foreach ($posts as $post): ?>
<li>
<h2><?php echo $post->title; ?></h2>
<p><?php echo $post->content; ?></p>
<a href="index.php?action=view&id=<?php echo $post->id; ?>">View</a>
<a href="index.php?action=edit&id=<?php echo $post->id; ?>">Edit</a>
<a href="index.php?action=delete&id=<?php echo $post->id; ?>">Delete</a>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
views/post.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php echo $post->title; ?></title>
<link rel="stylesheet" href="public/css/style.css">
</head>
<body>
<h1><?php echo $post->title; ?></h1>
<p><?php echo $post->content; ?></p>
<a href="index.php">Back</a>
</body>
</html>
通过这个简单的博客系统示例,你可以看到PHP在Web开发中的应用。这个项目展示了如何使用PHP进行数据库操作、MVC架构的应用以及基本的CRUD操作。希望这个案例能帮助你更好地理解PHP项目的开发和结构。
领取专属 10元无门槛券
手把手带您无忧上云