データベースのデータでリンク管理
ファイルの構成
project-root/
│
├── config/
│ └── db.php ← DB接続用の共通ファイル
│
├── index.php ← メイン(目次 + ポート一覧)
└── other_page.php ← 他のページでも共通のDB接続を利用可能
index.php
<?php
// DB接続
require_once __DIR__ . '/config/db.php';
// ---------- CRUD処理 ----------
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// リンク追加
if (isset($_POST['add_link'])) {
$stmt = $pdo->prepare("INSERT INTO menu_links (title, url, category, sort_order) VALUES (?, ?, ?, ?)");
$stmt->execute([$_POST['title'], $_POST['url'], $_POST['category'] ?? 'general', $_POST['sort_order'] ?? 0]);
}
// ポート追加
if (isset($_POST['add_port'])) {
$stmt = $pdo->prepare("INSERT INTO ports (port_number, description, url, sort_order) VALUES (?, ?, ?, ?)");
$stmt->execute([$_POST['port_number'], $_POST['description'], $_POST['url'], $_POST['sort_order'] ?? 0]);
}
// リンク更新
if (isset($_POST['update_link'])) {
$stmt = $pdo->prepare("UPDATE menu_links SET title=?, url=?, category=?, sort_order=? WHERE id=?");
$stmt->execute([$_POST['title'], $_POST['url'], $_POST['category'], $_POST['sort_order'], $_POST['id']]);
}
// ポート更新
if (isset($_POST['update_port'])) {
$stmt = $pdo->prepare("UPDATE ports SET port_number=?, description=?, url=?, sort_order=? WHERE id=?");
$stmt->execute([$_POST['port_number'], $_POST['description'], $_POST['url'], $_POST['sort_order'], $_POST['id']]);
}
header("Location: index.php");
exit;
}
// 削除
if (isset($_GET['delete_link'])) {
$pdo->prepare("DELETE FROM menu_links WHERE id=?")->execute([$_GET['delete_link']]);
header("Location: index.php");
exit;
}
if (isset($_GET['delete_port'])) {
$pdo->prepare("DELETE FROM ports WHERE id=?")->execute([$_GET['delete_port']]);
header("Location: index.php");
exit;
}
// データ取得
$links = $pdo->query("SELECT * FROM menu_links ORDER BY sort_order ASC")->fetchAll(PDO::FETCH_ASSOC);
$ports = $pdo->query("SELECT * FROM ports ORDER BY sort_order ASC")->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>目次管理</title>
<style>
.edit-form { display: none; margin-top: 5px; }
</style>
<script>
function toggleEditForm(id) {
const form = document.getElementById("edit-" + id);
form.style.display = (form.style.display === "none" || form.style.display === "") ? "block" : "none";
}
</script>
</head>
<body>
<h1>目次</h1>
<ul>
<?php foreach ($links as $link): ?>
<li>
<a href="<?= htmlspecialchars($link['url']) ?>" target="_blank">
<?= htmlspecialchars($link['title']) ?>
</a>
[<a href="?delete_link=<?= $link['id'] ?>" onclick="return confirm('削除しますか?')">削除</a>]
[<a href="javascript:void(0)" onclick="toggleEditForm('link<?= $link['id'] ?>')">編集</a>]
<!-- 編集フォーム(非表示) -->
<form id="edit-link<?= $link['id'] ?>" class="edit-form" method="post">
<input type="hidden" name="id" value="<?= $link['id'] ?>">
<input type="text" name="title" value="<?= htmlspecialchars($link['title']) ?>">
<input type="text" name="url" value="<?= htmlspecialchars($link['url']) ?>">
<input type="text" name="category" value="<?= htmlspecialchars($link['category']) ?>">
<input type="number" name="sort_order" value="<?= $link['sort_order'] ?>">
<button type="submit" name="update_link">更新</button>
</form>
</li>
<?php endforeach; ?>
</ul>
<h3>リンク追加</h3>
<form method="post">
<input type="text" name="title" placeholder="タイトル" required>
<input type="text" name="url" placeholder="URL" required>
<input type="text" name="category" placeholder="カテゴリ">
<input type="number" name="sort_order" placeholder="並び順">
<button type="submit" name="add_link">追加</button>
</form>
<hr>
<h2>現在使用しているポート</h2>
<table border="1">
<thead>
<tr>
<th>ポート番号</th>
<th>用途</th>
<th>URL</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php foreach ($ports as $port): ?>
<tr>
<td><?= htmlspecialchars($port['port_number']) ?></td>
<td><?= htmlspecialchars($port['description']) ?></td>
<td>
<?php if ($port['url']): ?>
<a href="<?= htmlspecialchars($port['url']) ?>" target="_blank"><?= htmlspecialchars($port['url']) ?></a>
<?php endif; ?>
</td>
<td>
<a href="?delete_port=<?= $port['id'] ?>" onclick="return confirm('削除しますか?')">削除</a>
<a href="javascript:void(0)" onclick="toggleEditForm('port<?= $port['id'] ?>')">編集</a>
<!-- 編集フォーム(非表示) -->
<form id="edit-port<?= $port['id'] ?>" class="edit-form" method="post">
<input type="hidden" name="id" value="<?= $port['id'] ?>">
<input type="number" name="port_number" value="<?= $port['port_number'] ?>">
<input type="text" name="description" value="<?= htmlspecialchars($port['description']) ?>">
<input type="text" name="url" value="<?= htmlspecialchars($port['url']) ?>">
<input type="number" name="sort_order" value="<?= $port['sort_order'] ?>">
<button type="submit" name="update_port">更新</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<h3>ポート追加</h3>
<form method="post">
<input type="number" name="port_number" placeholder="ポート番号" required>
<input type="text" name="description" placeholder="用途">
<input type="text" name="url" placeholder="URL">
<input type="number" name="sort_order" placeholder="並び順">
<button type="submit" name="add_port">追加</button>
</form>
</body>
</html>
db.php
<?php
$dsn = 'mysql:host=localhost;dbname=データベースの名前;charset=utf8mb4';
$user = 'ユーザー名';
$pass = 'パスワード';
try {
$pdo = new PDO($dsn, $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("DB接続エラー: " . $e->getMessage());
}
テーブル
CREATE TABLE menu_links (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
url VARCHAR(500) NOT NULL,
category VARCHAR(100) DEFAULT 'general', -- カテゴリ分け(目次、ポートなど)
sort_order INT DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE ports (
id INT AUTO_INCREMENT PRIMARY KEY,
port_number INT NOT NULL,
description VARCHAR(255),
url VARCHAR(500),
sort_order INT DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
フォルダでカテゴリ別にリンク管理
ファイルの構成
project-root/
│
├── カテゴリ/
│ └── フォルダ名1/
│ └── index.php
│ └── フォルダ名2/
│ └── index.php
│
├── index.php
index.php
<?php
$baseDir = __DIR__;
// カテゴリ一覧(help/直下のフォルダ)
$categories = array_filter(scandir($baseDir), function ($item) use ($baseDir) {
return is_dir($baseDir . DIRECTORY_SEPARATOR . $item)
&& $item !== '.'
&& $item !== '..';
});
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ヘルプ</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>ヘルプセンター</h1>
</header>
<main>
<section class="help-section">
<div class="accordion-controls">
<button id="openAll">すべて開く</button>
<button id="closeAll">すべて閉じる</button>
</div>
<div class="accordion">
<div class="accordion-item">
<button class="accordion-header">新規入職者</button>
<div class="accordion-content">
<p><a href="../shinki/" target="_blank">Googleアカウント作成・slack設定等</a></p>
<p><a href="https://docs.google.com/spreadsheets/d/1onNilx9ZpRPDA8yyTSQcDQlazRKCNgx1vfNEcRtUUak/edit?gid=0#gid=0" target="_blank">業務報告シート</a></p>
<p><a href="https://drive.google.com/drive/folders/0AK5OZ_1O2TaTUk9PVA" target="_blank">Googleドライブ全員共有</a></p>
<p>\\YOSHIJIMA-NAS-0</p>
</div>
</div>
<?php foreach ($categories as $category): ?>
<div class="accordion-item">
<button class="accordion-header">
<?php echo htmlspecialchars($category, ENT_QUOTES, 'UTF-8'); ?>
</button>
<div class="accordion-content">
<?php
// サブカテゴリ一覧(help/{カテゴリ}/直下のフォルダ)
$subDir = $baseDir . DIRECTORY_SEPARATOR . $category;
$subfolders = array_filter(scandir($subDir), function ($item) use ($subDir) {
return is_dir($subDir . DIRECTORY_SEPARATOR . $item)
&& $item !== '.'
&& $item !== '..';
});
?>
<?php foreach ($subfolders as $sub): ?>
<p>
<a href="<?php echo dirname($_SERVER['PHP_SELF']) . '/' . $category . '/' . $sub . '/'; ?>" target="_blank">
<?php echo htmlspecialchars($sub, ENT_QUOTES, 'UTF-8'); ?>
</a>
</p>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
</div>
</section>
</main>
<footer>
<p>© <a href="https://fukushicareer.jp" target="_blank" class="footer-link">2025 FCC</a></p>
</footer>
<script src="script.js"></script>
</body>
</html>