-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_functions.php
95 lines (75 loc) · 1.92 KB
/
common_functions.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
<?php
// opens db connection
function OpenDBconnection()
{
// For my local DB test
// $servername = "127.0.0.1:8889";
// $username = "root";
// $password = "root";
// $database = "bestfriendever";
// For DB on heroku.com
$url = parse_url(getenv("CLEARDB_DATABASE_URL"));
$servername = $url["host"];
$username = $url["user"];
$password = $url["pass"];
$database = substr($url["path"], 1);
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//else echo "Connected successfully";
return $conn;
}
// closes db connection
function CloseDBconnection($conn)
{
$conn->close();
}
// determines path to picture for carousel and articles
// if null -> placeholder image
function imageSource($picture)
{
return $picture ? "img/" . $picture : "img/placeholder.jpg";
}
// redirects to destination page
function RedirectTo($dest)
{
// Redirect back to blog article page
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
header("Location: http://$host$uri/$dest");
}
// cleans input
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// displays word in plural if count > 1 (ex. 2 comments)
function pluralize($count, $singular, $plural = false)
{
if (!$plural) $plural = $singular . 's';
return ($count == 1 ? $singular : $plural) ;
}
// email validation
function isValidEmail($email)
{
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}
// last id from database table
function lasdID()
{
$stmt = $this->conn->lastInsertId();
return $stmt;
}
// get article date
function article_date($date)
{
$date = new DateTime($date);
return $date->format('F d, Y');
}
?>