Permalink
Browse files

Merge branch 'master' of https://github.com/bwanicur/IntroToSQL

* 'master' of https://github.com/bwanicur/IntroToSQL:
  forgot the bootstrap css file
  week 5 blog app excersize
  • Loading branch information...
danielwrobert committed Apr 30, 2012
2 parents dce55f8 + 047cd73 commit 1b84eb1f9b586a6a48e9d6c0ad25dfea9e26bc71
Showing with 313 additions and 0 deletions.
  1. +198 −0 blog_app/blog_app.php
  2. +59 −0 blog_app/blog_app.sql
  3. +9 −0 blog_app/bootstrap.min.css
  4. +47 −0 week_5.sql
View
@@ -0,0 +1,198 @@
+<?php
+// NOTE: This PHP file is not a secure, scalable, or efficient way to build a web page. The sole purpose of this PHP page is to demonstrate how the SQL queries
+// and schema patterns learned in our class can be used in a web application. Doing this in one PHP file is a very transparent way to illustrate how we can use
+// what we have learned. DO NOT use this file as an example of web development.
+
+
+// helper function to insert post row
+function create_post($user_id, $title, $body){
+ // TODO:
+ $result = mysql_query("");
+ return $result;
+}
+
+// helper function to insert comment row
+function create_comment($post_id, $body){
+ // TODO:
+ $result = mysql_query("");
+ return $result;
+}
+
+// helper function to insert posts_labels row
+function create_post_label($post_id, $label_id){
+ // TODO:
+ $result = mysql_query("");
+ return $result;
+}
+
+
+// connect to DB
+$db_conn = mysql_connect(':/Applications/MAMP/tmp/mysql/mysql.sock', 'root', 'root');
+mysql_select_db('blog_app', $db_conn);
+
+// var_dump('POST: action: ' . $_POST['action'] . 'body: ' . $_POST['body'] . ' ' . 'title: ' . $_POST['title'] . ' ' . 'user_id: ' . $_POST['user_id']);
+
+// all users
+// TODO:
+$all_users_res = mysql_query("");
+
+// process action
+if($_POST['action'] && $_POST['action'] == 'add_post'){
+ create_post($_POST['user_id'], $_POST['title'], $_POST['body']);
+}
+else if($_POST['action'] && $_POST['action'] == 'add_comment'){
+ create_comment($_POST['post_id'], $_POST['body']);
+}
+else if($_POST['action'] && $_POST['action'] == 'add_label'){
+ create_post_label($_POST['post_id'], $_POST['label_id']);
+}
+
+// fetch user data
+$user_id = $_REQUEST['user_id'] ;
+// TODO:
+$user_res = mysql_query("");
+$user_data = mysql_fetch_assoc($user_res);
+
+// fetch user posts
+// TODO:
+$posts_res = mysql_query("");
+
+$num_posts_res = mysql_query("");
+$num_posts = mysql_fetch_array($num_posts_res);
+$num_posts = $num_posts[0];
+
+
+// TODO:
+$comments_query = <<<END
+
+END;
+
+?>
+
+
+<!DOCTYPE html>
+<html>
+<head>
+ <link type="text/css" rel="stylesheet" href="bootstrap.min.css" />
+ <style>
+ body{
+ background-color: #E6E6E6;
+ }
+ div.container div{
+ margin: 2px;
+ border-radius: 5px;
+ }
+ .inner {
+ margin: 0px;
+ padding: 0px;
+ }
+ .post {
+ background-color: #E6E6E6;
+ padding: 5px 5px 2px 10px;
+ }
+ .label {
+ background-color: #EFEFFB;
+ color: black;
+ }
+ .inner-content{
+ background-color: #FBF8EF;
+ padding: 5px;
+ margin-top: 10px;
+ }
+ #user-list{
+ padding: 50px 0px 50px 10px;
+ background-color: #E0F8E0;
+ }
+ </style>
+</head>
+<body>
+ <div class="container hero-unit">
+ <?php if($user_id){ ?>
+ <div id="user-posts" class="row span8">
+ <div id="welcome">
+ <h2>Welcome <?php echo $user_data['first_name'] . ' ' . $user_data['last_name']; ?></h2>
+ <p>Total Posts: <?php echo $num_posts; ?></p>
+ </div>
+ <div id="posts">
+ <?php while($post = mysql_fetch_assoc($posts_res)){ ?>
+ <div class="post inner">
+ <h4><?php echo $post['title']; ?></h4>
+ <div class="inner-content"><?php echo $post['body']; ?></div>
+ <?php
+ // TODO:
+ $labels_query = "";
+ $labels_res = mysql_query($labels_query);
+ ?>
+ <?php if(mysql_num_rows($labels_res) > 0){ ?>
+ <div class="labels inner">
+ <strong>Labels:</strong>
+ <?php while($label = mysql_fetch_assoc($labels_res)){ ?>
+ <?php echo '&nbsp;<span class="label">' . $label['name'] . '</span>&nbsp;'; ?>
+ <?php } ?>
+ </div>
+ <?php } ?>
+ <div id="label_form_div">
+ <strong>Add Label</strong>
+ <form action="blog_app.php?user_id=<?php echo $user_id; ?>" method="post" class="inner">
+ <input type="hidden" name="action" value="add_label" />
+ <input type="hidden" name="post_id" value="<?php echo $post['id']; ?>" />
+ <select name="label_id">
+ <option value="1">Sigil Related</option>
+ <option value="2">Snobbish</option>
+ <option value="3">Funny</option>
+ <option value="4">Whining</option>
+ </select>
+ <input type="submit" value="Add Label" />
+ </form>
+ </div>
+ <?php
+ // TODO:
+ $comments_query = "";
+ $comments_res = mysql_query($comments_query);
+ ?>
+ <?php if(mysql_num_rows($comments_res) > 0){ ?>
+ <div class="comments">
+ <h4>Comments:</h4>
+ <?php while($comment = mysql_fetch_assoc($comments_res)){ ?>
+ <div class="inner-content"><?php echo $comment['body']; ?></div>
+ <?php } ?>
+ </div>
+ <?php } ?>
+ <div id="comment_form_div">
+ <strong>Add Comment</strong>
+ <form action="blog_app.php?user_id=<?php echo $user_id; ?>" method="post" class="inner">
+ <input type="hidden" name="action" value="add_comment" />
+ <input type="hidden" name="post_id" value="<?php echo $post['id']; ?>" />
+ <textarea name="body"></textarea>
+ <input type="submit" value="Add Comment" />
+ </form>
+ </div>
+ </div>
+ <br />
+ <br/ >
+ <?php } ?>
+ </div>
+
+ <div id="add_post_form">
+ <h3>Create New Post</h3>
+ <form action="blog_app.php?user_id=<?php echo $user_id; ?>" method="post">
+ <input type="hidden" name="action" value="add_post" />
+ <input type="hidden" name="user_id" value="<?php echo $user_id; ?>" />
+ <input type="text" name="title" placeholder="Title" /><br />
+ <textarea name="body" placeholder="Post Goes Here" rows="10" cols="50"></textarea><br />
+ <input type="submit" value="Create Post" />
+ </form>
+ </div>
+ </div>
+ <?php } ?>
+ <div id="user-list" class="row span4">
+ <h3>Users</h3>
+ <ul>
+ <?php while($user = mysql_fetch_assoc($all_users_res)){ ?>
+ <li><a href="blog_app.php?user_id=<?php echo $user['id'] ?>"><?php echo $user['first_name'] . ' ' . $user['last_name']; ?></a></li>
+ <? } ?>
+ </ul>
+ </div>
+ </div>
+</body>
+</html>
View
@@ -0,0 +1,59 @@
+-- blog application database
+CREATE DATABASE blog_app;
+
+-- users table
+CREATE TABLE users (
+ id INT(11) AUTO_INCREMENT,
+ email VARCHAR(255) NOT NULL,
+ first_name VARCHAR(255),
+ last_name VARCHAR(255),
+ created_at TIMESTAMP DEFAULT NOW(),
+ PRIMARY KEY(id),
+ UNIQUE KEY(email)
+);
+INSERT INTO users (email, first_name, last_name) VALUES ('tyrion@lanister.com', 'Tyrion', 'Lanister');
+INSERT INTO users (email, first_name, last_name) VALUES ('jon@winterfell.com', 'Jon', 'Snow');
+INSERT INTO users (email, first_name, last_name) VALUES ('arya@winterfell.com', 'Arya', 'Stark');
+INSERT INTO users (email, first_name, last_name) VALUES ('tywin@lanister.com', 'Tywin', 'Lanister');
+INSERT INTO users (email, first_name, last_name) VALUES ('jamie@lanister.com', 'Jamie', 'Lanister');
+INSERT INTO users (email, first_name, last_name) VALUES ('rob@winterfell.com', 'Rob', 'Stark');
+INSERT INTO users (email, first_name, last_name) VALUES ('daenerys@targaryen.com', 'Daenerys', 'Targaryen');
+INSERT INTO users (email, first_name, last_name) VALUES ('varys@thespider.com', 'Lord', 'Varys');
+INSERT INTO users (email, first_name, last_name) VALUES ('peter@littlefinger.com', 'Peter', 'Baelish');
+
+-- posts table
+CREATE TABLE posts (
+ id INT(11) AUTO_INCREMENT,
+ user_id INT(11) NOT NULL,
+ title VARCHAR(255) NOT NULL,
+ body TEXT,
+ active TINYINT(1) DEFAULT 1,
+ created_at TIMESTAMP DEFAULT NOW(),
+ PRIMARY KEY(id),
+ KEY(user_id)
+);
+
+CREATE TABLE comments (
+ id INT(11) AUTO_INCREMENT,
+ post_id INT(11) NOT NULL,
+ body TEXT,
+ created_at TIMESTAMP DEFAULT NOW(),
+ PRIMARY KEY(id),
+ KEY(post_id)
+);
+
+CREATE TABLE labels (
+ id INT(11) AUTO_INCREMENT,
+ name VARCHAR(255) NOT NULL,
+ PRIMARY KEY(id)
+);
+INSERT INTO labels (name) VALUES ('Sigil Related');
+INSERT INTO labels (name) VALUES ('Snobbish');
+INSERT INTO labels (name) VALUES ('Funny');
+INSERT INTO labels (name) VALUES ('Whining');
+
+CREATE TABLE posts_labels (
+ post_id INT(11) NOT NULL,
+ label_id INT(11) NOT NULL,
+ UNIQUE KEY(post_id, label_id)
+);

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,47 @@
+-- week_5_authors
+CREATE TABLE authors_week_5 (
+ id INT(11) AUTO_INCREMENT,
+ email VARCHAR(255) NOT NULL,
+ first_name VARCHAR(255),
+ last_name VARCHAR(255),
+ created_at TIMESTAMP DEFAULT NOW(),
+ PRIMARY KEY(id),
+ UNIQUE KEY(email)
+);
+INSERT INTO authors_week_5 (email, first_name, last_name) VALUES ('ned@winterfell.com', 'Eddard', 'Stark');
+INSERT INTO authors_week_5 (email, first_name, last_name) VALUES ('jamie@lanister.com', 'Jamie', 'Lanister');
+INSERT INTO authors_week_5 (email, first_name, last_name) VALUES ('tyrion@lanister.com', 'Tyrion', 'Lanister');
+INSERT INTO authors_week_5 (email, first_name, last_name) VALUES ('jon@winterfell.com', 'Jon', 'Snow');
+INSERT INTO authors_week_5 (email, first_name, last_name) VALUES ('arya@winterfell.com', 'Arya', 'Stark');
+INSERT INTO authors_week_5 (email, first_name, last_name) VALUES ('tywin@lanister.com', 'Tywin', 'Lanister');
+
+-- week 5 posts
+CREATE TABLE posts_week_5 (
+ id INT(11) AUTO_INCREMENT,
+ author_id INT(11) NOT NULL,
+ title VARCHAR(255) NOT NULL,
+ body TEXT,
+ active TINYINT(1) DEFAULT 1,
+ created_at TIMESTAMP DEFAULT NOW(),
+ PRIMARY KEY(id)
+);
+INSERT INTO posts_week_5 (author_id, title, body) VALUES (3, "Tyrion's World", 'The world according to Tyrion.');
+INSERT INTO posts_week_5 (author_id, title, body) VALUES (5, "Why I am a Badass", 'I really do not have say why. That\'s how bad-assed I am.');
+INSERT INTO posts_week_5 (author_id, title, body) VALUES (6, "Lord Tywin's Daily Threat", 'I will destroy your family, root and stem.');
+INSERT INTO posts_week_5 (author_id, title, body) VALUES (2, "I'm Fantastic", 'Well, someone had to say it. No ego here.');
+INSERT INTO posts_week_5 (author_id, title, body) VALUES (7, "Missing Author", "I'm writing this post. As soon as I'm done, I'm deleting my account.");
+
+
+
+-- basic inner join
+SELECT a.first_name, a.last_name, p.title, p.body
+FROM authors_week_5 a INNER JOIN posts_week_5 p ON (a.id = p.author_id);
+
+-- left outer join
+SELECT a.id, a.first_name, a.last_name, p.id 'Post ID', p.title, p.body
+FROM authors_week_5 a LEFT OUTER JOIN posts_week_5 p ON (a.id = p.author_id);
+
+-- right outer join
+SELECT a.id 'Author ID', a.first_name, a.last_name, p.id 'Post ID', p.title, p.body
+FROM authors_week_5 a RIGHT OUTER JOIN posts_week_5 p ON (a.id = p.author_id);
+

0 comments on commit 1b84eb1

Please sign in to comment.