I wanted to try keeping the content of a website (mostly) in a SQL table and have made the basic template for the body. Set the $pageName
and include this template.
class Page {
public $content = [];
public $heading;
}
$page = new Page();
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
/* check connection*/
if ($conn->connect_errno) {
die("Failed to connect with MySQL: " . $conn->connect_errno);
};
$sql = "SELECT type, heading1, content1, style FROM $table WHERE page = '$pageName' ORDER BY ord";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
switch($row['type']){
case "content":
$page->content[] = array("content" => '<p>' . $row['content1'] . '</p>', "heading" => "<h3>" . $row['heading1'] . "</h3>", "style" => $row['style']);
break;
case "heading":
$page->heading = "<h1>" . $row['heading1'] . "</h1>";
if ($row['content1']) {
$page->heading .= "<h2>" . $row['content1'] . "</h2>";
}
}
}
mysqli_close($conn);
?>
<header>
<?php echo $page->heading; ?>
</header>
<?php
for ($i=0; $i < count($page->content); $i++) {
echo "<section class='content ".$page->content[$i]["style"]."'>";
echo $page->content[$i]["heading"];
echo $page->content[$i]["content"];
echo "</section>";
}
?>
I'm planning on adding more type
s than content
and heading
as needed. I will also add includes such as HTML head/meta, navbar, and footer.
I just wondered what people thought of this (besides "why?").