{"id":134,"date":"2025-01-27T08:08:24","date_gmt":"2025-01-27T07:08:24","guid":{"rendered":"http:\/\/cibis.de\/blog\/?p=134"},"modified":"2025-01-21T08:20:57","modified_gmt":"2025-01-21T07:20:57","slug":"linksammkung-teil-3-feinheiten","status":"publish","type":"post","link":"https:\/\/cibis.de\/blog\/linksammkung-teil-3-feinheiten\/2025\/","title":{"rendered":"Linksammlung Teil 3 &#8211; Feinheiten"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Mit <a href=\"http:\/\/cibis.de\/blog\/linksammlung-teil-2-die-eingabe\/2025\/\">Teil 2<\/a> war die Linksammlung fast schon vollst\u00e4ndig. Heute kommen noch ein paar Feinheiten dazu. Z.B. sollen die Tags ausgegeben und geordnet werden und die M\u00f6glichkeit bestehen Links wieder zu l\u00f6schen.<\/p>\n\n\n\n<!--more-->\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Tag-Liste ausgeben\necho \"&lt;h2&gt;Tags:&lt;\/h2&gt;\";\necho \"&lt;ul class=\\\"tag-list\\\"&gt;\";\nforeach ($tags as $tag) {\n    echo \"&lt;li&gt;&lt;a href=\\\"?tag=$tag\\\"&gt;$tag&lt;\/a&gt;&lt;\/li&gt;\";\n}\necho \"&lt;\/ul&gt;\";\n\n\/\/ Links nach Tag filtern\nif (isset($_GET&#91;\"tag\"])) {\n    $tag = $_GET&#91;\"tag\"];\n    $sql = \"SELECT * FROM links WHERE tags LIKE '%$tag%'\";\n    echo \"&lt;button onclick=\\\"window.location.href='urls.php'\\\"&gt;Zur\u00fcck&lt;\/button&gt;\";\n} else {\n    \/\/ Alle Links abrufen und nach Datum sortieren\n    $sql = \"SELECT * FROM links ORDER BY created_at DESC\";\n}\n\n$result = $conn-&gt;query($sql);\n\nif ($result-&gt;num_rows &gt; 0) {\n    echo \"&lt;h2&gt;Links:&lt;\/h2&gt;\";\n    echo \"&lt;ul&gt;\";\n    while($row = $result-&gt;fetch_assoc()) {\n        echo \"&lt;li&gt;\";\n        echo \"&lt;a href=\\\"\" . $row&#91;\"url\"] . \"\\\" target=\\\"_blank\\\"&gt;\" . $row&#91;\"name\"] . \"&lt;\/a&gt;\";\n        echo \" &lt;a href=\\\"?delete=\" . $row&#91;\"id\"] . \"\\\" class=\\\"delete-button\\\"&gt;L\u00f6schen&lt;\/a&gt;\";\n        echo \"&lt;\/li&gt;\";\n    }\n    echo \"&lt;\/ul&gt;\";\n} else {\n    echo \"Keine Links gefunden.\";\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Eigentlich fehlen jetzt nur noch die jeweiligen Tags um das PHP zu kennzeichnen, sowie eventuell CSS. Komplett w\u00fcrde die Datei folgenderma\u00dfen aussehen:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n\/\/ Datenbankverbindungsdaten\n$servername = \"localhost\";\n$username = \"dein_benutzername\";\n$password = \"dein_passwort\";\n$dbname = \"db_name\";\n\n\/\/ Datenbankverbindung herstellen\n$conn = new mysqli($servername, $username, $password);\n\n\/\/ \u00dcberpr\u00fcfen, ob die Verbindung erfolgreich war\nif ($conn-&gt;connect_error) {\n    die(\"Verbindung fehlgeschlagen: \" . $conn-&gt;connect_error);\n}\n\n\/\/ Datenbank erstellen, falls sie noch nicht existiert\n$sql = \"CREATE DATABASE IF NOT EXISTS $dbname\";\nif ($conn-&gt;query($sql) === TRUE) {\n    \/\/echo \"Datenbank erfolgreich erstellt\";\n} else {\n    echo \"Fehler beim Erstellen der Datenbank: \" . $conn-&gt;error;\n}\n\n\/\/ Datenbank ausw\u00e4hlen\n$conn-&gt;select_db($dbname);\n\n\/\/ Tabelle erstellen, falls sie noch nicht existiert\n$sql = \"CREATE TABLE IF NOT EXISTS links (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    url VARCHAR(255) NOT NULL,\n    name VARCHAR(255) NOT NULL,\n    tags VARCHAR(255),\n    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n)\";\nif ($conn-&gt;query($sql) === TRUE) {\n    \/\/echo \"Tabelle erfolgreich erstellt\";\n} else {\n    echo \"Fehler beim Erstellen der Tabelle: \" . $conn-&gt;error;\n}\n\n\/\/ Link hinzuf\u00fcgen\nif ($_SERVER&#91;\"REQUEST_METHOD\"] == \"POST\" &amp;&amp; isset($_POST&#91;\"url\"]) &amp;&amp; isset($_POST&#91;\"name\"]) &amp;&amp; isset($_POST&#91;\"tags\"])) {\n    $url = $_POST&#91;\"url\"];\n    $name = $_POST&#91;\"name\"];\n    $tags = $_POST&#91;\"tags\"];\n\n    \/\/ Prepared Statement verwenden, um SQL-Injection zu verhindern\n    $stmt = $conn-&gt;prepare(\"INSERT INTO links (url, name, tags) VALUES (?, ?, ?)\");\n    $stmt-&gt;bind_param(\"sss\", $url, $name, $tags);\n\n    if ($stmt-&gt;execute() === TRUE) {\n        \/\/echo \"Link erfolgreich hinzugef\u00fcgt\";\n    } else {\n        echo \"Fehler beim Hinzuf\u00fcgen des Links: \" . $stmt-&gt;error;\n    }\n    $stmt-&gt;close();\n}\n\n\/\/ Link l\u00f6schen\nif (isset($_GET&#91;\"delete\"])) {\n    $id = $_GET&#91;\"delete\"];\n\n    \/\/ Prepared Statement verwenden, um SQL-Injection zu verhindern\n    $stmt = $conn-&gt;prepare(\"DELETE FROM links WHERE id=?\");\n    $stmt-&gt;bind_param(\"i\", $id);\n\n    if ($stmt-&gt;execute() === TRUE) {\n        \/\/echo \"Link erfolgreich gel\u00f6scht\";\n    } else {\n        echo \"Fehler beim L\u00f6schen des Links: \" . $stmt-&gt;error;\n    }\n    $stmt-&gt;close();\n}\n\n?&gt;\n\n&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n  &lt;title&gt;Linksammlung&lt;\/title&gt;\n  &lt;meta charset=\"UTF-8\"&gt;\n  &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n  &lt;style&gt;\n    body {\n      font-family: Arial, sans-serif; color: black; background-color: white;\n background-repeat: no-repeat; background-position: right top;\nbackground-attachment: fixed; \nopacity: 0.7; \nmargin: 50px;\nborder: 1px dashed black; \/* 1px breite, gestrichelte, schwarze Linie *\/ padding: 20px; \/* 20px Abstand \n    }\na { text-decoration: none; \ncolor: black;  \n}\na:visited { color: black; \/* Setzt die Farbe f\u00fcr besuchte Links ebenfalls auf schwarz *\/ }\n    .tag-list {\n      list-style: none;\n      padding: 0;\n      margin-bottom: 20px;\n    }\n    .tag-list li {\n      display: inline-block;\n      margin-right: 10px;\n    }\n    .delete-button {\n      color: red;\n      font-size: smaller;\n    }\n    \/* Responsive Design f\u00fcr kleinere Bildschirme *\/\n    @media (max-width: 600px) {\n      input&#91;type=\"text\"] {\n        width: 100%;\n      }\n    }\n  &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n&lt;h1&gt;Linksammlung&lt;\/h1&gt;\n\n&lt;form method=\"post\"&gt;\n  URL: &lt;input type=\"text\" name=\"url\" required&gt;&lt;br&gt;&lt;br&gt;\n  Name: &lt;input type=\"text\" name=\"name\" required&gt;&lt;br&gt;&lt;br&gt;\n  Tags (durch Komma getrennt): &lt;input type=\"text\" name=\"tags\"&gt;&lt;br&gt;&lt;br&gt;\n  &lt;input type=\"submit\" value=\"Link hinzuf\u00fcgen\"&gt;\n&lt;\/form&gt;\n\n&lt;?php\n\n\/\/ Tags abrufen und alphabetisch sortieren\n$sql = \"SELECT DISTINCT tags FROM links\";\n$result = $conn-&gt;query($sql);\n$tags = array();\nif ($result-&gt;num_rows &gt; 0) {\n    while($row = $result-&gt;fetch_assoc()) {\n        $tags = array_merge($tags, explode(\",\", $row&#91;\"tags\"]));\n    }\n    $tags = array_unique($tags);\n    sort($tags);\n}\n\n\/\/ Tag-Liste ausgeben\necho \"&lt;h2&gt;Tags:&lt;\/h2&gt;\";\necho \"&lt;ul class=\\\"tag-list\\\"&gt;\";\nforeach ($tags as $tag) {\n    echo \"&lt;li&gt;&lt;a href=\\\"?tag=$tag\\\"&gt;$tag&lt;\/a&gt;&lt;\/li&gt;\";\n}\necho \"&lt;\/ul&gt;\";\n\n\/\/ Links nach Tag filtern\nif (isset($_GET&#91;\"tag\"])) {\n    $tag = $_GET&#91;\"tag\"];\n    $sql = \"SELECT * FROM links WHERE tags LIKE '%$tag%'\";\n    echo \"&lt;button onclick=\\\"window.location.href='urls.php'\\\"&gt;Zur\u00fcck&lt;\/button&gt;\";\n} else {\n    \/\/ Alle Links abrufen und nach Datum sortieren\n    $sql = \"SELECT * FROM links ORDER BY created_at DESC\";\n}\n\n$result = $conn-&gt;query($sql);\n\nif ($result-&gt;num_rows &gt; 0) {\n    echo \"&lt;h2&gt;Links:&lt;\/h2&gt;\";\n    echo \"&lt;ul&gt;\";\n    while($row = $result-&gt;fetch_assoc()) {\n        echo \"&lt;li&gt;\";\n        echo \"&lt;a href=\\\"\" . $row&#91;\"url\"] . \"\\\" target=\\\"_blank\\\"&gt;\" . $row&#91;\"name\"] . \"&lt;\/a&gt;\";\n        echo \" &lt;a href=\\\"?delete=\" . $row&#91;\"id\"] . \"\\\" class=\\\"delete-button\\\"&gt;L\u00f6schen&lt;\/a&gt;\";\n        echo \"&lt;\/li&gt;\";\n    }\n    echo \"&lt;\/ul&gt;\";\n} else {\n    echo \"Keine Links gefunden.\";\n}\n\n$conn-&gt;close();\n\n?&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<div class=\"gsp_post_data\" \r\n\t            data-post_type=\"post\" \r\n\t            data-cat=\"code,php\" \r\n\t            data-modified=\"120\"\r\n\t            data-created=\"1737965304\"\r\n\t            data-title=\"Linksammlung Teil 3 &#8211; Feinheiten\" \r\n\t            data-home=\"https:\/\/cibis.de\/blog\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Mit Teil 2 war die Linksammlung fast schon vollst\u00e4ndig. Heute kommen noch ein paar Feinheiten dazu. Z.B. sollen die Tags ausgegeben und geordnet werden und die M\u00f6glichkeit bestehen Links wieder zu l\u00f6schen.<\/p>\n","protected":false},"author":1,"featured_media":135,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,7],"tags":[10,8,9,4],"class_list":["post-134","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-code","category-php","tag-bookmark","tag-code","tag-mysql","tag-php"],"_links":{"self":[{"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/posts\/134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/comments?post=134"}],"version-history":[{"count":4,"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/posts\/134\/revisions"}],"predecessor-version":[{"id":138,"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/posts\/134\/revisions\/138"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/media\/135"}],"wp:attachment":[{"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/media?parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/categories?post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cibis.de\/blog\/wp-json\/wp\/v2\/tags?post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}