'[select one]','rant'=>'rant','praise'=>'praise','question'=>'question','other'=>'other');
// initilize default form values
$defaults = array('comment_type'=>'','text'=>'','posted_by'=>'');
// connect to the database
function db_connect() {
return @mysqli_connect('localhost','php','phpis2kool','php_university') or false;
}
function display_comments($link) {
//date_format(posted_on,'%a %d-%b-%Y %l:%i %p')
if (! $result=mysqli_query($link,
"SELECT
posted_on, date_format(posted_on,'%a %d-%b-%Y %l:%i %p') 'date',
posted_by, text, comment_type
FROM guestbook_comment
ORDER BY posted_on DESC limit 10")
) {
//echo mysql_error();
return false;
}
while ($comment = mysqli_fetch_assoc($result)) {
if ($comment['comment_type'] == 'other') {$comment['comment_type'] = 'comment';}
echo "
",
htmlentities($comment['text']),
" ",
htmlentities("$comment[comment_type]) by $comment[posted_by], $comment[date] ]"),
"
";
}
return true;
}
function save_comment($data,$link) {
foreach($data as $key=>$value) {
$data[$key]=mysqli_escape_string($link,$value) ;
}
if (! mysqli_query($link,
"INSERT INTO guestbook_comment (posted_by,text,comment_type)
VALUES ('$data[posted_by]','$data[text]','$data[comment_type]')")
) {
//echo mysqli_error($link) . sprintf(" at line %d of %s",__LINE__,basename(__FILE__));
return false;
}
return mysqli_affected_rows($link);
}
function validate_form(&$data) {
$errors = array();
// trim everything
foreach ($data as $name => $value) { $data[$name] = trim($value); }
if (empty($data['text'])) {
$errors[] = 'The comments field is required';
}
if (strlen($data['text']) > 255 ) {
$errors[] = 'The comments field exceeds the maximum length';
}
$data['text'] = strip_tags($data['text']);
$data['posted_by'] = strip_tags($data['posted_by']);
if (! strlen($data['posted_by'])) { $data['posted_by'] = 'anonymous'; }
if (strlen($data['posted_by']) > 30) {
// truncate it
$data['posted_by'] = substr($data['posted_by'],0,30);
$errors[] = 'Your name exceeds the max field length' ;
}
if (empty($data['comment_type'])) {
$errors[] = 'Subject field is required';
}
if (! array_key_exists($data['comment_type'],$GLOBALS['subject_options'])) {
$errors[] = 'Invalid subject field';
}
return $errors;
}
/*
this function courtesy of: Learning PHP 5 by David Sklar
ISBN: 0-596-00560-1 (O'Reilly)
http://www.oreilly.com/catalog/learnphp5/index.html
*/
function input_select($element_name, $selected, $options, $multiple = false) {
// print out the