linkvz/index.php

83 lines
2.1 KiB
PHP
Raw Normal View History

2021-12-28 23:49:45 +01:00
<?php
2022-01-09 00:05:57 +01:00
2022-01-08 22:43:50 +01:00
class Link{
public $name;
public $url;
public function __construct($newName, $newUrl){
$this->name = $newName;
$this->url = $newUrl;
}
}
2022-01-09 00:05:57 +01:00
class dbLinkHandler
{
private $pdo;
2022-01-08 22:43:50 +01:00
2022-01-09 00:05:57 +01:00
public function __construct()
{
try
{
// Verbindung zur Datenbank herstellen
$this->pdo = new PDO('mysql:host=localhost;dbname=linkdb', 'linkdb', 'linkdb');
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Tabelle link erstellen, falls nicht vorhanden
$sql = 'CREATE TABLE IF NOT EXISTS link (
link_id BIGINT(18) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
link_url VARCHAR(30) NOT NULL,
link_name VARCHAR(30),
reg_date TIMESTAMP
)';
$this->pdo->exec($sql); // use exec() because no results are returned
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
}
2022-01-08 22:43:50 +01:00
2022-01-09 00:05:57 +01:00
public function saveLink($link)
{
$statement = $this->pdo->prepare('INSERT INTO link(link_url, link_name) VALUES (:link_url, :link_name)');
$statement->execute
([
':link_url' => $link->url,
':link_name' => $link->name
]);
}
2022-01-08 22:43:50 +01:00
2022-01-09 22:05:20 +01:00
public function getListReverse()
{
// Mit verschachtelten Arrays optimieren!! Array schlüssel id enthält array mit schlüssel link_id, link_name, etc (repräsentiert datensatz)
$list = array();
$sql = "SELECT * FROM link ORDER BY link_id DESC";
return $this->pdo->query($sql);
}
public function getList()
2022-01-09 00:05:57 +01:00
{
// Mit verschachtelten Arrays optimieren!! Array schlüssel id enthält array mit schlüssel link_id, link_name, etc (repräsentiert datensatz)
$list = array();
$sql = "SELECT * FROM link";
2021-12-29 23:09:05 +01:00
2022-01-09 22:05:20 +01:00
return $this->pdo->query($sql);
2022-01-09 00:05:57 +01:00
}
}
// Main
$db = new dbLinkHAndler();
2021-12-28 23:49:45 +01:00
2022-01-09 00:05:57 +01:00
$link1=new Link("cloud.therealblue.de", "https://cloud.therealblue.de");
$db->saveLink($link1);
2022-01-09 22:05:20 +01:00
foreach ($db->getListReverse() as $link)
2022-01-09 00:05:57 +01:00
{
2022-01-09 22:05:20 +01:00
echo $link['link_id'].': <a href="'.$link['link_url'].'" target="_blank">'.$link['link_name'].'</a><br>';
2021-12-28 23:49:45 +01:00
}
?>