Full-service Internet Marketing & Web Development
Recent Posts

Sponsors
![]() |
Random Quote GeneratorCode Wizard, 12-30-2006 |
{Random Quote Generator} - Licence: FREEWARE (Open Sorce) - Requires: PHP4, mySQL
----
Feel free to modify or change the content, correct errors or do what you like with it.
If you really want you could print it out and it it, I don't care ;)
Note: You have to make 2 files: quote.php & add_quote.php
if you copy it to one file, the variables will conflict!
Remember to edit your DB variables...
mySQL table:
#
# Create this table in mySQL
#
CREATE TABLE Quotes (
QID int(6) NOT NULL auto_increment,
Quote text,
Quote_By varchar(25),
Date_Added datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
PRIMARY KEY (QID),
UNIQUE id (QID)
);
file: quote.php
<?php
// Assigning the Database variables
$DB_host = "127.0.0.1";
$DB_username = "root";
$DB_password = "password";
$DB_name = "myDataBase";
// Connecting the DB
$Connection = mysql_connect($DB_host, $DB_username, $DB_password) OR die("Cannot Connect: mysql_error()");
mysql_select_db($DB_name, $Connection) OR die("DB SELECTION FAILED: mysql_error()");
// Searching the database for quotes and count them
$Query_1 = "SELECT * FROM Qutoes";
$Result_1 = mysql_query($Query_1);
$Count_1 = mysql_num_rows($Result_1);
$Count_2 = $Count_1 - 1;
// Generate A random ID number
$Random_ID = mt_rand('0', $Count_2);
// Find the row that has that spesific ID number
$Query_2 = "Select Quote, Quote_By FROM Quotes WHERE QID = $Random_ID LIMIT 1";
$Result_2 = mysql_query($Query_2);
$Row = mysql_fetch_row($Result_2);
// Display the quote
echo "<I>"$Row[0]"</I><BR> - $Row[1]";
// Close DB connection
mysql_close() OR die("CLOSE FAILED: mysql_error()");
?>
file: add_quote.php
<?php
// Assigning the Database variables
$DB_host = "127.0.0.1";
$DB_username = "root";
$DB_password = "password";
$DB_name = "myDataBase";
// Connecting the DB
$Connection = mysql_connect($DB_host, $DB_username, $DB_password) OR die("Cannot Connect: mysql_error()");
mysql_select_db($DB_name, $Connection) OR die("DB SELECTION FAILED: mysql_error");
// Showing the Quotes in the DB
$Query_1 = "Select QID, Quote, Quote_By, Date_Added FROM Quotes";
$Result_1 = mysql_query($Query_1);
// Looping out the results
while (list($ID, $Quote, $By, $Added) = mysql_fetch_row($Result_1))
{
echo "$Quote - $By - $Added <A HREF="$PHP_SELF?edit=$ID">[EDIT]</A> - <A HREF="$PHP_SELF?delete=$ID">[DELETE]</A>";
}
// The time/date
$date_added = date(Y-m-d H:i:s);
// Showing the Forms
// Edit Form
if ($edit)
{
$Edit_Query = "Select Quote, Quote_By FROM Quotes WHERE QID=$edit";
$Edit_Result = mysql_query($Edit_Query);
$Edit_Row = mysql_fetch_array($Edit_Result);
// The form
echo "
<FORM ACTION=$PHP_SELF METHOD=POST>
Quote: <INPUT TYPE=text NAME=Edit_Quote SIZE=20 VALUE="$Edit_Row[0]"><BR>
By: <INPUT TYPE=text NAME=Edit_By SIZE=10 VALUE="$Edit_Row[1]"><BR>
<INPUT TYPE=hidden name=Edit_ID VALUE=$ID>
<INPUT TYPE=submit NAME=submit_edit VALUE="Edit Quote">
</FORM>
";
}
else
{
// The normal form
echo "
<FORM ACTION=$PHP_SELF METHOD=POST>
Quote: <INPUT TYPE=text NAME=Add_Quote SIZE=20><BR>
By: <INPUT TYPE=text NAME=Add_By SIZE=10><BR>
<INPUT TYPE=hidden name=Date_Added VALUE=$date_added>
<INPUT TYPE=submit NAME=submit VALUE="Add Quote">
</FORM>
";
}
if ($submit)
{
$Insert_Query = "INSERT INTO Quote (Quote, Quote_by, Date_Added) VALUES ('$Add_Quote', '$Add_By', '$date_added')";
$Execute_Insert = mysql_query($Insert_Query)
if ($Execute_Insert) { echo "Insertion Successfull!"; } else { echo "INSERTION FAILED: mysql_error($Execute_Insert)"; }
}
elseif ($submit_edit)
{
$Update_Query = "UPDATE Quotes SET Quote=$Edit_Quote Quote_By=$Edit_By WHERE QID=$Edit_ID";
$Execute_Update = mysql_query($Update_Query);
if ($Execute_Insert) { echo "Edit Successfull!"; } else { echo "EDIT FAILED: mysql_error($Execute_Update)"; }
}
elseif($delete)
{
$Delete_Query = "DELETE * FROM Quotes WHERE QID=$delete";
$Execute_Delete = mysql_query();
if ($Execute_Insert) { echo "Deletion Successfull!"; } else { echo "DELETION FAILED: mysql_error($Execute_Update)"; }
}
else
{
}
// Close DB connection
mysql_close() OR die("CLOSE FAILED: mysql_error()");
?>
|
|
Subscribe Now to receive new posts via Email as soon as they come out.
Comments
Post your comments

