Advertisement
Guest User

Untitled

a guest
Nov 20th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.20 KB | None | 0 0
  1. <?php
  2.     class Post {
  3.       public $title;
  4.       public $content;
  5.       public $timestamp;
  6.       public $tags;
  7.     }
  8.  
  9.     function get_posts($limit) {
  10.       // connect to database
  11.       $host     = "localhost";
  12.       $username = "weblog";
  13.       $password = "deadcats";
  14.       $database = "weblog";
  15.  
  16.       $db_conn = mysqli_connect($host,$username,$password,$database);
  17.       if (!$db_conn) {
  18.         echo "Error: Unable to connect to MySQL." . PHP_EOL;
  19.         echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
  20.         echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
  21.         exit;
  22.       }
  23.  
  24.       $tag_filter =  htmlspecialchars($_GET["tag"]);
  25.  
  26.       $posts = array();
  27.  
  28.       if (!empty($tag_filter)) {
  29.         $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}\"";
  30.       }
  31.  
  32.       $sql = "SELECT posts.id, posts.title, posts.content, posts.timestamp FROM posts " . $postfix . " ORDER BY posts.id DESC";
  33.       $result = $db_conn->query($sql);
  34.  
  35.       if ($result->num_rows > 0) {
  36.           while($row = $result->fetch_assoc()) {
  37.             $post = new Post();
  38.  
  39.             $post->id         = $row["id"];
  40.             $post->title      = $row["title"];
  41.             $post->content    = $row["content"];
  42.             $post->timestamp  = $row["timestamp"];
  43.  
  44.             $post->tags = array();
  45.             $tags_sql = "SELECT tags.name FROM posts_tags
  46.                        JOIN tags ON tags.id = posts_tags.tag_id
  47.                        WHERE posts_tags.post_id = {$post->id}";
  48.  
  49.             $tags_result = $db_conn->query($tags_sql);
  50.             while($tags_row = $tags_result->fetch_assoc()) {
  51.               array_push($post->tags, $tags_row["name"]);
  52.             }
  53.  
  54.             array_push($posts, $post);
  55.           }
  56.       }
  57.  
  58.       mysqli_close($db_conn);
  59.  
  60.       return $posts;
  61.     }
  62.  
  63.     function print_post($post) {
  64.       $tags = "";
  65.       foreach ($post->tags as $tag) {
  66.         $tags .= " <a href=\"index.php?tag=\"" . $tag . "\">$tag</a>";
  67.       }
  68.  
  69.       $output =  "<div class=\"common_item\">
  70.                    <div class=\"p_title\">{$post->title}</div>
  71.                    <hr align=\"left\">
  72.                    <p class=\"text\">{$post->content}</p>
  73.                    <div class=\"post_description\">
  74.                      <div class=\"left\">
  75.                        <b>Tags:</b> {$tags}
  76.                      </div>
  77.                      <div class=\"right\">
  78.                        Posted on {$post->timestamp}
  79.                      </div>
  80.                    </div>
  81.                  </div>";
  82.  
  83.       echo $output;
  84.     }
  85. ?>
  86.  
  87. <!doctype html>
  88. <html>
  89. <head>
  90.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <link rel="stylesheet alternative" type="text/css" href="dark.css" id="dark-style">
  91.     <link rel="stylesheet" type="text/css" href="light.css" id="light-style">
  92.     <script type="text/javascript" src="scheme.js"></script>
  93.     <title>Weblog about web-technologies</title>
  94. </head>
  95.  
  96. <body>
  97. <div id="content">
  98.     <div id="header">
  99.         <h2><a href="index.php">Weblog about web-technologies</a></h2>
  100.         <h3><p class="annotation">Lorem ipsum dolor sit amet</p></h3>
  101.     </div>
  102.  
  103. <?php
  104.     $posts = get_posts();
  105.  
  106.     foreach ($posts as $post) {
  107.       print_post($post);
  108.     }
  109. ?>
  110.  
  111.   </div>
  112.  
  113. <div id="sidebar">
  114.     <h3>Navigation</h3>
  115.     <div id="list_n">
  116.     <ul>
  117.         <li><a href="#C1">Log in</a></li>
  118.     </ul>
  119.     </div>
  120.  
  121.     <h3>References</h3>
  122.     <div id="list_r">
  123.     <ul>
  124.         <li><a href="https://en.wikipedia.org/wiki/HTML">HTML in Wiki</a></li>
  125.         <li><a href="http://www.w3schools.com/cssreF/">CSS reference</a></li>
  126.         <li><a href="https://www.w3.org/standards/techs/css#w3c_all">W3C.org</a></li>
  127.     </ul>
  128.     </div>
  129.  
  130.     <h3>Switch theme</h3>
  131.     <div id="list_s">
  132.     <ul>
  133.         <li><a href="#" onclick="changeCSS('light', 0);">Light</a></li>
  134.         <li><a href="#" onclick="changeCSS('dark', 0);">Vintage</a></li>
  135.     </ul>
  136.     </div>
  137. </div>
  138.  
  139. <div id="footer">
  140.     Assembled by <a href="https://github.com/innermous">innermous</a> in 2016
  141. </div>
  142.  
  143. </body>
  144. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement