Jump to content

Recommended Posts

Hello. I am learning php and I thought I would try my hand at new classes by putting together a small snippet for random quotes. I can't figure out how to get the code working though. I know how to do it without the class by echoing the $quote. But I want to learn how to do it in the class. This is my first attempt at code like this. Can you please look it over and explain how to get it working? I am calling the script in my main index file with this

 

<?php
public class fav_guote()
function quotes() {
switch(rand(1,6))
{
	case 1:
		$quote = "Life is not a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming – WOW – What a Ride! /n -Anon" ;
		break; 
	case 2:
		$quote = "It was when I found out I could make mistakes that I knew I was on to something. /n -Ornette Coleman" ;
		break;
	case 3:
		$quote = "You can't build a reputation on what you're going to do /n -Henry Ford";
		break;
	case 4:
		$quote = "People are just as happy as they make up their minds to be /n -Abraham Linkcoln";
		break;
	case 5:
		$quote = "Good advice is something a man gives when he is too old to set a bad example /n -François de La Rochefoucauld ";
		break;
	case 6:
		$quote = "I wish I owned this site";
		break;
}
}
}
	$quotes = new quotes();
	echo $quotes;
?>

 

<?php include('includes/quotes.php'); ?>

Link to comment
https://forums.phpfreaks.com/topic/177264-help-with-little-code/
Share on other sites

Hi,

 

This code should work now:

 

<?php
class quotes
{
function quotes() {
	switch(intval(rand(1,6)))
	{
		case 1:
			$this->quote = "Life is not a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming - WOW - What a Ride! /n -Anon" ;
			break;
		case 2:
			$this->quote = "It was when I found out I could make mistakes that I knew I was on to something. /n -Ornette Coleman" ;
			break;
		case 3:
			$this->quote = "You can't build a reputation on what you're going to do /n -Henry Ford";
			break;
		case 4:
			$this->quote = "People are just as happy as they make up their minds to be /n -Abraham Linkcoln";
			break;
		case 5:
			$this->quote = "Good advice is something a man gives when he is too old to set a bad example /n -Francois de La Rochefoucauld ";
			break;
		case 6:
			$this->quote = "I wish I owned this site";
			break;
	}
}
}

$quotes = new quotes();
echo $quotes->quote;
?>

 

quotes - is a class name. We assign a random quote to the $quote member of this class in its constructor.

Than we create an instance of this class in the variable $quotes. And then print its $quote member.

I think the easiest way would be using <META HTTP-EQUIV="REFRESH" CONTENT="60"> in the page header. This meta tag forces browser to reload the page every 60 secons.

 

Or you can use the JavaScript as an alternative.

 

I see how it works. how do you make it so that it would display a random quote every minute instead of just when the page is refreshed?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.