Kods ir varbūt mazliet neglīts, un linki ir neglīti, taču linkus ar Pretty URL's var sataisīt glītus. :)
Šis ir gatavs darbam ar Madara auth klasi.
.sql fails, kas jāimportē datubāzē.
if (isset($_GET['admin']) && $auth->id) {
$ip = $_SERVER['REMOTE_ADDR'];
$content = $db->real_escape_string($_POST['content']);
$category = $db->real_escape_string($_POST['category']);
$title = htmlspecialchars($db->real_escape_string($_POST['title']));
if (isset($_GET['act'])) {
$id = $_GET['id'];
if ($_GET['act'] == 'edit' && isset($_GET['id'])) {
$values = $db->query("SELECT * FROM `raksti` WHERE `id` = '$id'");
$values = $values->fetch_object();
$cat1 = '<option value="1">1. kategorija</option>';
$cat2 = '<option value="2">2. kategorija</option>';
$cat3 = '<option value="3">3. kategorija</option>';
if (isset($_POST['submit'])) {
$db->query("UPDATE `raksti` SET `ip` = '$ip', `content` = '$content', `category` = '$category', `title` = '$title' WHERE `id` = '$id'");
}
echo '
<p class="title center">Labot rakstu:</p>
<form action="" method="post">
<p class="center">
<select name="category">
<option value="'.$values->category.'">---</option>
'.$cat1.'
'.$cat2.'
'.$cat3.'
</select>
</p>
<p class="center"><textarea name="title" placeholder="Nosaukums" style="height: 20px; width: 400px;">'.$values->title.'</textarea></p>
<p class="center">Saturs:</p>
<textarea name="content" style="width:100%; height: 400px;">'.$values->content.'</textarea>
<p class="center"><input name="submit" type="submit" value="Labot" /></p></form>';
}
else if ($_GET['act'] == 'delete' && isset($_GET['id'])) {
$id = $_GET['id'];
echo '<a href="/?admin&act=delete&id='.$id.'&confirmdelete">apstiprināt dzēšanu</a>';
if (isset($_GET['confirmdelete'])) {
$db->query("DELETE FROM `raksti` WHERE `id` = '$id' ");
header('Location: /');
}
}
}
else {
if (isset($_POST['submit'])) {
header('Location: /');
$db->query("INSERT INTO `raksti` (`author_id`, `title`, `content`, `created`, `ip`, `category`) VALUES ('$auth->id', '$title', '$content', NOW(), '$ip', '$category')");
}
echo '
<p class="title center">Pievienot rakstu:</p>
<form action="" method="post">
<p class="center">
<select name="category">
'.$cat1.'
'.$cat2.'
'.$cat3.'
</select>
</p>
<p class="center"><input type="text" name="title" placeholder="Nosaukums" style="height: 20px; width: 400px;" /></p>
<p class="center">Saturs:</p>
<textarea name="content" style="width:100%; height: 400px;"></textarea>
<p class="center"><input name="submit" type="submit" value="Pievienot" /></p></form>';
}
}
else if (isset($_GET['raksti'])) {
$category = $_GET['raksti'];
$query = $db->query("SELECT * FROM `raksti` WHERE `category` = '$category'");
}
else {
$query = $db->query("SELECT * FROM raksti");
}
while ($izdruka = $query->fetch_object()) {
echo '<p class="title"><a class="admin" href="/?admin&act=edit&id='.$izdruka->id.'">labot</a> <a class="admin" href="/?admin&act=delete&id='.$izdruka->id.'">dzēst</a> '.$izdruka->title.' | '.$izdruka->created.'</p>';
echo '<p class="content">'.$izdruka->content.'</p>';
echo '<hr />';
}
Links uz kategorijām veidojas /?raksti=id
Links labošanai veidojas /?admin&act=edit&id=id
Links dzēšanai veidojas /?admin&act=delete&id=id (dzēšana pēctam ir jāapstiprina ;))
Laboja mad, labots 1x