/
testdb.php
85 lines (77 loc) · 1.95 KB
/
testdb.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
<?php
function test() {
$server = 'localhost';
$database = 'cs313';
$username = 'admin';
$password = 'loga2872';
$dsn = 'mysql:host='.$server.';dbname='.$database;
$options = array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION);
$g1db = new PDO('mysql:host=localhost;dbname=cs313', $username, $password);
return $g1db;
}
/*
try {
$g1db = new PDO($dsn, $username, $password, $options);
return $g1db;
}
catch (PDOException $ex) {
$error_message = $ex->getMessage();
exit();
}
}
*/
$test = test();
function viewScriptures() {
global $test;
$query = 'SELECT Scriptures.chapter, Scriptures.verse, Scriptures.content, Books.name
FROM Scriptures
INNER JOIN Books
ON Scriptures.book_id = Books.book_id
ORDER BY Books.name';
$statement = $test->prepare($query);
$statement->execute();
$scriptures = $statement->fetchAll();
$statement->closeCursor();
return $scriptures;
}
function viewScripturesByBook($book_id = "-1") {
global $test;
if ($book_id == "-1") {
$scriptures = viewScriptures();
} else {
$query = 'SELECT Scriptures.chapter, Scriptures.verse, Scriptures.content, Books.name
FROM Scriptures
INNER JOIN Books
ON Scriptures.book_id = Books.book_id
WHERE Scriptures.book_id = :book_id
ORDER BY Books.name';
$statement = $test->prepare($query);
$statement->bindValue(":book_id", $book_id);
$statement->execute();
$scriptures = $statement->fetchAll();
$statement->closeCursor();
}
return $scriptures;
}
$book = filter_input(INPUT_POST, 'book');
if (!isset($book) || $book == "all"){
$scriptures = viewScriptures();
} else {
$scriptures = viewScripturesByBook($book);
}
?>
<article>
<h1>Scripture Resources</h1>
<ul>
<?php foreach ($scriptures as $scripture): ?>
<li>
<strong>
<?php echo $scripture['name']; ?>
<?php echo $scripture['chapter']; ?>:
<?php echo $scripture['verse']; ?>
</strong>
- <?php echo $scripture['content']; ?>
</li>
<?php endforeach; ?>
</ul>
</article>