Skip to content

Instantly share code, notes, and snippets.

@christurnertv
Created September 12, 2014 20:55
Show Gist options
  • Save christurnertv/0cc0fbb1ba44a3c914ff to your computer and use it in GitHub Desktop.
Save christurnertv/0cc0fbb1ba44a3c914ff to your computer and use it in GitHub Desktop.
Super Ad Site - Friday Review
<?php
require_once('AdManager.class.php');
require_once('Ad.class.php');
if (!empty($_POST))
{
// load existing ads
$adManager = new AdManager();
$ads = $adManager->loadAds();
// init new ad
$createdAt = date('D, d M Y H:i');
$ad = new Ad($_POST['title'], $_POST['body'], $_POST['contact_name'], $_POST['contact_email'], $createdAt);
// add new ad to current ads
$ads[] = $ad;
// save all ads
$adManager->saveAds($ads);
// redirect to ad show view
header('location: ad-view.php?id=' . (count($ads) - 1));
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_once('AdManager.class.php');
require_once('Ad.class.php');
$adManager = new AdManager();
$ads = $adManager->loadAds();
$adId = $_GET['id'];
$ad = $ads[$adId];
?>
<?php include('header.php'); ?>
<div class="container">
<h1><?= htmlspecialchars($ad->title); ?></h1>
<p>Posted at: <?= htmlspecialchars($ad->createdAt); ?></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 $title = '';
public $body = '';
public $contactName = '';
public $contactEmail = '';
public $createdAt = '';
public function __construct($title, $body, $contactName, $contactEmail, $createdAt)
{
$this->title = $title;
$this->body = $body;
$this->contactName = $contactName;
$this->contactEmail = $contactEmail;
$this->createdAt = $createdAt;
}
public function toArray()
{
return [$this->title, $this->body, $this->contactName, $this->contactEmail, $this->createdAt];
}
}
<?php
require_once('Ad.class.php');
class AdManager {
public $dataFile = "";
public function __construct($dataFile = "ads.csv")
{
$this->dataFile = $dataFile;
}
public function loadAds()
{
$handle = fopen($this->dataFile, 'r');
$ads = [];
while(!feof($handle))
{
$csvRow = fgetcsv($handle);
// make sure csv row had data
if (!empty($csvRow[0]))
{
$ad = new Ad($csvRow[0], $csvRow[1], $csvRow[2], $csvRow[3], $csvRow[4]);
$ads[] = $ad;
}
}
fclose($handle);
return $ads;
}
public function saveAds($ads)
{
$handle = fopen($this->dataFile, 'w');
foreach ($ads as $ad) {
fputcsv($handle, $ad->toArray());
}
fclose($handle);
}
}
New Car Awesome new car. Drives great... honest.. :) Joe Smith joe@mailinator.com Fri, 12 Sep 2014 20:27
Super NES Still works. It's totally awesome. Comes with mario kart! Chris chris@mailinator.com Fri, 12 Sep 2014 20:28
<?php
require_once('AdManager.class.php');
require_once('Ad.class.php');
$adManager = new AdManager();
$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 $index => $ad) : ?>
<tr>
<td><a href="ad-view.php?id=<?= $index; ?>"><?= $ad->title; ?></a></td>
<td><?= $ad->contactName; ?></td>
<td><?= $ad->createdAt; ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
<?php include('footer.php'); ?>
<!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