search.php4.9 KB
<?php
// 1. 引入配置文件并连接数据库
if (!file_exists(__DIR__ . '/config.php')) {
die('系统未安装!请先访问 <a href="install/">安装向导</a>');
}
require __DIR__ . '/config.php';
try {
$dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=utf8mb4";
$pdo = new PDO($dsn, DB_USER, DB_PASS, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
} catch (PDOException $e) {
die("数据库连接失败:" . $e->getMessage());
}
// 2. 获取并处理搜索关键词
$keyword = isset($_GET['q']) ? trim($_GET['q']) : '';
// 【复用】获取所有独立页用于顶部导航
$stmt_pages = $pdo->query("SELECT id, title, slug FROM pages ORDER BY id ASC");
$pages = $stmt_pages->fetchAll(PDO::FETCH_ASSOC);
// 3. 如果有关键词,则执行搜索查询
$posts = [];
$total_posts = 0;
if (!empty($keyword)) {
// 动态检测 status 字段
$has_status = false;
try {
$pdo->query("SELECT status FROM posts LIMIT 1");
$has_status = true;
} catch (PDOException $e) {}
// 构建搜索 SQL(同时搜索标题和内容)
$sql = "SELECT p.id, p.title, p.content, p.created_at, u.username AS author_name
FROM posts p LEFT JOIN users u ON p.author_id = u.id
WHERE (p.title LIKE :kw OR p.content LIKE :kw2)";
if ($has_status) {
$sql .= " AND p.status = 'published'";
}
$sql .= " ORDER BY p.created_at DESC";
$stmt = $pdo->prepare($sql);
// 使用通配符 %keyword% 进行模糊搜索
$like_keyword = '%' . $keyword . '%';
$stmt->execute([':kw' => $like_keyword, ':kw2' => $like_keyword]);
$posts = $stmt->fetchAll(PDO::FETCH_ASSOC);
$total_posts = count($posts);
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>搜索结果:<?php echo htmlspecialchars($keyword); ?> - 我的 PHP 博客</title>
<style>
.section-box {
background: var(--card-bg);
border-radius: 12px;
padding: 25px;
margin-bottom: 20px;
box-shadow: 0 2px 10px rgba(0,0,0,0.03);
}
</style>
</head>
<body>
<!-- 搜索弹窗 -->
<?php require_once __DIR__ . '/search-tc.php'; ?>
<div class="container">
<?php require_once __DIR__ . '/header.php'; ?>
<div class="main-content">
<div class="content-left">
<div class="page-header">
<nav>
<!-- 【新增】全局搜索框 -->
<form action="search.php" method="GET" class="header-search">
<input type="text" name="q" placeholder="搜索文章..." value="<?php echo htmlspecialchars($keyword); ?>">
<button type="submit">搜索</button>
</form></nav>
<div class="section-box">
<h3 style="margin-top:0; font-size:16px; color:#666;">
<?php if (!empty($keyword)): ?>
<h4>🔍 搜索结果</h4>
<p>关于 "<?php echo htmlspecialchars($keyword); ?>" 共找到 <?php echo $total_posts; ?> 篇文章</p>
<?php else: ?>
<h4>🔍 请输入关键词搜索</h4>
<p>请在上方输入框中输入您想查找的内容。</p>
<?php endif; ?>
</h3></div>
</div>
<?php if (!empty($posts)): ?>
<?php foreach ($posts as $post): ?>
<div class="post-card">
<h2 class="post-title"><a href="post.php?id=<?php echo $post['id']; ?>"><?php echo htmlspecialchars($post['title']); ?></a></h2>
<div class="post-meta">
<span>🕒 <?php echo date('Y-m-d H:i', strtotime($post['created_at'])); ?></span>
<?php if (!empty($post['author_name'])): ?>
<span>✍️ <?php echo htmlspecialchars($post['author_name']); ?></span>
<?php endif; ?>
</div>
<div class="post-excerpt">
<?php echo mb_substr(strip_tags($post['content']), 0, 150) . '...'; ?>
</div>
</div>
<?php endforeach; ?>
<?php elseif (!empty($keyword)): ?>
<div class="post-card" style="text-align:center; color:#aaa;">
<p>没有找到相关的文章,请尝试更换其他关键词。</p>
</div>
<?php endif; ?>
</div>
<?php require_once __DIR__ . '/sidebar-right.php'; ?>
</div>
<!-- 【结构优化】将 footer 移出 container,使其能够横跨整个屏幕宽度 -->
<?php require_once __DIR__ . '/footer.php'; ?>
</div>
</body>
</html>