forked from JarekSed/Manny-Blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.php
More file actions
88 lines (83 loc) · 2.89 KB
/
Copy pathcommon.php
File metadata and controls
88 lines (83 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
<script src="http://code.jquery.com/jquery.js"></script>
<script type = "text/javascript" src = "script.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<link type = "text/css" rel = "stylesheet" href = "stylesheet.css"/>
<?php
require_once("database.php");
session_start(); // creates session array(kinda like POST) that can save variables between pages
/*
returns username if user is logged in
returns false if not logged in
*/
function getUserName(){
if(isset($_SESSION['username'])){
return $_SESSION['username'];
}else{
return false;
}
}
/*
returns author_id from mysql Author table if user is logged in
returns false if not logged in
*/
function getID(){
global $db;
if(!isset($_SESSION['username'])){
return false;
?>
<a href = "index.php">Not logged in. Return to home page</a>
<?php
}
$username = $_SESSION['username'];
$command = "SELECT author_id FROM Authors WHERE username=:value";
$stmt = $db->prepare($command); //prepare creates object from string
$stmt->bindParam(':value', $username); // puts variable $ in placeholder : while checking for string escape bs
if (!$stmt->execute()) { //executes the command from the string. returns boolean
echo "Database is down. Screwed up the command";
exit;
}
$results = $stmt->fetchAll(); //returns all the results from the command
if(count($results)==1){ //checks how many results where returned. returns 1 or 0
return $results[0]['author_id'];
}else{
return false;
}
}
/*
prints all posts from user
*/
function getPosts(){
if(!isset($_SESSION['username'])){
return false;
?>
<a href = "index.php">Not logged in. Return to home page</a>
<?php
}
global $db;
$id = getID();
$command = "SELECT content,title,date from Posts Where author_id=:value"; //gets posts, title and date from user
$stmt = $db->prepare($command);
$stmt->bindParam(':value', $id);
if(!$stmt->execute()){
echo "Screwed up command";
}
$results = $stmt->fetchAll(); // puts output in array
/*prints all post title,content,date from results array starting with most recent;
divides all posts into collapsable divs
*/
for($i = count($results)-1; $i>=0; $i--){
?><div class = "posts"><?php
echo " <span id = 'author'>Author: " . getUserName() . "</span></br>";
echo " <span id = 'title'>Title: ". $results[$i]["title"] . "</span></br>";
echo " <span id = 'date'>Date: " . $results[$i]['date'] . "</span></br>";
?><span id = 'hidden'><?php
echo $results[$i]["content"]?>
</span>
</div>
</br>
<?php
}
}
?>