Skip to content

Instantly share code, notes, and snippets.

@scoumbourdis
Created June 15, 2020 10:39
Show Gist options
  • Save scoumbourdis/6300ddbfc347b4e9e4b975d751d9d4c0 to your computer and use it in GitHub Desktop.
Save scoumbourdis/6300ddbfc347b4e9e4b975d751d9d4c0 to your computer and use it in GitHub Desktop.
This is an example of a messy code. Please do not copy any of this code as this is just an example of a bad code
<?php
// This is an example of a messy code. Please do not copy any of this code as this is just an example of a bad code.
if (!empty($_GET['id'])) {
$id = $_GET['id'];
if (!is_numeric($id)) {
header("HTTP/1.0 404 Not Found");
echo "<h1>⚠️ We are sorry, page not found</h1>";
die;
}
} else {
echo "<h1>Show all blog posts</h1>";
}
$credentials = include('credentials.php');
$servername = "localhost";
$username = $credentials->username;
$password = $credentials->password;
$databaseName = "tutorial_blog";
// Create connection
$conn = new mysqli($servername, $username, $password, $databaseName);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
function getPosts($conn) {
$sql = "SELECT `id`, `title`, `small_description`, `author`, `image_path`, `insert_datetime` FROM `posts` ORDER BY insert_datetime DESC";
$result = $conn->query($sql);
return $result->fetch_all(MYSQLI_ASSOC);
}
function getPost($conn, $id) {
$query = "
SELECT `title`, `small_description`, `author`, `full_post` , `image_path`, `insert_datetime`
FROM `posts`
WHERE id = ?
";
$stmt = $conn->prepare($query);
$stmt->bind_param("s", $id);
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_assoc();
}
if (!empty($id)) {
$post = getPost($conn, $id);
} else {
$posts = getPosts($conn);
}
$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Clean Blog - Start Bootstrap Theme</title>
<!-- Bootstrap core CSS -->
<link href="https://blackrockdigital.github.io/startbootstrap-clean-blog/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom fonts for this template -->
<link href="https://blackrockdigital.github.io/startbootstrap-clean-blog/vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
<link href='https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
<!-- Custom styles for this template -->
<link href="https://blackrockdigital.github.io/startbootstrap-clean-blog/css/clean-blog.min.css" rel="stylesheet">
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-light fixed-top" id="mainNav">
<div class="container">
<a class="navbar-brand" href="index.html">Start Bootstrap</a>
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
Menu
<i class="fas fa-bars"></i>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="about.html">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="post.html">Sample Post</a>
</li>
<li class="nav-item">
<a class="nav-link" href="contact.html">Contact</a>
</li>
</ul>
</div>
</div>
</nav>
<?php if (!empty($posts)) { ?>
<!-- Page Header -->
<header class="masthead" style="background-image: url('https://blackrockdigital.github.io/startbootstrap-clean-blog/img/home-bg.jpg')">
<div class="overlay"></div>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="site-heading">
<h1>Clean Blog</h1>
<span class="subheading">A Blog Theme by Start Bootstrap</span>
</div>
</div>
</div>
</div>
</header>
<?php } ?>
<?php if (!empty($post)) {
$prettierDate = date_format(date_create($post['insert_datetime']), 'l jS F Y');
?>
<header class="masthead" style="background-image: url('https://blackrockdigital.github.io/startbootstrap-clean-blog/<?php echo $post['image_path'];?>')">
<div class="overlay"></div>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="post-heading">
<h1><?php echo $post['title'];?></h1>
<h2 class="subheading"><?php echo $post['small_description'];?></h2>
<span class="meta">Posted by
<a href="#"><?php echo $post['author'];?></a>
on <?php echo $prettierDate; ?></span>
</div>
</div>
</div>
</div>
</header>
<?php } ?>
<!-- Main Content -->
<?php if (!empty($post)) { ?>
<article>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<?php echo $post['full_post']; ?>
</div>
</div>
</div>
</article>
<?php } ?>
<?php if (!empty($posts)) { ?>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<?php foreach ($posts as $post) {
$prettierDate = date_format(date_create($post['insert_datetime']), 'l jS F Y');
?>
<div class="post-preview">
<a href="blog.php?id=<?php echo $post['id']; ?>">
<h2 class="post-title">
<?php echo $post['title']; ?>
</h2>
<h3 class="post-subtitle">
<?php echo $post['small_description']; ?>
</h3>
</a>
<p class="post-meta">Posted by
<a href="#"><?php echo $post['author'];?></a>
on <?php echo $prettierDate; ?></p>
</div>
<hr>
<?php }?>
<!-- Pager -->
<div class="clearfix">
<a class="btn btn-primary float-right" href="#">Older Posts &rarr;</a>
</div>
</div>
</div>
</div>
<?php }?>
<hr>
<!-- Footer -->
<footer>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<ul class="list-inline text-center">
<li class="list-inline-item">
<a href="#">
<span class="fa-stack fa-lg">
<i class="fas fa-circle fa-stack-2x"></i>
<i class="fab fa-twitter fa-stack-1x fa-inverse"></i>
</span>
</a>
</li>
<li class="list-inline-item">
<a href="#">
<span class="fa-stack fa-lg">
<i class="fas fa-circle fa-stack-2x"></i>
<i class="fab fa-facebook-f fa-stack-1x fa-inverse"></i>
</span>
</a>
</li>
<li class="list-inline-item">
<a href="#">
<span class="fa-stack fa-lg">
<i class="fas fa-circle fa-stack-2x"></i>
<i class="fab fa-github fa-stack-1x fa-inverse"></i>
</span>
</a>
</li>
</ul>
<p class="copyright text-muted">Copyright &copy; Your Website 2019</p>
</div>
</div>
</div>
</footer>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment