Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: PHP Random Quote Generator  (Read 3171 times)

0 Members and 1 Guest are viewing this topic.

rubylynn

  • Guest
PHP Random Quote Generator
« on: July 03, 2009, 10:22:09 PM »
Ok,  I have written this script for a Random Quote Generator.  What it is suppose to do is this.

Select a quote randomly from quotes.txt file. 
Write a quote to the quotes.txt file that you submit.

My code is here:
<?php

function printHeader ()
{
$header = '<html>
<head>
<title>Random Quotes Generator and New Quotes Adder</title>
</head>
<body>
<table cellspacing=1 cellpadding=1 border=1 align="center">';

print($header);
}

function printTrailer ()
{
$trailer = '<tr>
<td>
<center>


<form method="post" action="lab2.php">
<input type="hidden" name="choice" value="generate">
<input type="submit" value="Generate Random Quote">
</form>


<form method="post" action="lab2.php">
<input type="hidden" name="choice" value="add">
<textarea name="uquote" rows=5 cols=60>
Enter your favorite quote here!
</textarea>




<input type="text" name="uauth" size=50 value="Type author name here">




<input type="submit" value="Submit New Quote">
</form>
</center>
</td>
</tr>
</table>

</body>
</html>';

print($trailer);
}

function printQuote ($quote, $author)
{
$quotation = "<tr>
<td>


<center>
$quote


$author
</center>




</td>
</tr>";

print($quotation);
}

// Get the choice
$choice = $_POST['choice'];

$fileName = "quotes.txt";
printHeader();

if ($choice == "generate")
{
   // Read all the lines of the quotation file into an array.
   $quotationLines = file ($fileName, FILE_IGNORE_NEW_LINES);

   // Each quotation is spread over two contiguous lines in the quotation file - first
   // line has the quotation text and second has the author information.
   $numQuotes = count($quotationLines) / 2;

   // Select a random quote from existing quotations, and display it.
   $selected = rand(0, ($numQuotes-1));   // Array indexing starts at 0.

   // Ith quotation starts at line 2*I and ends at line 2*I+1 .
   printQuote($quotationLines[2*$selected], $quotationLines[2*$selected+1]);
}
else if ($choice == "add")
{
   $uquote = $_POST['uquote'];
   $uauth = $_POST['uauth'];

   // Open the quotation file in append mode
   $handle = fopen($fileName, 'a');
   fputs($handle, $uquote . "\n");
   fputs($handle, "- " . $uauth . "\n");
   fclose($handle);
   
}

printTrailer();

?>


Now I was told that the quotes.txt does not have newline ending for the last line and I needed to Fix it before trying to add new quotes to this file via the script.

What do they mean by not having a newline ending?  My quote file consists of the following:

Humpty Dumpty was pushed.
- Unknown
How come there's only one Monopolies Commission?
- Unknown
"Television won't matter in your lifetime or mine."
- Radio Times editor Rex Lambert, 1936
"Everything worthwhile has already been invented."
- director of the US Patent Office, 1899.
"A computer lets you make more mistakes faster than any invention in human history--with the possible exceptions of handguns and tequila."
- Mitch Ratliffe
"Cats are smarter than dogs. You can't get eight cats to pull a sled through snow."
- Jeff Valdez
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
- Rich Cook
"No *censored* ever won a war by dying for his country. He won it by making the other poor dumb *censored* die for his country."
- Gen. George S. Patton
"The Swiss have an interesting army. Five hundred years without a war. Pretty impressive. Also pretty lucky for them. Ever see that little Swiss Army knife they have to fight with? Not much of a weapon there. Corkscrews. Bottle openers. 'Come on, buddy, let's go. You get past me, the guy in back of me, he's got a spoon. Back off. I've got the toe clippers right here.'"
- Jerry Seinfeld
There is only one difference between a madman and me. I am not mad.
- Salvador Dali
"Radio has no future." "X-rays are clearly a hoax." "The aeroplane is scientifically impossible."
- Royal Society president Lord Kelvin, 1897-9.
"The atom bomb will never go off - and I speak as an expert in explosives."
- U.S. Admiral William Leahy in 1945.
this fortune would be seven words long if it were six words shorter.
- Unknown