|
|
|
| Title : |
Random quote generator using MySQL and PHP. Allows HTML tags inside quotes and
features a marker so a sigle quote will not be repeated after consecutive page
loads.
|
| Categories : |
PHP, HTML and PHP, MySQL |
 Jose Pellicer |
| Date : |
Jul 12th 2000 |
| Grade : |
3 of 5 (graded 2 times) |
| Viewed : |
5410 |
| File : |
No file for this code example. |
| Images : |
No Images for this code example. |
|
| Search : |
More code by Jose Pellicer |
|
| Action : |
Grade This Code Example
|
|
| Tools : |
My Examples List |
|
|
|
|
|
|
<?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";
?>
|
|
| Message of the Day - Random Message (Needs MySQL!) Categories : Databases, HTML and PHP, PHP, MySQL | | | phpCards - PHP/mySQL postcard script with web based admin to add, edit, and delete cards and categories. Very easy to install. Categories : PHP, Complete Programs, HTML and PHP, MySQL | | | A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL | | | Automatically printing the contents of an sql table in MySQL. Categories : MySQL, PHP, HTML and PHP, Databases | | | Amazon book cover handling Categories : HTML and PHP, PHP, MySQL, Ecommerce | | | Complete, simple working example of login screen and check on a unique page using php functions, cookies and mysql database. Categories : PHP, Cookies, MySQL, HTML and PHP, Authentication | | | Pull Down Surfing - Surf on Change Categories : Java Script, MySQL, HTML and PHP, PHP, Databases | | | Dynamically generated pop-ups (Select items) Categories : PHP, HTML and PHP, MySQL, Databases | | | a function that builds an HTML select list from any mysql table. Categories : PHP, MySQL, HTML and PHP | | | Alternating background color for HTML table rows Categories : PHP, Databases, MySQL, HTML and PHP | | | Functions for loading images into a MySQL database and displaying them. Categories : Graphics, HTML and PHP, MySQL, PHP, Databases | | | Creating thumbnails from MySQL Blobs online Categories : PHP, MySQL, Graphics, HTML and PHP, Databases | | | Editing the virtusertable and sendmail.cw via PHP3.0 and Mysql Categories : MySQL, HTML and PHP, PHP, Databases | | | This function will populate the options in a drop down HTML select list
in a form from a database query.
Categories : MySQL, General SQL, PHP, HTML and PHP, Databases | | | How can i Preload a 'SELECT MULTIPLE'? Categories : HTML and PHP, PHP, MySQL, Databases | |
|
|
|