Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-05-2007, 05:37 PM   PM User | #1
LemonLimeLucky
New to the CF scene

 
Join Date: Jun 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
LemonLimeLucky is an unknown quantity at this point
Newbie, I need help

I'm trying to make a php file that puts random quotes on my webpage. I have a txt file full of quotes, and they're all separated by slashes ( / ) but I'm still having trouble. Each day I want a random quote to pop up. You know, like today, a George Washington quote, next day, another quote, etc. Here's what I have so far, I was wondering if anyone could tell me what I need to fix or fill in. Thanks I appreciate it!

-Lucky

Here's my coding so far (I really have no experience in PHP whatsoever, so if this can be explained as simply as possible I'd appreciate it!):

<?php
$today_counter = 3;
$fh = fopen("quotes.txt", "r");
$counter = 0;
while (!feof($fh) && $counter < $today_counter) {
$line = fgets($fh, 1024);
if (substr($line,0,1) != "#") {
$counter++;
}
}
fclose($fh);
list($person,$date,$quote) = split("/", $line);
print "Quote of the Day:\n";
print "Person: $person\n";
print "Date: $date\n";
print "Quote: $quote\n";

?>

<?php
$textfile = "Includes/Quotes.txt"; //quotes file
if ($quotes = @file("$textfile")) { //don't display errors on file open
echo $quotes[array_rand($quotes)]; //echo a random quote
}else{
echo ("default quote"); //if quotes file wasn't found, echo out a default quote
}
?>

(Something a friend sent me)

set the permisions on your txt file to 777.
<?php
$file = 'nameof file.txt';
$array = file($file);
shuffle($array);
echo "$array[0]";
?>
LemonLimeLucky is offline   Reply With Quote
Old 06-05-2007, 06:28 PM   PM User | #2
rafiki
Senior Coder

 
rafiki's Avatar
 
Join Date: Aug 2006
Location: Floating around somewhere...
Posts: 2,034
Thanks: 18
Thanked 42 Times in 42 Posts
rafiki will become famous soon enough
whats happening or not happening with you current code?
__________________
Get Firefox Now
rafiki is offline   Reply With Quote
Old 06-06-2007, 03:31 PM   PM User | #3
westmatrix99
Regular Coder

 
westmatrix99's Avatar
 
Join Date: Dec 2006
Location: South Africa
Posts: 307
Thanks: 12
Thanked 0 Times in 0 Posts
westmatrix99 is an unknown quantity at this point
Why not just save yourself some time and use a javascript or similar that means you don't have to re-invent the wheel?
PHP Code:
function initBanner() {
    if ( !
document.getElementById ) return;
    
// arguments: id, delay (amount of time in milliseconds you linger on each item)
    // set up pause onmouseover and resume onmouseout? (boolean)
var ban1 = new dw_Banner('bannerDiv'4500true);
    
// add as many items as you like
    
ban1.addItem('‘Do what you love and love what your’re doing and you’ll never work another day in your life’ Unknown source');
    
ban1.addItem('‘If you are not using your smile, your’e like a man with a million dollars in the bank account and no checkbook’ Les Giblin');
    
ban1.addItem('‘The time is always right to do what is right’ Martin Luther King Jr');
    
ban1.addItem('‘What lies behind us and what lies before us are tiny matters compared to what lies within us’ Ralph Waldo Emerson');
    
ban1.addItem('‘Integrity is not a 90 percent thing, not a 95 percent thing, either you have it or you don’t’ Peter Scotese');
    
ban1.addItem('‘Never, never, never give up’ Winston Churchill');
    
ban1.addItem('‘You must be the change you wish to see in the world’ Ghandi');
ban1.rotate(); // Begin the rotation

Then add this to the body tag!
PHP Code:
<body onLoad="initBanner()" 
Add your div wherever!
PHP Code:
<div id="bannerDiv" >
‘Nevernevernever give up’
<br>
Winston Churchill
</div
Now look I did not create this so credit goes to: www.dyn-web.com/bus/terms.html
Check their terms and stuff but I am sure it's ok

Have fun
__________________
Thanks for you support!
westmatrix99 is offline   Reply With Quote
Old 06-07-2007, 02:53 AM   PM User | #4
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,909
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
nearly there..

PHP Code:
$file 'nameoffile.txt';
$array file($file);
shuffle($array);
$quotes explode('/',$array[0]);
shuffle($quotes);
echo 
$quotes[0]; 
you may want to remove the lines starting with '#" unless they have a particular purpose ?
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is online now   Reply With Quote
Old 06-07-2007, 05:28 AM   PM User | #5
thnbgr
New to the CF scene

 
Join Date: Jun 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
thnbgr is an unknown quantity at this point
PHP Code:
$file file_get_contents('quotes.txt');
$array explode("/"$file);
$array array_rand($array1);
echo 
$array[0]; 
thnbgr is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:38 AM.


Advertisement
Log in to turn off these ads.