CyberShot Posted October 11, 2009 Share Posted October 11, 2009 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'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/177264-help-with-little-code/ Share on other sites More sharing options...
alexkorep Posted October 11, 2009 Share Posted October 11, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/177264-help-with-little-code/#findComment-934657 Share on other sites More sharing options...
CyberShot Posted October 11, 2009 Author Share Posted October 11, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/177264-help-with-little-code/#findComment-934661 Share on other sites More sharing options...
alexkorep Posted October 11, 2009 Share Posted October 11, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/177264-help-with-little-code/#findComment-934675 Share on other sites More sharing options...
CyberShot Posted October 11, 2009 Author Share Posted October 11, 2009 I thought about javascript, but thought there might be a way to do it with php. I tried using a loop but when I do that, it doesn't loop the script, it loops through and prints a fiew characters of the quotes. Quote Link to comment https://forums.phpfreaks.com/topic/177264-help-with-little-code/#findComment-934677 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.