links.php4.4 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. 查询所有友情链接
$stmt_links = $pdo->query("SELECT name, url, description FROM links ORDER BY id ASC");
$links = $stmt_links->fetchAll(PDO::FETCH_ASSOC);
// 【复用】获取所有独立页用于顶部导航
$stmt_pages = $pdo->query("SELECT id, title, slug FROM pages ORDER BY id ASC");
$pages = $stmt_pages->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>友情链接 - 我的 PHP 博客</title>
<style>
nav ul { list-style: none; margin: 0; padding: 0; display: flex; gap: 5px; }
nav ul li a { display: block; padding: 8px 12px; color: #ddd; text-decoration: none; font-size: 14px; border-radius: 4px; transition: 0.3s; }
nav ul li a:hover { background-color: #555; color: #fff; }
/* 页面主体样式 */
.page-card { background: #fff; border-radius: 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.05); padding: 35px; margin-bottom: 20px; }
.page-title { font-size: 28px; margin: 0 0 25px; border-bottom: 2px solid #eee; padding-bottom: 15px; color: #222; }
/* 链接卡片网格布局 */
.links-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 20px; }
.link-item { display: block; text-decoration: none; color: inherit; padding: 20px; border-radius: 8px; border: 1px solid #eee; transition: all 0.3s ease; background-color: #fafafa; }
.link-item:hover { transform: translateY(-3px); box-shadow: 0 5px 15px rgba(0,0,0,0.08); border-color: #007bff; background-color: #fff; }
.link-name { font-size: 16px; font-weight: bold; color: #333; margin-bottom: 8px; display: block; }
.link-desc { font-size: 13px; color: #888; line-height: 1.5; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
/* 空状态提示 */
.empty-state { text-align: center; padding: 40px; color: #aaa; font-size: 16px; grid-column: 1 / -1; }
.back-home { display: inline-block; margin-top: 20px; color: #007bff; text-decoration: none; font-size: 14px; }
.back-home:hover { text-decoration: underline; }
</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-card">
<h2 class="page-title">🔗 友情链接</h2>
<?php if (!empty($links)): ?>
<div class="links-grid">
<?php foreach ($links as $link): ?>
<!-- target="_blank" 确保在新标签页打开 -->
<a href="<?php echo htmlspecialchars($link['url']); ?>" class="link-item" target="_blank" rel="noopener noreferrer">
<span class="link-name"><?php echo htmlspecialchars($link['name']); ?></span>
<?php if (!empty($link['description'])): ?>
<span class="link-desc"><?php echo htmlspecialchars($link['description']); ?></span>
<?php endif; ?>
</a>
<?php endforeach; ?>
</div>
<?php else: ?>
<div class="empty-state">
<p>暂无友情链接。</p>
</div>
<?php endif; ?>
<a href="index.php" class="back-home">← 返回首页</a>
</div>
</div>
<?php require_once __DIR__ . '/sidebar-right.php'; ?>
</div>
<!-- 【结构优化】将 footer 移出 container,使其能够横跨整个屏幕宽度 -->
<?php require_once __DIR__ . '/footer.php'; ?>
</div>
</body>
</html>