Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Post {
- public $title;
- public $content;
- public $timestamp;
- public $tags;
- }
- function get_posts($limit) {
- // connect to database
- $host = "localhost";
- $username = "weblog";
- $password = "deadcats";
- $database = "weblog";
- $db_conn = mysqli_connect($host,$username,$password,$database);
- if (!$db_conn) {
- echo "Error: Unable to connect to MySQL." . PHP_EOL;
- echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
- echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
- exit;
- }
- $tag_filter = htmlspecialchars($_GET["tag"]);
- $posts = array();
- if (!empty($tag_filter)) {
- $postfix = "JOIN posts_tags ON posts.id = posts_tags.post_id JOIN tags ON tags.id = posts_tags.tag_id WHERE tags.name = \"{$tag_filter}\"";
- }
- $sql = "SELECT posts.id, posts.title, posts.content, posts.timestamp FROM posts " . $postfix . " ORDER BY posts.id DESC";
- $result = $db_conn->query($sql);
- if ($result->num_rows > 0) {
- while($row = $result->fetch_assoc()) {
- $post = new Post();
- $post->id = $row["id"];
- $post->title = $row["title"];
- $post->content = $row["content"];
- $post->timestamp = $row["timestamp"];
- $post->tags = array();
- $tags_sql = "SELECT tags.name FROM posts_tags
- JOIN tags ON tags.id = posts_tags.tag_id
- WHERE posts_tags.post_id = {$post->id}";
- $tags_result = $db_conn->query($tags_sql);
- while($tags_row = $tags_result->fetch_assoc()) {
- array_push($post->tags, $tags_row["name"]);
- }
- array_push($posts, $post);
- }
- }
- mysqli_close($db_conn);
- return $posts;
- }
- function print_post($post) {
- $tags = "";
- foreach ($post->tags as $tag) {
- $tags .= " <a href=\"index.php?tag=\"" . $tag . "\">$tag</a>";
- }
- $output = "<div class=\"common_item\">
- <div class=\"p_title\">{$post->title}</div>
- <hr align=\"left\">
- <p class=\"text\">{$post->content}</p>
- <div class=\"post_description\">
- <div class=\"left\">
- <b>Tags:</b> {$tags}
- </div>
- <div class=\"right\">
- Posted on {$post->timestamp}
- </div>
- </div>
- </div>";
- echo $output;
- }
- ?>
- <!doctype html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <link rel="stylesheet alternative" type="text/css" href="dark.css" id="dark-style">
- <link rel="stylesheet" type="text/css" href="light.css" id="light-style">
- <script type="text/javascript" src="scheme.js"></script>
- <title>Weblog about web-technologies</title>
- </head>
- <body>
- <div id="content">
- <div id="header">
- <h2><a href="index.php">Weblog about web-technologies</a></h2>
- <h3><p class="annotation">Lorem ipsum dolor sit amet</p></h3>
- </div>
- <?php
- $posts = get_posts();
- foreach ($posts as $post) {
- print_post($post);
- }
- ?>
- </div>
- <div id="sidebar">
- <h3>Navigation</h3>
- <div id="list_n">
- <ul>
- <li><a href="#C1">Log in</a></li>
- </ul>
- </div>
- <h3>References</h3>
- <div id="list_r">
- <ul>
- <li><a href="https://en.wikipedia.org/wiki/HTML">HTML in Wiki</a></li>
- <li><a href="http://www.w3schools.com/cssreF/">CSS reference</a></li>
- <li><a href="https://www.w3.org/standards/techs/css#w3c_all">W3C.org</a></li>
- </ul>
- </div>
- <h3>Switch theme</h3>
- <div id="list_s">
- <ul>
- <li><a href="#" onclick="changeCSS('light', 0);">Light</a></li>
- <li><a href="#" onclick="changeCSS('dark', 0);">Vintage</a></li>
- </ul>
- </div>
- </div>
- <div id="footer">
- Assembled by <a href="https://github.com/innermous">innermous</a> in 2016
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement