Skip to content

Instantly share code, notes, and snippets.

@bbatsche
Forked from christurnertv/Ad.class.php
Last active August 29, 2015 14:06
Show Gist options
  • Save bbatsche/7b75f5f7e642614d5d96 to your computer and use it in GitHub Desktop.
Save bbatsche/7b75f5f7e642614d5d96 to your computer and use it in GitHub Desktop.
<?php
require 'dbconnect.php';
require_once('Ad.class.php');
if (!empty($_POST)) {
// init new ad
$ad = new Ad($dbc);
$ad->title = $_POST['title'];
$ad->body = $_POST['body'];
$ad->contactName = $_POST['contact_name'];
$ad->contactEmail = $_POST['contact_email'];
// save all ads
$ad->save();
// redirect to ad show view
header('location: ad-view.php?id=' . $ad->id);
exit;
}
?>
<?php include('header.php'); ?>
<div class="container">
<h1>Create a New Ad</h1>
<form role="form" method="POST">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" id="title" name="title" placeholder="A descriptive title for your ad">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" id="body" name="body" rows="6"></textarea>
</div>
<div class="form-group">
<label for="contact_name">Contact Name</label>
<input type="text" class="form-control" id="contact_name" name="contact_name" placeholder="Who you gonna call?">
</div>
<div class="form-group">
<label for="contact_email">Contact Email</label>
<input type="email" class="form-control" id="contact_email" name="contact_email" placeholder="Email address to contact at">
</div>
<a href="ads.php" class="btn btn-default">Cancel</a>
<button type="submit" class="btn btn-primary">Create Ad</button>
</form>
</div>
<?php include('footer.php'); ?>
<?php
require 'dbconnect.php';
require_once('Ad.class.php');
$adId = $_GET['id'];
$ad = new Ad($dbc, $adId);
if (!empty($_POST)) {
$ad->title = $_POST['title'];
$ad->body = $_POST['body'];
$ad->contactName = $_POST['contact_name'];
$ad->contactEmail = $_POST['contact_email'];
// save all ads
$ad->save();
// redirect to ad show view
header('location: ad-view.php?id=' . $ad->id);
exit;
}
?>
<?php include('header.php'); ?>
<div class="container">
<h1>Create a New Ad</h1>
<form role="form" method="POST">
<div class="form-group">
<label for="title">Title</label>
<input type="text" value="<?= $ad->title; ?>" class="form-control" id="title" name="title" placeholder="A descriptive title for your ad">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" id="body" name="body" rows="6"><?= $ad->body; ?></textarea>
</div>
<div class="form-group">
<label for="contact_name">Contact Name</label>
<input type="text" value="<?= $ad->contactName; ?>" class="form-control" id="contact_name" name="contact_name" placeholder="Who you gonna call?">
</div>
<div class="form-group">
<label for="contact_email">Contact Email</label>
<input type="email" value="<?= $ad->contactEmail; ?>" class="form-control" id="contact_email" name="contact_email" placeholder="Email address to contact at">
</div>
<a href="ads.php" class="btn btn-default">Cancel</a>
<button type="submit" class="btn btn-primary">Update Ad</button>
</form>
</div>
<?php include('footer.php'); ?>
<?php
require 'dbconnect.php';
require_once('Ad.class.php');
$adId = $_GET['id'];
$ad = new Ad($dbc, $adId);
?>
<?php include('header.php'); ?>
<div class="container">
<h1>
<?= htmlspecialchars($ad->title); ?>
<small><a href="ad-edit.php?id=<?= $ad->id; ?>">Edit</a></small>
</h1>
<p>Posted at: <?= htmlspecialchars($ad->createdAt->format('l, F jS, Y')); ?></p>
<p><?= htmlspecialchars($ad->body); ?></p>
<h2>Contact Info:</h2>
<p>
<?= htmlspecialchars($ad->contactName); ?><br>
<a href="mailto:<?= htmlspecialchars($ad->contactEmail); ?>"><?= htmlspecialchars($ad->contactEmail); ?></a>
</p>
</div>
<?php include('footer.php'); ?>
<?php
class Ad {
public $dbc;
public $id;
public $title = '';
public $body = '';
public $contactName = '';
public $contactEmail = '';
public $createdAt = '';
public function __construct($dbc, $id = null)
{
$this->dbc = $dbc;
if (isset($id)) {
$this->id = $id;
$selectStmt = $this->dbc->prepare('SELECT * FROM items WHERE id = ?');
$selectStmt->execute([$this->id]);
$row = $selectStmt->fetch(PDO::FETCH_ASSOC);
$this->title = $row['title'];
$this->body = $row['body'];
$this->contactName = $row['name'];
$this->contactEmail = $row['email'];
$this->createdAt = new DateTime($row['created_at']);
}
}
public function save()
{
if (isset($this->id)) {
$this->update();
} else {
$this->insert();
}
}
protected function insert()
{
$this->createdAt = new DateTime();
$insertSql = 'INSERT INTO items (title, body, name, email, created_at)
VALUES (:title, :body, :name, :email, :created_at)';
$insertStmt = $this->dbc->prepare($insertSql);
$insertStmt->bindValue(':title', $this->title, PDO::PARAM_STR);
$insertStmt->bindValue(':body', $this->body, PDO::PARAM_STR);
$insertStmt->bindValue(':name', $this->contactName, PDO::PARAM_STR);
$insertStmt->bindValue(':email', $this->contactEmail, PDO::PARAM_STR);
$insertStmt->bindValue(':created_at', $this->createdAt->format('c'), PDO::PARAM_STR);
$insertStmt->execute();
$this->id = $this->dbc->lastInsertId();
}
protected function update()
{
$updateSql = 'UPDATE items
SET title = :title, body = :body, name = :name, email = :email
WHERE id = :id';
$updateStmt = $this->dbc->prepare($updateSql);
$updateStmt->bindValue(':title', $this->title, PDO::PARAM_STR);
$updateStmt->bindValue(':body', $this->body, PDO::PARAM_STR);
$updateStmt->bindValue(':name', $this->contactName, PDO::PARAM_STR);
$updateStmt->bindValue(':email', $this->contactEmail, PDO::PARAM_STR);
$updateStmt->bindValue(':id', $this->id, PDO::PARAM_INT);
$updateStmt->execute();
}
}
<?php
require_once('Ad.class.php');
class AdManager {
public $dbc;
public function __construct($dbc)
{
$this->dbc = $dbc;
}
public function loadAds()
{
$adsStmt = $this->dbc->query('SELECT id FROM items');
$ads = [];
while($row = $adsStmt->fetch(PDO::FETCH_ASSOC))
{
$ad = new Ad($this->dbc, $row['id']);
$ads[] = $ad;
}
return $ads;
}
}
<?php
require 'dbconnect.php';
require_once('AdManager.class.php');
require_once('Ad.class.php');
$adManager = new AdManager($dbc);
$ads = $adManager->loadAds();
?>
<?php include('header.php'); ?>
<div class="container">
<div class="jumbotron">
<h1>Welcome to the Super Ad Site!</h1>
<p>Here you can list your awesome junk and sell it lightning fast!</p>
</div>
<table class="table table-striped">
<?php foreach ($ads as $ad) : ?>
<tr>
<td><a href="ad-view.php?id=<?= $ad->id; ?>"><?= $ad->title; ?></a></td>
<td><?= $ad->contactName; ?></td>
<td><?= $ad->createdAt->format('l, F jS, Y'); ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
<?php include('footer.php'); ?>
# ************************************************************
# Sequel Pro SQL dump
# Version 4096
#
# http://www.sequelpro.com/
# http://code.google.com/p/sequel-pro/
#
# Host: 127.0.0.1 (MySQL 5.5.38-0ubuntu0.14.04.1)
# Database: adlister
# Generation Time: 2014-09-19 21:06:16 +0000
# ************************************************************
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
# Dump of table categories
# ------------------------------------------------------------
CREATE TABLE categories (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
type VARCHAR(127) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY category_type_unq (type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
# Dump of table items
# ------------------------------------------------------------
CREATE TABLE items (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
body TEXT NOT NULL,
name VARCHAR(127) NOT NULL,
email VARCHAR(63) NOT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
# Dump of table item_category
# ------------------------------------------------------------
CREATE TABLE item_category (
item_id INT UNSIGNED NOT NULL,
category_id INT UNSIGNED NOT NULL,
PRIMARY KEY (item_id,category_id),
KEY category_id_fk (category_id),
CONSTRAINT category_id_fk FOREIGN KEY (category_id) REFERENCES categories (id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT item_id_fk FOREIGN KEY (item_id) REFERENCES items (id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
<?php
// Get new instance of PDO object
$dbc = new PDO('mysql:host=127.0.0.1;dbname=adlister', 'codeup', 'codeuprocks');
// Tell PDO to throw exceptions on error
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Super Ad Site</title>
<!-- Bootstrap core CSS -->
<link href="/css/bootstrap.min.css" rel="stylesheet">
<link href="/css/main.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body role="document">
<?php include('navbar.php'); ?>
body {
padding-top: 70px;
}
<!-- Fixed navbar -->
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/ads.php">Super Ad Site</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="<?php echo ($_SERVER['PHP_SELF'] == '/ads.php') ? 'active' : ''; ?>"><a href="ads.php">Home</a></li>
<li class="<?php echo ($_SERVER['PHP_SELF'] == '/ad-create.php') ? 'active' : ''; ?>"><a href="ad-create.php">New Ad</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment