Version: 1.0
Type: Function
Category: HTML
License: GNU General Public License
Description: This script selects one random quote from a MySQL database. Allows the use of HTML tags, so it can be used to display banners, images, bg images, text, links, etc. Features a marker that prevents displayed quotes to repeat themselves until all quotes have been displayed.
<?PHP
/*
* tips.php3
* This is a not-so-small script to display random quotes.
* The quotes are stored in a MySQL table named "quote" with 3 fields: q_id, q_quote and q_mark.
* The program will select ONE RANDOM quote from the database and mark it as read (q_mark = 1),
* this will prevent a quote from repeting itself after consecutive page loads.
* The script is conceived so special characters and HTML tags can be used inside the quotes.
* Configure it by changing host, user, password and database in lines 15 and 16 to match your settings.
* If you like it or modify it in any way so that it is more efficient, less lines or add any other feature,
* please let me know: Jose Luis jlpellicer@bigfoot.com
* Have fun!
*/
// initialize variable that will hold the quote.
$quote = "";
// establish connection to the database containing the quotes.
$db = mysql_connect("host", "user", "password") or die ("Unable to connect to database.");
mysql_select_db("database") or die ("Unable to select database.");
// select the quotes that have not been displayed (q_mark = 0).
$sql = "SELECT * from quote WHERE q_mark = 0";
$result = mysql_query($sql);
// simple error checking
if (mysql_errno()>0) {
echo "<BR>\n<FONT COLOR=\"#990000\">".mysql_errno().": ".mysql_error()."<BR>\n";
exit;
}
// put the number of rows found into $max.
$max = mysql_num_rows($result)-1;
// if we do not find an available quote, then mark them (q_mark) to 0 and select again.
if ($max < 0) {
$result = mysql_query("UPDATE quote SET q_mark = 0");
if (mysql_errno()>0) {
echo "<BR>\n<FONT COLOR=\"#990000\">".mysql_errno().": ".mysql_error()."<BR>\n";
exit;
}
$sql = "SELECT * from quote WHERE q_mark = 0";
$result = mysql_query($sql);
if (mysql_errno()>0) {
echo "<BR>\n<FONT COLOR=\"#990000\">".mysql_errno().": ".mysql_error()."<BR>\n";
exit;
}
$max = mysql_num_rows($result)-1;
}
// generate a random number between 0 and the number of quotes available.
mt_srand((double)microtime()*1000000);
if ($max > 0) {
$random = mt_rand(0,$max);
} else {
$random = 0;
}
// select the random quote and store the text in $quote and it's id in $id.
for ($x=0;$x<=$random;$x++) {
$myrow = mysql_fetch_array($result);
}
$id = $myrow[0];
$quote = $myrow[1];
// mark this selected quote as displayed (q_mark = 1).
$result = mysql_query("UPDATE quote SET q_mark = 1 WHERE q_id = '$id'");
if (mysql_errno()>0) {
echo "<BR>\n<FONT COLOR=\"#990000\">".mysql_errno().": ".mysql_error()."<BR>\n";
exit;
}
// convert to HTML special characters, you know, like, and so on.
$quote = nl2br(htmlentities($quote));
// finally replace the "<" and ">" for < and > so you that can use tags.
$quote = ereg_replace ("<", "<", $quote);
$quote = ereg_replace (">", ">", $quote);
echo $quote."<BR>\n";
?>