Worked out a class to easily include subpages/content and put the footer in a function

This commit is contained in:
Jack-Benny Persson 2014-06-28 15:13:12 +02:00
parent d5077b085a
commit 2f5b8734c4
7 changed files with 122 additions and 21 deletions

35
content.php Normal file
View File

@ -0,0 +1,35 @@
<?php
class Page
{
public $name, $filename;
public static $contentFolder = "content/";
public function __construct($name, $filename)
{
$this->name = $name;
$this->filename = $filename;
}
public function createPage()
{
include ("{$contentFolder}{$this->filename}.html");
}
public function createMenuItem()
{
// Match just the name, without the .html, .php etc parc
preg_match_all("/[a-z_\-0-9]*/i", $this->filename, $out);
$bareFilename = $out[0][0];
// Print the menu item
print ("<li class=\"link\"><a href=\"index.php?content={$bareFilename}\">" .
$this->name . "</a></li>\n");
}
public function getName()
{
return $this->name;
}
}
?>

4
content/about.html Normal file
View File

@ -0,0 +1,4 @@
<h2>About</h2>
<p>
This is my blog about everything and nothing, please enjoy!
</p>

6
content/contact.html Normal file
View File

@ -0,0 +1,6 @@
<h2>Contact</h2>
<p>
Please feel free to contact me at <a href="mailto:name@example.tld">name@example.tld</a>
</p>

View File

@ -3,10 +3,10 @@ require "config.php";
// Connect to MySQL database
$link = mysql_connect($host, $user, $password)
or die("Could not connect...");
or terminate("Could not connect...");
mysql_select_db($database)
or die("Could not open database");
or terminate("Could not open database");
?>

View File

@ -15,4 +15,16 @@ function end_html()
print "\n\n</body>\n</html>\n";
}
function footer()
{
print "
</div>
<div id=\"footer\">
<p>&copy; 2014 - Jack-Benny Persson</p>
</div>
</div>
</body>
</html>";
}
?>

8
includes/miscfunc.php Normal file
View File

@ -0,0 +1,8 @@
<?php
function terminate($message="")
{
print "$message\n";
footer();
exit(1);
}
?>

View File

@ -18,6 +18,19 @@
-->
<?php
require ("content.php");
require ("includes/htmlcode.php");
require ("includes/config.php");
require ("includes/miscfunc.php");
/* For our subpages (About, Contact etc)
First argument is the name of link as it should appear in the menu, second
argument is the filename of file in content/ without directory, slashed etc.
*/
$aboutPage = new Page("About", "about.html");
$contactPage = new Page("Contact", "contact.html");
?>
<!DOCTYPE html>
@ -35,10 +48,12 @@
<div id="navbar">
<div id="navlink-right">
<ul class="ul-links">
<li class="link"><a href="#">Home</a></li>
<li class="link"><a href="#">About</a></li>
<li class="link"><a href="#">Articles</a></li>
<li class="link"><a href="#">Contact</a></li>
<li class="link"><a href="index.php">Home</a></li>
<?php
// Add menu items here! ($object->createMenuItem();)
$aboutPage->createMenuItem();
$contactPage->createMenuItem();
?>
<li class="link"><a href="user/">Login</a></li>
</ul>
</div>
@ -49,11 +64,38 @@
<div id="content">
<?php
// Subpage content begin
if (isset($_GET['content']))
{
$content = $_GET['content'];
function nodir($item)
{
return (!is_dir(Page::$contentFolder . $item));
}
$dirContent = scandir(Page::$contentFolder);
// Include config file and other include-files
require "includes/config.php";
require "includes/dbconnect.php";
$files = (array_filter($dirContent, "nodir"));
foreach($files as $file)
{
preg_match_all("/[a-z_\-0-9]*/i", $file, $withoutExt);
if ($withoutExt[0][0] == $content)
{
include (Page::$contentFolder . $file);
}
}
terminate();
}
// Subpage content ends
// Simplog (blog posts etc)
// Connect to db (it's down here to show the blogpage even in cases
// where of failure to connect)
require ("includes/dbconnect.php");
// Divide the posts into pages, N number of posts on every page
if (isset($_GET['page']))
{
@ -65,13 +107,13 @@
}
$start = ($page-1) * $posts_per_page;
// Fetch posts
$query = "SELECT * FROM blog ORDER BY date DESC LIMIT $start,
$posts_per_page";
$result = mysql_query($query)
or die("No matching queries...<br /> It seems you either " .
"haven't started blogging yet or haven't installed the".
" the database table yet<br/>");
or terminate("No matching queries...<br /> It seems you " .
"either haven't started blogging yet or haven't " .
"installed the the database table yet<br/>");
// Printing posts in HTML
while ($line = mysql_fetch_array($result))
@ -92,20 +134,14 @@
print "<strong>Page: ";
for ($i=1; $i<=$total_posts; $i++)
{
print "<a href='blogsite.php?page=".$i."'>".$i."</a> ";
print "<a href='index.php?page=".$i."'>".$i."</a> ";
}
print "</strong>";
// Close MySQL link
require "includes/dbclose.php";
footer();
?>
</div>
<div id="footer">
<p>&copy; 2014 - Jack-Benny Persson</p>
</div>
</div>
</body>
</html>