Skip to content

Commit

Permalink
pre Ajax commit
Browse files Browse the repository at this point in the history
  • Loading branch information
paleomedia committed Apr 27, 2015
1 parent b5706a3 commit df2db74
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 112 deletions.
3 changes: 2 additions & 1 deletion JS/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ $(function() {
$("#form").submit(function(){
var values = $("form").serialize();
var comment = $("#comment").val();

console.log(values);
$.ajax({
type: "POST",
url: "handlerAjax.php",
url: "handler.php",
data: values,
success: function() {
$("#comments tbody").prepend("<tr><td>" +
Expand Down
15 changes: 15 additions & 0 deletions api_functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

function last_action($bill_id) {

$api_key = "/?apikey=bcc2a830883c4f459dbffe94b2a3e90f";
$url_base = "http://openstates.org/api/v1/";
$url = $url_base . 'bills/' . $bill_id . $api_key;
$bill_json = file_get_contents($url);
$bill_detail = json_decode($bill_json, true);

return strtok($bill_detail[action_dates][last], " ") ;

}

?>
20 changes: 20 additions & 0 deletions api_getter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
// api_getter.php
// class for saving and getting info to/from API

class api_getter {

private $api_key = "/?apikey=bcc2a830883c4f459dbffe94b2a3e90f";
private $url_base = "http://openstates.org/api/v1/";

public function last_action($bill_id) {
$url_base = $this->url_base
$key = $this->api_key
$url = $url_base . 'bills/' . $bill_id . $key;
$bill_json = file_get_contents($url);
$bill_detail = json_decode($bill_json, true);
return $bill_detail[action_dates][last];
}

}
?>
19 changes: 11 additions & 8 deletions dao.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
// Dao.php
// dao.php
// class for saving and getting info to/from MySQL

class Dao {
Expand Down Expand Up @@ -43,21 +43,24 @@ public function redirect($url, $flash_message = NULL) {
die;
}

public function saveComment ($comment) {
public function saveComment ($username, $comment, $bill, $comment_type) {
$conn = $this->getConnection();
$saveQuery =
"INSERT INTO comments
(comment)
(username, comment, bill_id, comment_type)
VALUES
(:comment)";
(:username, :comment, :bill, :comment_type)";
$q = $conn->prepare($saveQuery);
$q->bindParam(":username", $username);
$q->bindParam(":comment", $comment);
$q->execute();
$q->bindParam(":bill", $bill);
$q->bindParam(":comment_type", $comment_type);
return $q->execute();
}

public function getComments () {
public function getComments ($bill, $comment_type) {
$conn = $this->getConnection();
return $conn->query("SELECT * FROM comments");
return $conn->query("SELECT username, comment, date FROM comments WHERE bill_id = '$bill' AND comment_type = '$comment_type'");
}

public function newUser ($username, $password, $email) {
Expand Down Expand Up @@ -92,7 +95,7 @@ public function saveBills ($bill_id, $year, $title, $bill_name, $connection) {

public function getBills () {
$conn = $this->getConnection();
return $conn->query("SELECT bill_name, title, (votes_for + votes_against) AS total
return $conn->query("SELECT bill_name, bill_id, title, (votes_for + votes_against) AS total
FROM bills
ORDER BY total
LIMIT 5");
Expand Down
1 change: 0 additions & 1 deletion dash.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
<div class="loginbox">
<div class="dashitem">
<p>You are logged in as <?= $_SESSION["name"] ?>.</p>
<?php var_dump($_SESSION); ?>
</div>
<form id="logout" action="logout.php" method="post">
<input type="submit" value="Log out" />
Expand Down
23 changes: 19 additions & 4 deletions handler.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
<?php
// handler.php
// handle comment posts, saving to MySQL and redirecting back to the list
require_once("Dao.php");

if (!isset($_SESSION)) {
session_start();
}

require_once("dao.php");

if (isset($_SESSION["name"]) && isset($_POST["commentButton"])) {
$comment = $_POST["comment"];
$comment_type = $_POST["vote"];
$bill = $_POST["bill"];
$username = $_SESSION["name"];

$_SESSION["billfromdao"] = $bill;

try {
$dao = new Dao();
$dao->saveComment($comment);
}catch (Exception $e) {
$dao->saveComment($username, $comment, $bill, $comment_type);
}
catch (Exception $e) {
var_dump($e);
die;
}
}
}
else {
$dao = new Dao();
$dao -> redirect("index.php", "Please log in to comment.");
}

header("Location:index.php");
141 changes: 46 additions & 95 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
$thisPage = 'Home';
require_once "dao.php";
include 'api_functions.php';
$dao = new Dao();

include 'top.php'; ?>

<body>
Expand All @@ -11,118 +13,67 @@
<?php include 'dash.php'; ?>

<div class="billmain">
<div class="active">
<p>Most Active Bills</p>

<?php
$bill_list = $dao->getBills();
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
<script src="JS/ajax.js" type="text/javascript"></script>
<?php
$bill_list = $dao->getBills();

foreach ($bill_list as $bill) { ?>
foreach ($bill_list as $bill) {
$action_date = last_action($bill["bill_id"]) ?>
<div class="active">
<p>Active Bills</p>
<div class="billimage"><span><?php echo $bill["bill_name"]; ?></span></div>
<div class="lastaction">Passed xx/xx/xxxx</div>
<div class="lastaction">Last Action: <?php echo $action_date; ?> </div>
<div class="billsummary"><?php echo $bill["title"]; ?></div>
<?php } ?>
</div>
</div>

<!--
<div class="comments">

<div class="comments">
<div class="commentbox">
<form name="commentForm" action="handler.php" method="POST">
<textarea name="comment" rows="4" placeholder="Write comments or testimony here, select pro, neutral or anti, and press Go."></textarea>
<label>Yea or Nay?</label>
<textarea name="comment" rows="4" placeholder="Write comments or testimony here, select pro, neutral or anti, and press Submit."></textarea>
<label>Yea, Nay of Neutral?</label>
<label>
<input type="radio" name="vote" value="pro" /><img class="prolabel" src="images/thumbs_up.png" />
</label>
<label class="neutrallabel">
<input type="radio" name="vote" value="neutral" />?</label>
<label>
<input type="radio" name="vote" value="anti" /><img class="antilabel" src="images/thumbs_down.png" />
</label>
<input type="submit" name="commentButton" value="Comment" />
<input type="hidden" name="form" value="comment">
</form>
</div> -->

<!-- <div class="billmain">
<div class="active">
<p>Most Active</p>
<div class="billimage"><span>S 1081</span></div>
<div class="lastaction">Passed Senate, 2/20/2015</div>
<div class="billsummary">Summary: HEALTH CARE - Amends existing law to provide reserves and surplus requirements of public postsecondary educational institutions with a public postsecondary educational institution plan for health care benefits.</div>
<div class="comments">
<div class="commentbox">
<form name="commentForm" action="handler.php" method="POST">
<textarea name="comment" rows="4" placeholder="Write comments or testimony here, select pro, neutral or anti, and press Go."></textarea>
<label>Yea or Nay?</label>
<label>
<input type="radio" name="vote" value="pro" /><img class="prolabel" src="images/thumbs_up.png" />
</label>
<label class="neutrallabel">
<input type="radio" name="vote" value="neutral" />?</label>
<label>
<input type="radio" name="vote" value="anti" /><img class="antilabel" src="images/thumbs_down.png" />
</label>
<input type="submit" name="commentButton" value="Comment" />
<input type="hidden" name="form" value="comment">
<input type="radio" name="vote" value="neutral" checked="checked" />?</label>
<input type="submit" name="commentButton" value="Submit" />
<input type="hidden" name="form" value="comment" />
<input type="hidden" name="bill" value="<?php echo $bill["bill_id"]; ?>" />
</form>
</div> -->


<?php
/* $comments = $dao->getComments();
echo "<table>";
foreach ($comments as $comment) {
echo "<tr>";
echo "<td>" . $comment["comment"] . "</td>";
# echo "<td>" . $comment["created"] . "</td>";
echo "</tr>";
}
echo "</table>";
*/ ?>
</div>


<!-- <div class="active">
<p>Also Active</p>
<div class="billimage"><span>S 1082</span>
</div>
<div class="lastaction">Passed Senate, 2/20/2015</div>
<div class="billsummary">Summary: HEALTH CARE - Amends existing law to provide reserves and surplus requirements of public postsecondary educational institutions with a public postsecondary educational institution plan for health care benefits.</div>
<div class="comments">
<div class="commentbox">
<form>
<textarea name="comment" rows="4" placeholder="Write comments or testimony here, select pro, neutral or anti, and press Go."></textarea>
<label>Yea or Nay?</label>
<label>
<input type="radio" name="vote" value="pro" /><img class="prolabel" src="images/thumbs_up.png" />
</label>
<label class="neutrallabel">
<input type="radio" name="vote" value="neutral" />?</label>
<label>
<input type="radio" name="vote" value="anti" /><img class="antilabel" src="images/thumbs_down.png" />
</label>


<div class="pro"><h3>Yea</h3>
<?php $comments = $dao->getComments($bill["bill_id"], "pro");
foreach ($comments as $comment) {
?>
<span><?php echo $comment["username"]; ?> says:</span> <?php echo $comment["comment"];
echo "DATE:" . $comment["date"]; ?>
<?php } ?>
</div>

<div class="neutral"><h3>Neutral</h3>
<?php $comments = $dao->getComments($bill["bill_id"], "neutral");
foreach ($comments as $comment) { ?>
<span><?php echo $comment["username"]; ?> says:</span> <?php echo $comment["comment"] ?>
<?php } ?>
</div>

<input type="submit" value="Go" />
</form>
</div>
<div class="pro">
<span>Conrad says:</span> Best bill ever ... Lorem ipsum dolor sit amet, nobis suavitate iracundia ei his, ad nihil eirmod quo, viris temporibus qui eu. Et idque omnes instructior usu, qui ut posse everti lobortis, id his deserunt assentior.
Quo oratio senserit te, verterem constituto usu ut. Te pro aeque equidem maluisset, ponderum consetetur sea no. At volutpat torquatos adipiscing est, tempor temporibus in cum.
</div>
<div class="neutral">
<span>Sarah says:</span> Could go either way... Lorem ipsum dolor sit amet, nobis suavitate iracundia ei his, ad nihil eirmod quo, viris temporibus qui eu. Et idque omnes instructior usu, qui ut posse everti lobortis, id his deserunt assentior.
Quo oratio senserit te, verterem constituto usu ut. Te pro aeque equidem maluisset, ponderum consetetur sea no. At volutpat torquatos adipiscing est, tempor temporibus in cum.
</div>
<div class="anti">
<span>José says:</span> Impeach! Impeach! Lorem ipsum dolor sit amet, nobis suavitate iracundia ei his, ad nihil eirmod quo, viris temporibus qui eu. Et idque omnes instructior usu, qui ut posse everti lobortis, id his deserunt assentior.
Quo oratio senserit te, verterem constituto usu ut. Te pro aeque equidem maluisset, ponderum consetetur sea no. At volutpat torquatos adipiscing est, tempor temporibus in cum.
</div>
</div>
<div class="anti"><h3>Nay</h3>
<?php $comments = $dao->getComments($bill["bill_id"], "anti");
foreach ($comments as $comment) { ?>
<span><?php echo $comment["username"]; ?> says:</span> <?php echo $comment["comment"] ?>
<?php } ?>
</div>
</div>
</div>

<?php } ?>

</div> -->
</div>
</div>



Expand Down
6 changes: 3 additions & 3 deletions nhoffman.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ comment_id BIGINT(20) NOT NULL PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(64),
comment MEDIUMBLOB,
comment_link VARCHAR(200),
type VARCHAR(9),
comment_type VARCHAR(9),
votes_for TINYINT,
votes_against TINYINT,
flags TINYINT,
bill_id BIGINT(20),
bill_id VARCHAR(20),
comment_ip VARCHAR(100),
date DATETIME,
comment TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
comment_parent BIGINT(20),
approved BOOL
);
Expand Down
10 changes: 10 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,19 @@ div {
height: 100%;
padding: 5px 0px 5px 5px;
}
.pro, .neutral, .anti, h3 {
color: black;
text-transform: uppercase;
font-size: 1.2em;
line-height: 1.5em;
text-align: center;
}
.pro {
float: left;
width: 32%;
border-right: 1px dashed black;
padding-right: 5px;
min-height: 1px;
line-height: 1.2em;
}
.neutral {
Expand All @@ -315,12 +323,14 @@ div {
border-right: 1px dashed black;
padding-left: 5px;
padding-right: 5px;
min-height: 1px;
line-height: 1.2em;
}
.anti {
float: left;
width: 31%;
padding-left: 5px;
min-height: 1px;
line-height: 1.2em;
}
.commentbox input[type=radio] {
Expand Down

0 comments on commit df2db74

Please sign in to comment.