Poniżej znajdziesz kompletny, minimalny lecz w pełni działający zestaw plików:
• index.php – interfejs użytkownika (Bootstrap 5, modal do dodawania/edycji, podgląd Markdown dzięki marked.js)
• api.php – proste REST-owe API w PDO + SQLite (CRUD + wyszukiwanie)
• setup.sh – skrypt bash, który na serwerze od zera stworzy katalog oraz oba powyższe pliki
Wymagania: PHP ≥ 7.4 z włączonym rozszerzeniem pdo_sqlite, dowolny serwer HTTP (Apache/Nginx/…).
1. index.php
<?php /* index.php */ ?>
<!doctype html>
<html lang="pl">
<head>
<meta charset="utf-8">
<title>Notatki – PHP + SQLite</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<!-- Bootstrap 5 -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<style> textarea {font-family:monospace;} </style>
</head>
<body>
<div class="container py-4">
<h1 class="mb-4">📝 Moje notatki</h1>
<div class="d-flex mb-3">
<input class="form-control me-2" id="search" placeholder="Szukaj …">
<button class="btn btn-primary" id="btnAdd">+ Nowa notatka</button>
</div>
<table class="table table-hover" id="notesTable">
<thead class="table-light">
<tr><th>Tytuł</th><th>Ostatnia modyfikacja</th><th class="text-end">Akcje</th></tr>
</thead>
<tbody></tbody>
</table>
</div>
<!-- === Modal dodawania / edycji === -->
<div class="modal fade" id="noteModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-lg modal-dialog-scrollable">
<div class="modal-content">
<form id="noteForm" novalidate>
<div class="modal-header">
<h5 class="modal-title" id="modalTitle">Nowa notatka</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Zamknij"></button>
</div>
<div class="modal-body">
<input type="hidden" id="noteId">
<div class="mb-3">
<label class="form-label">Tytuł</label>
<input type="text" class="form-control" id="title" required>
</div>
<div class="mb-3">
<label class="form-label">Treść (Markdown)</label>
<textarea class="form-control" id="content" rows="10" required></textarea>
</div>
<div>
<label class="form-label">Podgląd</label>
<div id="preview" class="border rounded p-3 bg-light"></div>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-success" type="submit">💾 Zapisz</button>
<button class="btn btn-secondary" type="button" data-bs-dismiss="modal">Anuluj</button>
</div>
</form>
</div>
</div>
</div>
<!-- Bootstrap & marked.js -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script>
const noteModal = new bootstrap.Modal('#noteModal');
const preview = document.getElementById('preview');
const contentFld = document.getElementById('content');
/* Live-preview Markdown */
contentFld.addEventListener('input', () => {
preview.innerHTML = marked.parse(contentFld.value || '');
});
/* Pobranie i rysowanie listy */
function listNotes(q='') {
fetch('api.php?action=list&q='+encodeURIComponent(q))
.then(r=>r.json())
.then(data=>{
const tbody = document.querySelector('#notesTable tbody');
tbody.innerHTML = '';
data.forEach(n=>{
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${n.title}</td>
<td>${n.updated_at}</td>
<td class="text-end">
<button class="btn btn-sm btn-outline-secondary me-1" onclick="editNote(${n.id})">✏️</button>
<button class="btn btn-sm btn-outline-danger" onclick="deleteNote(${n.id})">🗑️</button>
</td>`;
tbody.appendChild(tr);
});
});
}
/* Dodawanie */
document.getElementById('btnAdd').addEventListener('click', ()=>{
document.getElementById('noteId').value='';
document.getElementById('title').value='';
contentFld.value='';
preview.innerHTML='';
document.getElementById('modalTitle').innerText='Nowa notatka';
noteModal.show();
});
/* Edycja */
function editNote(id){
fetch('api.php?action=get&id='+id)
.then(r=>r.json())
.then(n=>{
document.getElementById('noteId').value=n.id;
document.getElementById('title').value=n.title;
contentFld.value=n.content;
preview.innerHTML = marked.parse(n.content);
document.getElementById('modalTitle').innerText='Edytuj notatkę';
noteModal.show();
});
}
/* Usuwanie */
function deleteNote(id){
if(!confirm('Usunąć tę notatkę?')) return;
fetch('api.php?action=delete&id='+id)
.then(()=>listNotes());
}
/* Zapis */
document.getElementById('noteForm').addEventListener('submit', e=>{
e.preventDefault();
const fd=new FormData();
fd.append('id', document.getElementById('noteId').value);
fd.append('title',document.getElementById('title').value);
fd.append('content',contentFld.value);
fetch('api.php?action=save', {method:'POST', body:fd})
.then(()=>{ noteModal.hide(); listNotes(); });
});
/* Wyszukiwanie na żywo */
document.getElementById('search').addEventListener('input',e=>{
listNotes(e.target.value);
});
listNotes(); // start
</script>
</body>
</html>
2. api.php
<?php
/* api.php – proste API JSON */
header('Content-Type: application/json; charset=utf-8');
$dsn = 'sqlite:' . __DIR__ . '/db.sqlite';
try {
$pdo = new PDO($dsn);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Tworzymy tabelę przy pierwszym uruchomieniu
$pdo->exec(
"CREATE TABLE IF NOT EXISTS notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
content TEXT NOT NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL
)"
);
} catch (PDOException $e) {
http_response_code(500);
echo json_encode(['error'=>$e->getMessage()]); exit;
}
$action = $_GET['action'] ?? '';
switch ($action) {
/* ===== LISTA / SEARCH ===== */
case 'list':
$q = $_GET['q'] ?? '';
if ($q !== '') {
$stmt = $pdo->prepare(
"SELECT id, title, updated_at
FROM notes
WHERE title LIKE :q OR content LIKE :q
ORDER BY updated_at DESC"
);
$stmt->execute([':q'=>"%$q%"]);
} else {
$stmt = $pdo->query("SELECT id, title, updated_at FROM notes ORDER BY updated_at DESC");
}
echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
break;
/* ===== POBIERANIE JEDNEJ NOTATKI ===== */
case 'get':
$id = (int)($_GET['id'] ?? 0);
$stmt = $pdo->prepare("SELECT * FROM notes WHERE id=?");
$stmt->execute([$id]);
echo json_encode($stmt->fetch(PDO::FETCH_ASSOC) ?: new stdClass());
break;
/* ===== DODAWANIE / EDYCJA ===== */
case 'save':
$id = $_POST['id'] ?? '';
$title = trim($_POST['title'] ?? '');
$content = trim($_POST['content'] ?? '');
if ($title==='' || $content==='') {
http_response_code(400);
echo json_encode(['error'=>'Brak tytułu lub treści']); exit;
}
if ($id==='') { // INSERT
$stmt = $pdo->prepare(
"INSERT INTO notes(title,content,created_at,updated_at)
VALUES(?,?,datetime('now'),datetime('now'))"
);
$stmt->execute([$title,$content]);
} else { // UPDATE
$stmt = $pdo->prepare(
"UPDATE notes SET title=?, content=?, updated_at=datetime('now') WHERE id=?"
);
$stmt->execute([$title,$content,$id]);
}
echo json_encode(['success'=>true]);
break;
/* ===== USUWANIE ===== */
case 'delete':
$id=(int)($_GET['id'] ?? 0);
$pdo->prepare("DELETE FROM notes WHERE id=?")->execute([$id]);
echo json_encode(['success'=>true]);
break;
default:
http_response_code(400);
echo json_encode(['error'=>'Nieznana akcja']);
}
3. setup.sh – automatyczna instalacja
#!/usr/bin/env bash
# setup.sh – utworzy katalog oraz wszystkie pliki aplikacji
# Użycie: ./setup.sh /var/www/notes (jeśli brak argumentu -> ./notes-app w bieżącym katalogu)
set -e
APP_DIR="${1:-$PWD/notes-app}"
echo "➜ Tworzę aplikację w: $APP_DIR"
mkdir -p "$APP_DIR"
# ---------- index.php ----------
cat > "$APP_DIR/index.php" <<'PHP'
<?php /* index.php */ ?>
<!doctype html>
<html lang="pl">
<head>
<meta charset="utf-8">
<title>Notatki – PHP + SQLite</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<style> textarea {font-family:monospace;} </style>
</head>
<body>
<div class="container py-4">
<h1 class="mb-4">📝 Moje notatki</h1>
<div class="d-flex mb-3">
<input class="form-control me-2" id="search" placeholder="Szukaj …">
<button class="btn btn-primary" id="btnAdd">+ Nowa notatka</button>
</div>
<table class="table table-hover" id="notesTable">
<thead class="table-light"><tr><th>Tytuł</th><th>Ostatnia modyfikacja</th><th class="text-end">Akcje</th></tr></thead>
<tbody></tbody>
</table>
</div>
<div class="modal fade" id="noteModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-lg modal-dialog-scrollable">
<div class="modal-content">
<form id="noteForm" novalidate>
<div class="modal-header">
<h5 class="modal-title" id="modalTitle">Nowa notatka</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<input type="hidden" id="noteId">
<div class="mb-3"><label class="form-label">Tytuł</label><input type="text" class="form-control" id="title" required></div>
<div class="mb-3"><label class="form-label">Treść (Markdown)</label><textarea class="form-control" id="content" rows="10" required></textarea></div>
<div><label class="form-label">Podgląd</label><div id="preview" class="border rounded p-3 bg-light"></div></div>
</div>
<div class="modal-footer"><button class="btn btn-success" type="submit">💾 Zapisz</button><button class="btn btn-secondary" data-bs-dismiss="modal">Anuluj</button></div>
</form>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script>
const noteModal=new bootstrap.Modal('#noteModal'),preview=document.getElementById('preview'),contentFld=document.getElementById('content');
contentFld.addEventListener('input',()=>{preview.innerHTML=marked.parse(contentFld.value||'');});
function listNotes(q=''){fetch('api.php?action=list&q='+encodeURIComponent(q)).then(r=>r.json()).then(d=>{const tb=document.querySelector('#notesTable tbody');tb.innerHTML='';d.forEach(n=>{const tr=document.createElement('tr');tr.innerHTML=`<td>${n.title}</td><td>${n.updated_at}</td><td class="text-end"><button class="btn btn-sm btn-outline-secondary me-1" onclick="editNote(${n.id})">✏️</button><button class="btn btn-sm btn-outline-danger" onclick="deleteNote(${n.id})">🗑️</button></td>`;tb.appendChild(tr);});});}
document.getElementById('btnAdd').addEventListener('click',()=>{document.getElementById('noteId').value='';document.getElementById('title').value='';contentFld.value='';preview.innerHTML='';document.getElementById('modalTitle').innerText='Nowa notatka';noteModal.show();});
function editNote(id){fetch('api.php?action=get&id='+id).then(r=>r.json()).then(n=>{document.getElementById('noteId').value=n.id;document.getElementById('title').value=n.title;contentFld.value=n.content;preview.innerHTML=marked.parse(n.content);document.getElementById('modalTitle').innerText='Edytuj notatkę';noteModal.show();});}
function deleteNote(id){if(!confirm('Usunąć tę notatkę?'))return;fetch('api.php?action=delete&id='+id).then(()=>listNotes());}
document.getElementById('noteForm').addEventListener('submit',e=>{e.preventDefault();const fd=new FormData();fd.append('id',document.getElementById('noteId').value);fd.append('title',document.getElementById('title').value);fd.append('content',contentFld.value);fetch('api.php?action=save',{method:'POST',body:fd}).then(()=>{noteModal.hide();listNotes();});});
document.getElementById('search').addEventListener('input',e=>listNotes(e.target.value));
listNotes();
</script>
</body></html>
PHP
# ---------- api.php ----------
cat > "$APP_DIR/api.php" <<'PHP'
<?php
header('Content-Type: application/json; charset=utf-8');
$dsn='sqlite:'.__DIR__.'/db.sqlite';
try{$pdo=new PDO($dsn);$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$pdo->exec("CREATE TABLE IF NOT EXISTS notes(id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT NOT NULL,content TEXT NOT NULL,created_at DATETIME NOT NULL,updated_at DATETIME NOT NULL)");}
catch(PDOException $e){http_response_code(500);echo json_encode(['error'=>$e->getMessage()]);exit;}
$action=$_GET['action']??'';
switch($action){
case'list':
$q=$_GET['q']??'';if($q!==''){$st=$pdo->prepare("SELECT id,title,updated_at FROM notes WHERE title LIKE :q OR content LIKE :q ORDER BY updated_at DESC");$st->execute([':q'=>"%$q%"]);}else{$st=$pdo->query("SELECT id,title,updated_at FROM notes ORDER BY updated_at DESC");}
echo json_encode($st->fetchAll(PDO::FETCH_ASSOC));break;
case'get':
$id=(int)($_GET['id']??0);$st=$pdo->prepare("SELECT * FROM notes WHERE id=?");$st->execute([$id]);echo json_encode($st->fetch(PDO::FETCH_ASSOC)?:new stdClass());break;
case'save':
$id=$_POST['id']??'';$title=trim($_POST['title']??'');$content=trim($_POST['content']??'');
if($title==''||$content==''){http_response_code(400);echo json_encode(['error'=>'Brak tytułu lub treści']);exit;}
if($id===''){$st=$pdo->prepare("INSERT INTO notes(title,content,created_at,updated_at) VALUES(?,?,datetime('now'),datetime('now'))");$st->execute([$title,$content]);}
else{$st=$pdo->prepare("UPDATE notes SET title=?,content=?,updated_at=datetime('now') WHERE id=?");$st->execute([$title,$content,$id]);}
echo json_encode(['success'=>true]);break;
case'delete':
$id=(int)($_GET['id']??0);$pdo->prepare("DELETE FROM notes WHERE id=?")->execute([$id]);echo json_encode(['success'=>true]);break;
default:http_response_code(400);echo json_encode(['error'=>'Nieznana akcja']);}
PHP
chmod 644 "$APP_DIR"/*.php
echo "✔ Gotowe! Wskaż serwer WWW na $APP_DIR i korzystaj."
Nadaj uprawnienia:
chmod +x setup.sh
a następnie uruchom, np.
./setup.sh /var/www/html/notes
i odwiedź http://serwer/notes/index.php.
Miłego notowania! ✨