/
post_process.php
121 lines (102 loc) · 3.37 KB
/
post_process.php
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
// include('header.php');
session_start();
require('connection.php');
// get info from users post
if(isset($_POST['action']) && $_POST['action']=='post_comment'){
insert_query_comment($_POST);
}
function insert_query_post($post){
$content = escape_this_string($post['content']);
$query = "INSERT INTO posts
(content, created_at, updated_at,user_id)
values
('{$content}',now(),now(),{$_SESSION['user_id']});";
if(run_mysql_query($query)){ // run query here
header('Location: wall.php');
return true;
} else{
var_dump($post);
die("System Error");
}//return boolean false if errors
}
function insert_query_comment($post){
$content = escape_this_string($post['content']);
$query = "INSERT INTO comments
(content, created_at, updated_at,post_id,user_id)
values
('{$content}',now(),now(),{$_POST['post_id']},{$_SESSION['user_id']});";
if(run_mysql_query($query)){ // run query here
header('Location: wall.php');
return true;
} else{
var_dump($post);
die("System Error");
}//return
}
if(isset($_POST['action']) && $_POST['action']=="write-post"){
insert_query_post($_POST);
}
function pull_all_posts(){
$query= 'SELECT concat(users.name_first," ",users.name_last) as user_name,
users.id as user_id, posts.id as post_id,
posts.content as post_content,
posts.created_at as posted_at
FROM posts
JOIN users ON users.id = posts.user_id
ORDER BY posted_at DESC;'
;
// var_dump($query);
// die();
if(!fetch($query)){
$_SESSION['errors']['posts'] = "post query was wrong";
header("Location: wall.php");
die();
}
return fetch($query); // returns array
}
function pull_all_comments($post_id){
$query= "SELECT concat(users.name_first,' ',users.name_last) as user_name,
comments.id as comment_id,
comments.content as comment_content,
comments.user_id as user_who_commented,
comments.created_at as comment_date
FROM comments
JOIN users ON users.id = comments.user_id
-- JOIN posts ON posts.id = comments.user_id
WHERE comments.post_id = {$post_id}
ORDER BY comment_date DESC;"
;
// var_dump($query);
// die();
return fetch($query); // returns array
}
function post_to_wall(){
$posts = pull_all_posts();
foreach ($posts as $post) { ?>
<div class="well well-sm col-sm-8 col-sm-offset-2 blog text-center">
<h3><?php echo ucfirst($post['user_name'])?></h3>
<p class="initialism" ><?php echo date('g:i a F j Y ', strtotime($post['posted_at']));?></p>
<p><?php echo $post['post_content']; ?></p>
<?php $comments = pull_all_comments($post['post_id']); ?>
<?php foreach ($comments as $comment) { ?>
<div>
<h4><?= ucfirst($comment['user_name']); ?></h4>
<p class="initialism" ><?php echo date('g:i a F j Y ', strtotime($comment['comment_date']));?></p>
<p><?php echo $comment['comment_content']; ?></p>
</div>
<?php } ?>
<form method="post" action="post_process.php">
<div class="form-group">
<input type="hidden" name="action" value="post_comment">
<input type="hidden" name="post_id" value="<?= $post['post_id'] ?>">
<textarea class="form-control" name="content" placeholder="Whats your comment"></textarea>
</div>
<div class="form-group">
<input class="btn btn-success btn-sm" type="submit" value="Comment">
</div>
</form>
</div>
<?php }
}
?>