einfache Liste

master
Helge 2022-01-09 00:05:57 +01:00
parent 092e3db6b9
commit b99b5beab7
1 changed files with 59 additions and 33 deletions

View File

@ -1,23 +1,5 @@
<?php <?php
try {
// Verbindung zur Datenbank herstellen
$pdo = new PDO('mysql:host=localhost;dbname=linkdb', 'linkdb', 'linkdb');
$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
)';
$pdo->exec($sql); // use exec() because no results are returned
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
// Versuch der Objektorientierung // Versuch der Objektorientierung
@ -32,25 +14,69 @@ class Link{
} }
} }
$link1=new Link("therealblue.de", "https://therealblue.de"); class dbLinkHandler
{
private $pdo;
echo '<a href="'.$link1->url.'" target="_blank">'.$link1->name.'</a><br>'; 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);
/* Alt vor Obejktorientierung // Tabelle link erstellen, falls nicht vorhanden
$url = 'https://therealblue.de'; $sql = 'CREATE TABLE IF NOT EXISTS link (
$name = 'therealblue.de'; link_id BIGINT(18) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
link_url VARCHAR(30) NOT NULL,
link_name VARCHAR(30),
reg_date TIMESTAMP
)';
$statement = $pdo->prepare('INSERT INTO link(link_url, link_name) VALUES (:link_url, :link_name)'); $this->pdo->exec($sql); // use exec() because no results are returned
$statement->execute([ }
':link_url' => $url, catch(PDOException $e)
':link_name' => $name {
]); echo $sql . "<br>" . $e->getMessage();
}
$sql = "SELECT * FROM link"; }
foreach ($pdo->query($sql) as $row) {
echo '<a href="'.$row['link_url'].'" target="_blank">'.$row['link_name'].'</a><br>'; 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
]);
}
public function getLinkList()
{
// 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";
foreach ($this->pdo->query($sql) as $row)
{
$list[$row['link_name']] = $row['link_url'];
}
return $list;
}
}
// Main
$db = new dbLinkHAndler();
$link1=new Link("cloud.therealblue.de", "https://cloud.therealblue.de");
$db->saveLink($link1);
foreach($db->getLinkList() as $url => $name)
{
echo '<a href="'.$url.'" target="_blank">'.$name.'</a><br>';
} }
*/
?> ?>