News:

Wondering if this will always be free?  See why free is better.

Main Menu

Include one line from txt file

Started by Owdy, June 26, 2005, 02:56:39 PM

Previous topic - Next topic

Owdy

Lets say i got aphorism.txt. There are one aphorism per line. What is easy way to print one line per time with include or similar?
Former Lead Support Specialist

Tarvitsetko apua SMF foorumisi kanssa? Otan työtehtäviä vastaan, lue:http://www.simplemachines.org/community/index.php?topic=375918.0

Chris Cromer

Do you mean going through a loop of the lines in that file? Or printing one at random from that file? Or am I missunderstanding what you want to do?
Chris Cromer

"I was here, here I was, was I here, sure I was" - The little voice in my head.

Kirby

Quote from: Owdy on June 26, 2005, 02:56:39 PM
Lets say i got aphorism.txt. There are one aphorism per line. What is easy way to print one line per time with include or similar?
You could use file(<file path>), it will make an array with every line in it.

Owdy

Quote from: Chris Cromer on June 26, 2005, 02:59:10 PM
Do you mean going through a loop of the lines in that file? Or printing one at random from that file? Or am I missunderstanding what you want to do?
One random line. So i write 20 aphorisms and then somehow print one at the time to my site.
Former Lead Support Specialist

Tarvitsetko apua SMF foorumisi kanssa? Otan työtehtäviä vastaan, lue:http://www.simplemachines.org/community/index.php?topic=375918.0

Kirby

something like:

$aphorisms = @file(<file path>);
echo $aphorisms[rand(0, count($aphorisms))];

Owdy

#5
What is file name? I dont understand.

Like this?
$aphorisms = @aforismi.txt(<file path>);
echo 
$aphorisms[rand(0count($aphorisms))];


With .shtml that seems to be easy http://www.opus1.com/www/demo/random.htmlx
Former Lead Support Specialist

Tarvitsetko apua SMF foorumisi kanssa? Otan työtehtäviä vastaan, lue:http://www.simplemachines.org/community/index.php?topic=375918.0

Chris Cromer

No like this:
$aphorisms = @file('aphorism.txt');
echo $aphorisms[rand(0, count($aphorisms))];


You where supposed to replace "<file path>" with the name of the file.
Chris Cromer

"I was here, here I was, was I here, sure I was" - The little voice in my head.

Owdy

Hey it works! Thanks both :)

I also find this from net:

<?
  // Quick random quote
  // This script reads a quote file (flat text file), and picks one of the loaded
  // quotes at random and displays it.
  // Line breaks and formatting can be put into the quotefile as HTML tags
  // Place the quotes.txt on your site, edit the $quotefile variable below,
  // And copy and paste this code into your PHP page.
 
  // $delim tells this script what delimits the quotes (default is a CR/LF)
  $delim = "\n";
 
  // $wuotefile points to the file that holds the actual quotes
  $quotefile = "quotes.txt";
 
  $fp = fopen($quotefile, "r");
  $contents = fread($fp, filesize($quotefile));
  $quote_arr = explode($delim,$contents);
  fclose($fp);
  srand((double)microtime()*1000000);
  $quote_index = (rand(1, sizeof($quote_arr)) - 1);
  $herequote = $quote_arr[$quote_index];
  echo $herequote;
?>
Former Lead Support Specialist

Tarvitsetko apua SMF foorumisi kanssa? Otan työtehtäviä vastaan, lue:http://www.simplemachines.org/community/index.php?topic=375918.0

Chris Cromer

Yeah that does the exact same thing but uses different commands... except that file puts it into an array already instead of you having explode it into one.
Chris Cromer

"I was here, here I was, was I here, sure I was" - The little voice in my head.

Owdy

How about random aphorism a day? I mean that it only changes one time per day?
Former Lead Support Specialist

Tarvitsetko apua SMF foorumisi kanssa? Otan työtehtäviä vastaan, lue:http://www.simplemachines.org/community/index.php?topic=375918.0

Kirby

You can make it store it in modSettings, and then have it reset every 24 hours (determined via page view, just like auto optimization).

Owdy

hmmm, not sure that i know how to do that...
Former Lead Support Specialist

Tarvitsetko apua SMF foorumisi kanssa? Otan työtehtäviä vastaan, lue:http://www.simplemachines.org/community/index.php?topic=375918.0

morph

Or make 31 lines of aphorism's , then do something like


<?php

$aphorisms 
= @file('aphorism.txt');
$date date("d");
echo 
$date{0} == $aphorisms[strtr($date, array("0" => ''))] : $aphorisms[$date];

?>


Coming soon!

Advertisement: