/
funCollection.php
142 lines (113 loc) · 2.43 KB
/
funCollection.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* Instantiates a PDO object to the db and returns a handle
*/
function dbHandler() {
global $dbh;
/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:dbname=CS143';
$user = 'cs143';
$password = '';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
die();
}
return $dbh;
}
/**
* Return error page
*/
function print_error() {
header( 'Error found !' );
?>
<!DOCTYPE html>
<html>
<head>
<title>Error found !</title>
</head>
<body>
<h1>Error found !</h1>
</body>
</html>
<?php
die;
}
function render_table($objects, $link_url, $id_col, $link_col, $table_header = '', $show_headers = true, $skip_col_names = array()) {
if ( empty( $objects ) ) {
return '';
}
$html = '<table border="1">';
$display_col_names = array_diff_key( $objects[0], $skip_col_names );
unset( $display_col_names[$id_col] );
$display_col_names = array_keys( $display_col_names );
if ( !empty( $table_header) ) {
$html .= '<tr><th colspan="' . sizeof( $display_col_names ) . '">' . $table_header . '</th></tr>';
}
if ( $show_headers ) {
$html .= '<tr>';
foreach ( $display_col_names as $col ) {
$html .= "<td><strong>$col</strong></td>";
}
$html .= '</tr>';
}
foreach ( $objects as $o ) {
$html .= '<tr>';
$id = $o[$id_col];
foreach ( $display_col_names as $col ) {
$html .= '<td>';
if ( $id && $link_url && $col === $link_col ) {
$html .= hyperlink( $link_url, $id, $o[$col] );
} else {
$html .= $o[$col];
}
$html .= '</td>';
}
$html .= '</tr>';
}
$html .= '</table>';
return $html;
}
/**
* Appends "?id=" and $id to $url
*/
function url_for_id( $url, $id ) {
return "$url?id=$id";
}
/**
* Returns and HTML anchor tag for the given inputs
* @param $url - url to link to
* @param $id - parameter to submit as an 'id' GET parameter
* @param $target
* @param $text
*/
function hyperlink( $url, $id, $text, $target = NULL ) {
$a = '<a href="' . url_for_id( $url, $id ) . '"';
if ( $target )
$a .= ' target="_blank"';
$a .= ">$text</a>";
return $a;
}
/**
* Redirects to $url and stops execution
*/
function redirect_to( $url ) {
header( "Location: $url" );
die;
}
/**
* Generate the HTML header for each page.
* @param $title - the page's title
*/
function page_header( $title ) {
?>
<?php
}
/**
* Generate the HTML footer for each page
*/
function page_footer() {
?>
<?php
}