Be a Supporter!

PHP: Create a random quotes script

  • 3,063 Views
  • 12 Replies
New Topic Respond to this Topic
DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
PHP: Create a random quotes script 2005-11-03 19:44:16 Reply

PHP: Main

Creating a random quotes script with PHP

In this tutorial, you will learn how to create a random quotes script, step by step.

You should already know: Basic PHP

So, lets begin!

Here's our first part of code:

<html>
<head>
<title>A Random Quote</title>
</head>
<body>

This starts the HTML page, defines the title of the page (A Random Quote), and begins the body.

Lets look at our next piece of code:

<?php

As you probably already know, this begins our PHP script.

And the next group of code we have is this:

// Declare all of the quotes we will use in an array

$quotes[] = '"I always tried to turn every disaster into an opportunity. ~ John D. Rockefeller"';
$quotes[] = '"It\'s been reported that Britney Spears is having financial trouble and recently took out a $5 million loan. Kevin Federline offered to co-sign the loan and then everyone had a big laugh."';
$quotes[] = '
"Yesterday, Baltimore slugger Rafael Palmiero tested the positive for steroids a few months after telling congress, \'I never took steroids, period.\' Then today, Palmiero said, \'I mean to say \'I never took steroids, question mark.\'"';
$quotes[] = '"This week, rapper Snoop Dogg started doing a series of commercials for Chrysler automobiles. This marks the first time Snoop has recommended a dealer who sells cars."';
$quotes[] = '
"Britney Spears is reportedly fighting with her husband Kevin Federline because he\'s insisting that their child be named after him. Apparently Britney told him, \'Why would I want to name him \'Lazy Jackass?\'"';

This is the heart of our random quotes script. It's where we define all of our quotes in something called an array. So, we say $quotes[] = 'Some quote'

What that means is, we add on to the array, which is named "quotes". Each quote actually has a number. So, at any time, we can do something like: $quotes[3] Which would actually call the second quote in our list. When we say: $quotes[] = 'something', we don't put a number in the []. You can, but PHP autonumbers it, so it automaticlay assigns the next number to that quote.

Another important thing to note is that the array numbering starts at 0. So, the first quote would be acessed like this: $quotes[0].

Now, lets move on to the next piece of code:

// Get the number of quotes we have, and subtract one because arrays start at 0

$number_quotes = count($quotes) - 1;

The purpose of this line is simple. We create a variable, and then assign the value of the upper range to it. So, if we have 10 quotes, the value of the variable becomes 9. Here's how that happens. we use the count() function, which counts all of the elements in the array $quotes. So, if we had 10 quotes, that count($quotes) would come up with 10. Then, we subtract 1 from that 10. We do that because the array starts at 0, so we don't have an array element 10.

Lets go on to the next piece of code:

// Pick a quote number randomly between 0, the beggining of the array, and $number_quotes which stands for the number of qoutes we have minus 1

$quote_number = rand(0, $number_quotes);

This is where we actually select a number randomly to use to get the random quote. So, we use the rand() function. First we have rand(0 That says the minimum our random number can be is 0. Then, we have a $number_quotes, which says our maximum random number can be the value of $number_quotes, the number of quotes we have minus 1.

As you can see, we give the variable $quote_number the value of the random number we just selected.

Lets look at our last piece of code:

// Ouotput the quote that matches the random number we came up with

echo $quotes[$quote_number];

On that line, we access one of the quotes in our quote array by using $quotes[$quote_number]. Lets say $quote_number (our random number) had a value of 5. It turns that line into: echo $quotes[5]. So, it takes the quote that has they key of the random number chosen, and outputs it to the screen.

Just in case you missed something, here is the complete random quotes script:

<html>
<head>
<title>A Random Quote</title>
</head>
<body>
<?php

// Declare all of the quotes we will use in an array

$quotes[] = '"I always tried to turn every disaster into an opportunity. ~ John D. Rockefeller"';
$quotes[] = '"It\'s been reported that Britney Spears is having financial trouble and recently took out a $5 million loan. Kevin Federline offered to co-sign the loan and then everyone had a big laugh."';
$quotes[] = '
"Yesterday, Baltimore slugger Rafael Palmiero tested the positive for steroids a few months after telling congress, \'I never took steroids, period.\' Then today, Palmiero said, \'I mean to say \'I never took steroids, question mark.\'"';
$quotes[] = '"This week, rapper Snoop Dogg started doing a series of commercials for Chrysler automobiles. This marks the first time Snoop has recommended a dealer who sells cars."';
$quotes[] = '
"Britney Spears is reportedly fighting with her husband Kevin Federline because he\'s insisting that their child be named after him. Apparently Britney told him, \'Why would I want to name him \'Lazy Jackass?\'"';

// Get the number of quotes we have, and subtract one because arrays start at 0

$number_quotes = count($quotes) - 1;

// Pick a quote number randomly between 0, the beggining of the array, and $number_quotes which stands for the number of qoutes we have minus 1

$quote_number = rand(0, $number_quotes);

// Ouotput the quote that matches the random number we came up with

echo $quotes[$quote_number];

?>
</body>
</html>

If you have any questions, just post them in this thread.


BBS Signature
Gylfi
Gylfi
  • Member since: Aug. 20, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to PHP: Create a random quotes script 2006-01-27 08:35:30 Reply

Nice tutorial. But i have some problems, seeing as im completely new at PHP i tried testing my script on the local host(dont know if i defined my site or localhost rite, well get to that later) and it didn't do anything, it just showed me the code used to write the site and when i switch to 'Desing' theres a little icon in the corner that says PHP. I need help i have barely ever looked at PHP scripts.

nixopax
nixopax
  • Member since: Jan. 10, 2005
  • Offline.
Forum Stats
Member
Level 20
Programmer
Response to PHP: Create a random quotes script 2006-01-27 09:14:00 Reply

You need a server that can handle PHP scripts. Apache is a personal favourite of mine, but unless you know what you're doing, configuring and installing can be a little tricky. So, if you want a quick painless install, I'd suggest using WAMP server. It runs on Windows, and has Apache, MySQL, and PHP bundled in the install. You can download it here

Simply install that, choose your site directory, and then save all your PHP files in that directory. Then run WAMP. Based on you saying "design view" I'm going to assume you are using dreamweaver. When dreamweaver first opens up, there will be a dialog box for you to open a recent item, create a new item, and a few other things. In the Create New section, choose DREAMWEAVER SITE. A popup will appear. Go to the advanced tab. On the left hand of the popup will be the categories of the site that you'll have to work with. First, in local info, change the local root folder to the WAMP server file directory. In site name, call it whatever you want. Now, go to the Testing Server category (left hand side of the popup). Choose a PHP MySQL server model, and change access to Local/Network. Now set the testing server folder to be the same as local root folder. Press okay. Now, create the PHP script. Save the file as a ".php" extension. In the php file that you want to test, press F12. This will preview your script in your localhost. And that's about it.

I like the random quotes tutorial GamesCool. You've made it so that no matter how many quotes you have, the script automatically checks to see how many there are. I usually do static numbers, but your method is much better. :-D

Pilot-Doofy
Pilot-Doofy
  • Member since: Sep. 13, 2003
  • Offline.
Forum Stats
Member
Level 37
Musician
Response to PHP: Create a random quotes script 2006-01-27 10:50:38 Reply

At 1/27/06 08:35 AM, M4TR1X wrote: Nice tutorial. But i have some problems, seeing as im completely new at PHP i tried testing my script on the local host(dont know if i defined my site or localhost rite, well get to that later) and it didn't do anything, it just showed me the code used to write the site and when i switch to 'Desing' theres a little icon in the corner that says PHP. I need help i have barely ever looked at PHP scripts.

First of all, it would help if you told us what program you're using. There is more than one program to edit PHP with, so help me out there. Secondly, if it's showing you the source, it's probably a problem with PHP not being installed.

If you want to run PHP you have to have it installed...hard one to figure out eh?

TheDoodleTwins
TheDoodleTwins
  • Member since: Jun. 23, 2006
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to PHP: Create a random quotes script 2006-07-17 14:33:16 Reply

I dident read threw it so it might do somthing different but it can be alot simple if you want to just create a random quote:

Just write this in a php file.

<?php
$filename="words.txt";
$words=file($filename);
shuffle($words);
$word=$words[0];
echo $word;
?>

Then create a .txt called "words" and write a differnt sentance on each line...its that simple.

But does your script do somthing differnt, i realy dident look threw it, it looked to boring, srry.

DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to PHP: Create a random quotes script 2006-07-17 14:41:59 Reply

At 7/17/06 02:33 PM, GwenTheDoodle wrote: I dident read threw it so it might do somthing different but it can be alot simple if you want to just create a random quote:

Just write this in a php file.

<?php
$filename="words.txt";
$words=file($filename);
shuffle($words);
$word=$words[0];
echo $word;
?>

Then create a .txt called "words" and write a differnt sentance on each line...its that simple.

But does your script do somthing differnt, i realy dident look threw it, it looked to boring, srry.

Why is that simpler? Both scripts are the exact same amount of lines. Any way these tutorials are for teaching purposes.


BBS Signature
TheDoodleTwins
TheDoodleTwins
  • Member since: Jun. 23, 2006
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to PHP: Create a random quotes script 2006-07-17 14:44:16 Reply

In what way are they the same ammount of lines! Your script has a hell of alot more!

But yes i get what you meen by teaching!

DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to PHP: Create a random quotes script 2006-07-17 14:45:40 Reply

At 7/17/06 02:44 PM, GwenTheDoodle wrote: In what way are they the same ammount of lines! Your script has a hell of alot more!

But yes i get what you meen by teaching!

Because all of the lines of the array are the quotes...


BBS Signature
TheDoodleTwins
TheDoodleTwins
  • Member since: Jun. 23, 2006
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to PHP: Create a random quotes script 2006-07-17 14:51:43 Reply

So, there is still alot more to write and understand in yours!

DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to PHP: Create a random quotes script 2006-07-17 14:53:02 Reply

At 7/17/06 02:51 PM, GwenTheDoodle wrote: So, there is still alot more to write and understand in yours!

I disagree. To me opening a file, making sure the path is correct (full path), and maybe having to modify the permissions on the file is more complicated.

That's just my opinion. Others might disagree.


BBS Signature
DannyIsOnFire
DannyIsOnFire
  • Member since: Apr. 14, 2005
  • Offline.
Forum Stats
Member
Level 21
Movie Buff
Response to PHP: Create a random quotes script 2006-07-17 14:56:39 Reply

At 7/17/06 02:53 PM, GamesCool wrote:
At 7/17/06 02:51 PM, GwenTheDoodle wrote: So, there is still alot more to write and understand in yours!
I disagree. To me opening a file, making sure the path is correct (full path), and maybe having to modify the permissions on the file is more complicated.

That's just my opinion. Others might disagree.

Plus it uses 2 files rather than one.
I can see where this guys coming from, since your script does look a lot longer, but thats to do with the actual quotes, and the //lines.

I think it comes down to personal choice really...


|| Portfolio || Facebook || Twitter ||

BBS Signature
TheDoodleTwins
TheDoodleTwins
  • Member since: Jun. 23, 2006
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to PHP: Create a random quotes script 2006-07-17 14:57:19 Reply

At 7/17/06 02:53 PM, GamesCool wrote:
At 7/17/06 02:51 PM, GwenTheDoodle wrote: So, there is still alot more to write and understand in yours!
I disagree. To me opening a file, making sure the path is correct (full path), and maybe having to modify the permissions on the file is more complicated.

That's just my opinion. Others might disagree.

Well maby, you know a hell of alot more about php than me, im having problems with a simple script at the moment!

DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to PHP: Create a random quotes script 2006-07-17 14:58:18 Reply

At 7/17/06 02:57 PM, GwenTheDoodle wrote: Well maby, you know a hell of alot more about php than me, im having problems with a simple script at the moment!

Maybe make a thread so we can help?


BBS Signature