|
|
|
| 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 : |
12191 |
| 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";
?>
|
|
| a function that builds an HTML select list from any mysql table. Categories : PHP, MySQL, HTML and PHP | | | Required form fields that pull from MySQL database Categories : PHP, HTML and PHP, Databases, MySQL | | | Database resultset navigation Categories : PHP, HTML and PHP, Databases, MySQL, Navigation | | | 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 | | | Storing / Retrieving pictures from database This can be used for banner / ads exchange. very useful for people developing big portal with ads in the site........ Categories : PHP, MySQL, Databases, HTML and PHP | | | mySQL/PHP/search with multientry
form and table output with colored rows Categories : PHP, Beginner Guides, MySQL, HTML and PHP, Databases | | | Creating thumbnails from MySQL Blobs online Categories : PHP, MySQL, Graphics, HTML and PHP, Databases | | | html split bar used to split in multiple pages a database result Categories : HTML and PHP, Databases, MySQL, PHP | | | Function to do live population of HTML's <Select> tag from a Table Categories : PHP, MySQL, HTML and PHP, Databases | | | Alternating background color for HTML table rows Categories : PHP, Databases, MySQL, HTML and PHP | | | 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 | | | Record Set Paging with PHP (RSP) Categories : PHP, MySQL, Navigation, Databases, HTML and PHP | | | A script to generate a report from a valid mysql connection. The user has to supply which fields he wants to display in table. All properties are changable.
Categories : PHP, PHP Classes, Databases, MySQL, HTML and PHP | | | DDN FFA Network Script Categories : PHP, MySQL, Complete Programs, HTML and PHP, Databases | | | How to Create a Shoutbox Using PHP & MySQL Categories : PHP, MySQL, Web Applications, Beginner Guides, HTML and PHP | |
| |
| |
|