# シンプルなメモ帳のサンプル

#### JSDB API を利用した PHP メモ帳サンプル

以下は、JSON Scalable Database (JSDB) API を活用したシンプルなメモ帳アプリケーションのサンプルコードです。PHP を使用して、メモの追加、表示、編集、削除が行えます。

***

### ファイル構成

```
/notepad
├── index.php      // メモ帳のUIと操作ページ
├── style.css      // 簡易スタイルシート（オプション）
```

***

### `index.php` (メインのPHPファイル)

```php
<?php
$apiKey = "";  // 使用するAPIのkey名（データベース名）
$apiEndpoint = "https://events-front.mixeder.net/v2/jsdb";

// メモ一覧を取得する関数
function getNotes() {
    global $apiKey, $apiEndpoint;
    $response = file_get_contents("$apiEndpoint?key=$apiKey&action=read");
    return json_decode($response, true) ?? [];
}

// メモを追加する関数
if (isset($_POST['add'])) {
    $newNote = json_encode(["text" => $_POST['note']]);
    file_get_contents("$apiEndpoint?key=$apiKey&action=add&data=" . urlencode($newNote));
    header("Location: index.php");
    exit();
}

// メモを削除する関数
if (isset($_GET['delete'])) {
    $id = $_GET['delete'];
    file_get_contents("$apiEndpoint?key=$apiKey&action=delete&id=$id");
    header("Location: index.php");
    exit();
}

// メモの更新
if (isset($_POST['update'])) {
    $id = $_POST['id'];
    $updatedNote = json_encode(["text" => $_POST['note']]);
    file_get_contents("$apiEndpoint?key=$apiKey&action=update&id=$id&data=" . urlencode($updatedNote));
    header("Location: index.php");
    exit();
}

// すべてのメモを取得
$notes = getNotes();
?>
<!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>
    <h1>メモ帳</h1>

    <!-- メモ追加フォーム -->
    <form method="POST">
        <textarea name="note" rows="3" placeholder="新しいメモを入力してください..." required></textarea>
        <button type="submit" name="add">追加</button>
    </form>

    <!-- メモ一覧表示 -->
    <ul>
        <?php foreach ($notes as $note): ?>
            <li>
                <form method="POST" style="display:inline;">
                    <input type="hidden" name="id" value="<?= $note['id'] ?>">
                    <input type="text" name="note" value="<?= htmlspecialchars($note['text']) ?>" required>
                    <button type="submit" name="update">更新</button>
                </form>
                <a href="?delete=<?= $note['id'] ?>" onclick="return confirm('このメモを削除しますか？')">削除</a>
            </li>
        <?php endforeach; ?>
    </ul>
</body>
</html>
```

***

### `style.css` (簡易スタイルシート)

```css
body {
    font-family: 'Arial', sans-serif;
    background-color: #f0f0f0;
    margin: 0;
    padding: 20px;
}

h1 {
    text-align: center;
}

form {
    margin-bottom: 10px;
}

textarea, input[type="text"] {
    width: 80%;
    margin-right: 5px;
    padding: 10px;
}

button {
    padding: 10px;
}

ul {
    list-style-type: none;
    padding: 0;
}

li {
    margin-bottom: 10px;
    display: flex;
    align-items: center;
}
```

***

### 説明

1. **メモの追加**: 新しいメモを入力して「追加」ボタンを押すと、JSDB API を通じてメモが保存されます。
2. **メモの表示**: すべてのメモはリスト形式で表示され、各メモには更新フォームと削除リンクがあります。
3. **メモの更新**: 各メモのテキストを変更し、「更新」ボタンを押すと、JSDB API を使用してその内容が更新されます。
4. **メモの削除**: 削除リンクを押すと、メモが削除されます。

***

### 注意点

* **APIのkey** (`$apiKey`) は一意のものを使用してください。
* APIエンドポイントが有効であることを確認してください。
* `data` パラメータは **JSONエンコード** および **URLエンコード** が必要です。

***

このサンプルを使えば、JSDB API を使った軽量なメモ帳アプリをすぐに構築できます。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developer.mixeder.net/mixederpublicapis/apirifarensu/json-scalable-database-jsdb-api/shinpurunamemonosanpuru.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
