Link to Notes with Demo Code

advertisement
Data Interoperability | Class Notes #2
1
2
3
4
5
6
7
8
9
10
11
12
CLASS NOTES:
G. Benoit, benoit@simmons.edu
For this class, given people’s self-review (on a 0-10 scale) of skills and background taken on the first day, I’ve created a series of how-to guides for performing certain
important actions with computer scripts and programs on various data stores.
The demos are predicated on the student having tackled the required “Prefatory readings” section on the syllabus (http://web.simmons.edu/~benoit/lis531z/s16/
review.html). Continue to review JavaScript, PHP, MySQL, and Java - working through the tutorials on your own.
For these class notes, we introduce the following:
• integration of php commands (in yellow)
• php and mysqli integration (including particularly the use of the config.ini file; in pink)
• introduction of the object-oriented approach (the $useri->connect…)
• arrays (and their use in extracting row-> from the $result object variable
• integration with HTML elements & attributes, demonstrating the repeated use of Strings and escape sequences (the \”) in light green
• the chatheader.txt and chatfooter.txt are text files holding the usual header and footer materials in a website.
13
14
15
16
Contents:
1. config.ini - the small text file that holds username, password, and database names that are read and
stored in an array $config[]
17
2. chathead.txt and chatfooter.txt - text files that are actually the beginning and ends of an html page
18
3. chatFetchQuestions.php - the script that is called and uses the .txt files
19
20
a. Links to chathome.html, a web form for asking questions
i. -> This page calls saveAnswers.php - the script that sanitizes the input, connects to the database,
inserts and checks the input.
21
22
23
_____________________________________________________________________________________________________________________________________________________
24
config.ini
25
26
27
28
29
30
31
32
33
34
35
36
consists of a text file with each of the following lines (starting with [database] … make your file and post in your webspace. Replace
‘benoit’ with your user name; replace the XXXXX in password with your student ID, and replace “mydb” with the name of your
database, probably something like lis531zs16-jones. Save these data as config.ini
[database]
username = benoit
password = xxxxx
dbname = mydb
_____________________________________________________________________________________________________________________________________________________
chatheader.txt and chatfooter.txt
are the html tags. Check them out here as links.
File name: /Users/gbenoit/Documents/lis531z-DataInterop/s16/classNotes2.rtf | Page 1 of 5
Data Interoperability | Class Notes #2
37
38
39
_____________________________________________________________________________________________________________________________________________________
40
chatFetchQuestions.php -
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
I like to include the mysql database table just so I know what I’m working with, tho of course the same belong in
the Data Dictionary. Notice the different data types: the recno is in integer that is created automatically by the database; next we have a
varchar() [for storing String data], TEXT [for input from users that’s > 256 characters); and usefully a TIMESTAMP that is created also
by the database table.
<?php
// chatFetchQuestions.php
// called from chatcatch.php
// written by G Benoit, Jan 26, 2016, 9 am
/*
USE gbdemo;
CREATE TABLE questionTable (
recno INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
questionTitle varchar(256),
question TEXT,
postedBy varchar(30),
postedOn TIMESTAMP
);
*/
$questionTableName
$fileName = "q";
= "questionTable";
echo file_get_contents("chatheader.txt");
echo "<h1>Our Q&A Site</h1>";
echo "<a href='http://web.simmons.edu/~benoit/lis531z/chat/chathome.html' target='new'>Add a new question.</a>";
// CONFIG FILE
$config = parse_ini_file('config.ini');
$useri = new mysqli('dany.simmons.edu',$config['username'],$config['password'],$config['dbname']);
// CHECK CONNECTION
if ($useri->connect_error) {
die("Connection failed, sorry. ".$useri->connect_error);
}
File name: /Users/gbenoit/Documents/lis531z-DataInterop/s16/classNotes2.rtf | Page 2 of 5
Data Interoperability | Class Notes #2
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
$sql = "SELECT * FROM questionTable ORDER BY postedOn";
// FOR DEBUGGING: // echo $sql;
echo '<h3>Questions and Answers... add yours.</h3>';
// decorate the output...
if ($result = $useri->query($sql)) {
echo '<table width="100%" border="0">';
while ($row = $result->fetch_object()) { //only this is changed
echo "<tr><td colspan='2'><hr /></td></tr>";
echo '<tr><td>ID: '.$row->recno.'</td><td> Posted by <strong>'.$row->postedBy.'</strong> '.
$row->postedOn .
' <strong>'.$row->questionTitle . '</strong></td></tr>';
echo '<tr><td colspan="2"><font size="+2">'.$row->question;
echo '</font></td></tr>';
echo '<tr><td colspan="2">';
echo "<a onclick=\"document.getElementById('div_name".$row->recno."').style.display='';
return false;\"
href=\"\" style=\"text-decoration:none;border-bottom:1px dotted blue;\">
Add Your Answer ...</a><br />
Others’ answers (if any)<blockquote><p>";
$fileName = "q".$row->recno.".txt";
if (file_exists($fileName)) {
echo file_get_contents($fileName);
} else {
echo 'No answers yet.';
}
echo "<br />
<div id=\"div_name".$row->recno."\" style=\"display:none;margin:15px 15px 0px 15px;
padding:5px;border:1px solid #aaa;\">
To cancel, just click the
<a onclick=\"document.getElementById('div_name".$row->recno."').style.display='none';
File name: /Users/gbenoit/Documents/lis531z-DataInterop/s16/classNotes2.rtf | Page 3 of 5
Data Interoperability | Class Notes #2
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
141
142
143
144
145
146
147
148
149
150
151
152
153
return false;\"
href=\"\"
style=\"text-decoration:none;border-bottom:1px dotted blue;\">Close</a>
<form method=\"post\" action=\"saveAnswers.php\">
<input type=\"text\" size=\"30\" maxlength=\"30\" name=\"postedBy\"
placeholder=\"Posted by ... 30 max\"><br />
<!-- <input type=\"text\" size=\"50\" maxlength=\"50\" name=\"title\" placeholder=\"Topic Title ... 256
chars\"><br /> -->
What is your answer for ".$row->recno."<br />
<textarea rows=\"25\" cols=\"80\" name=\"answer\" id=\"answer\"></textarea>
<input type=\"hidden\" value=\"".$row->recno."\" name=\"qNo\" />
<br />
<input type=\"submit\" value=\"Submit your answer\">
</form>
To cancel, just click the
<a onclick=\"document.getElementById('div_name".$row->recno."').style.display='none';
return false;\"
href=\"\"
style=\"text-decoration:none;border-bottom:1px dotted blue;\">Close</a>
</p>
</blockquote>
</div>";
echo '</td></tr>';
}
echo '</table>';
$result->close();
} else { //check for error if query was wrong
echo $useri->error;
}
echo "</div>";
echo ' Closing the connection.';
$useri->close();
echo '<a href="http://web.simmons.edu/~benoit/lis531z/index.html">531z Homepage</a>';
echo '</div>';
echo file_get_contents("chatfooter.txt");
?>
File name: /Users/gbenoit/Documents/lis531z-DataInterop/s16/classNotes2.rtf | Page 4 of 5
Data Interoperability | Class Notes #2
154
_____________________________________________________________________________________________________________________________________________________
155
saveAnswers.php -
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
This script saves the user’s input. How much can you read? Notice the use of the date_default_timezone_set(). This is
required. The variable $today first gets a date() object and then formats it … try it in your own script that sets the date and then echoes $today
back and see. Read line 183: notice the fileName, the msg, and the use of FILE_APPEND. If we can save okay, the value is > 1 (byte).
<?php
date_default_timezone_set("America/New_York");
$today = date("D M j G:i:s T Y");
/* save answers - these data are captured from the chathome.html webform. Since we’re sending the data by POST,
we must use the $_POST() command to extract the data from the form and store them in variables in the php
script.
*/
$qNo
= $_POST["qNo"];
$answer = $_POST["answer"];
$postedBy = $_POST["postedBy"];
$fileName = "q".$qNo.".txt";
/* Notice we concatenate Strings and vars to create “q1.txt” - a String.
*/
echo file_get_contents("chatheader.txt");
echo '<h2>Confirmation</h2>';
echo "qNo. $qNo posted by $postedBy on $today";
echo "<br />$answer will saved in $fileName";
if (is_null($answer)) {
/* notice this if statement doesn’t respond to nulls but we could. */
// do nothing
} else {
$msg = "<p><hr />$postedBy on $today answers $answer </p>";
if ( (file_put_contents($fileName, $msg, FILE_APPEND)) > 1) {
echo "Data saved to $fileName";
} else {
echo "Data NOT saved to $fileName";
}
}
echo "<blockquote><ol><li><a href='http://web.simmons.edu/~benoit/lis531z/chat/chathome.html'>Add a new
question.</a></li>";
echo "<li><a href='http://web.simmons.edu/~benoit/lis531z/chat/chatFetchQuestions.php'>Back to Q&A Site</
a></li></ul>";
echo "<font size='-2'>©2015 GB for our class.</font></blockquote>";
echo file_get_contents("chatfooter.txt");
?>
File name: /Users/gbenoit/Documents/lis531z-DataInterop/s16/classNotes2.rtf | Page 5 of 5
Download