function getUserMaxStories($db_link, $default) { $perfsQuery = sprintf("SELECt max_stories FROM user_perfs WHERe user= '%s'", mysql_real_escape_string($user));
$result = mysql_query($perfsQuery, $db_link);
$max_stories = $default;
if ($row = mysql_fetch_assoc($result)) { $max_stories = $row['max_stories']; }
return $max_stories; }
function writeRssFeed($user) { // Get the DB connection information $settings = parse_ini_file("rss_server.ini");
// look up the user's preferences... $link = mysql_connect($settings['db_host'], $settings['user'], $settings['password']) OR die(mysql_error());
$max_stories = getUserMaxStories($link, 25);
// go get my data $newsQuery = sprintf("SELECt * FROM stories WHERe post_date = '%s'", mysql_real_escape_string(time()));
class ResultMessage { private $severity; private $message;
public function __construct($sev, $msg) { $this->severity = $sev; $this->message = $msg; }
public function getSeverity() { return $this->severity; }
public function setSeverity($severity) { $this->severity = $severity; }
public function getMessage() { return $this->message; }
public function setMessage($msg) { $this->message = $msg; } }
function cntMsgs($messages) { $n = 0;
foreach($messages as $m) { if ($m->getSeverity() == 'Error') { $n++; // add one to the result; } } return $n; }
$messages = array(new ResultMessage("Error", "This is an error!"), new ResultMessage("Warning", "This is a warning!"), new ResultMessage("Error", "This is another error!"));
$errs = cntMsgs($messages);
echo("There are " . $errs . " errors in the result.n");
class ResultMessage { private $severity; private $message;
public function __construct($sev, $msg) { $this->severity = $sev; $this->message = $msg; }
public function getSeverity() { return $this->severity; }
public function setSeverity($severity) { $this->severity = $severity; }
public function getMessage() { return $this->message; }
public function setMessage($msg) { $this->message = $msg; } }
function countErrors($messages) { $matchingCount = 0; foreach($messages as $m) { if ($m->getSeverity() == "Error") { $matchingCount++; } } return $matchingCount; }
$messages = array(new ResultMessage("Error", "This is an error!"), new ResultMessage("Warning", "This is a warning!"), new ResultMessage("Error", "This is another error!"));
$errs = countErrors($messages);
echo("There are " . $errs . " errors in the result.n");
// Get the actual name of the function convertDayOfWeekToName($day) { $dayNames = array( "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); return $dayNames[$day]; }
echo("The name of the 0 day is: " . convertDayOfWeekToName(0) . "n"); echo("The name of the 10 day is: " . convertDayOfWeekToName(10) . "n"); echo("The name of the 'orange' day is: " . convertDayOfWeekToName('orange') . "n");
class InvalidDayOfWeekException extends Exception { }
class InvalidDayFormatException extends Exception { }
function convertDayOfWeekToName($day) { if (! is_numeric($day)) { throw new InvalidDayFormatException('The value '' . $day . '' is an ' . 'invalid format for a day of week.'); }
if (($day > 6) || ($day < 0)) { throw new InvalidDayOfWeekException('The day number '' . $day . '' is an ' . 'invalid day of the week. Expecting 0-6.'); }
echo("The name of the 0 day is: " . convertDayOfWeekToName(0) . "n");
try { echo("The name of the 10 day is: " . convertDayOfWeekToName(10) . "n"); } catch (InvalidDayOfWeekException $e) { echo ("Encountered error while trying to convert value: " . $e->getMessage() . "n"); }
try { echo("The name of the 'orange' day is: " . convertDayOfWeekToName('orange') . "n"); } catch (InvalidDayFormatException $e) { echo ("Encountered error while trying to convert value: " . $e->getMessage() . "n"); }
function countErrors($messages) { $matchingCount = 0; foreach($messages as $m) { if ($m->getSeverity() == "Error") { $matchingCount++; } } return $matchingCount; }
function countWarnings($messages) { $matchingCount = 0; foreach($messages as $m) { if ($m->getSeverity() == "Warning") { $matchingCount++; } } return $matchingCount; }
function countInformation($messages) { $matchingCount = 0; foreach($messages as $m) { if ($m->getSeverity() == "Information") { $matchingCount++; } } return $matchingCount; }
$messages = array(new ResultMessage("Error", "This is an error!"), new ResultMessage("Warning", "This is a warning!"), new ResultMessage("Error", "This is another error!"));
$errs = countErrors($messages);
echo("There are " . $errs . " errors in the result.n"); ?>
function countMessages($messages, $withSeverity) { $matchingCount = 0; foreach($messages as $m) { if ($m->getSeverity() == $withSeverity) { $matchingCount++; } } return $matchingCount; }
function countErrors($messages) { return countMessages($messages, "Errors"); }
function countWarnings($messages) { return countMessages($messages, "Warning"); }
function countInformation($messages) { return countMessages($messages, "Information"); }
$messages = array(new ResultMessage("Error", "This is an error!"), new ResultMessage("Warning", "This is a warning!"), new ResultMessage("Error", "This is another error!"));
$errs = countErrors($messages);
echo("There are " . $errs . " errors in the result.n");