Viewing file: friendly.class.php (2.21 KB) -rw-rw-rw- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
// load error handling sequence
require_once ('error_handler.php');
// load configuration
require_once ('config.php');
// class stores Friendly web application functionality
class Friendly
{
// stores the database connection
private $mMysqli;
// constructor opens database connection
function __construct()
{
$this->mMysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD,
DB_DATABASE);
}
// generate news message
public function getNews()
{
// this will store the news line
$news = 'No news for today.';
// SQL that selects two random users from the database.
$query = 'SELECT user_name FROM users ' .
'ORDER BY RAND() ' .
'LIMIT 2';
// execute the query
$result = $this->mMysqli->query($query);
// retrieve the user rows
$row1 = $result->fetch_array(MYSQLI_ASSOC);
$row2 = $result->fetch_array(MYSQLI_ASSOC);
// close the input stream
$result->close();
// generate the news
if (!$row1 || !$row2)
{
$news = 'The project needs more users!';
}
else
{
// create HTML-formatted news message
$name1 = '<b>' . $row1['user_name'] . '</b>';
$name2 = '<b>' . $row2['user_name'] . '</b>';
$randNum = $this->getRandomNumber();
$news = 'User ' . $name1 . ' works with user ' . $name2 .
' at project #' . $randNum . '.';
}
// output the news line
return $news;
}
// returns a random number between 1 and 100
private function getRandomNumber()
{
// delays execution for quarter of a second
usleep(250000);
// holds the remote server address and parameters
$serverAddress = 'http://www.random.org/cgi-bin/randnum';
$serverParams = 'num=1&min=1&max=100';
// retrieve the random number from remote server
$randomNumber = file_get_contents($serverAddress . '?' .
$serverParams);
// output the random number
return trim($randomNumber);
}
// destructor closes database connection
function __destruct()
{
$this->mMysqli->close();
}
}
?>
|