Page 1 of 1
Pull ordered line from text file, instead of random line?
Posted: Tue Jul 06, 2010 12:56 pm
by shanehunter
Hey guys,
I have this code:
Code: Select all
function RandomLine($seed) {
$textfile = "links.txt";
$sites = array();
if(file_exists($textfile)){
$sites = file($textfile);
srand ($seed);
$string = $sites[array_rand($sites)];
} else {
$string = "Error";
}
return $string;
}
$s0 = RandomLine(rand((float) microtime(), 10000000));
echo "$s0";
This code extracts a random line from a text file 'links.txt'
I'm looking for code that will pull each line, sequentially from start to finish, one by one.
Only one line should come up each time this script is run via cron job.
How would I do this? Please, I need help with the code, as I'm a newb to PHP. So one word answers, variables, or otherwise won't be of much use to me.
Re: Pull ordered line from text file, instead of random line
Posted: Tue Jul 06, 2010 2:05 pm
by AbraCadaver
You'll need a way to track what line has already been called. You can store this in a database or a file. Here's a quick hack, it could be shortened up a bit:
Code: Select all
function NextLine() {
$counterfile = "counter.txt";
if(file_exists($counterfile)) {
$count = file_get_contents($counterfile);
}
if(empty($count)) {
$count = 0;
}
$textfile = "links.txt";
if(file_exists($textfile)){
$sites = file($textfile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$string = $sites[$count];
} else {
$string = "Error";
}
$count++;
if($count >= count($sites)) {
$count = 0;
}
file_put_contents($counterfile, $count);
return $string;
}
$s0 = NextLine();
echo "$s0";
Re: Pull ordered line from text file, instead of random line
Posted: Tue Jul 06, 2010 2:15 pm
by Jade
You can write to the file with the last quote you used but using a database would be faster. Create a table with a auto-incrementing quoteID, the quoteText and a used boolean. Then you can do something like this:
Code: Select all
<?php
//connect to your database first
//check to see if all the quotes have been displayed yet, if so, reset them
$total_left_unused = mysql_query("SELECT 1 FROM quotestable WHERE used=0")
or die (mysql_error());
//there are no unused quotes left, reset them all
if (!mysql_num_rows($result))
mysql_query("UPDATE quotestable SET used=0")
or die ('cannot reset used quotes ' . mysql_error());
//select one un-used quote from the database at random
$result = mysql_query("SELECT * FROM quotestable WHERE used=0 ORDER BY RAND() LIMIT 1")
or die ('cannot select an unused quote: ' . mysql_error());
$row = mysql_fetch_array($result);
echo $row['quoteText']; //here's your one random quote
//change the quote so the status is set to used
mysql_query("UPDATE quotestable SET used=1 WHERE quoteID = '" . $row['quoteID'] . "'")
or die ('cannot update quote as being used ' . mysql_error());
?>
Re: Pull ordered line from text file, instead of random line
Posted: Tue Jul 06, 2010 2:59 pm
by shanehunter
Hmm.
any way to pull the line, use it in the script then just immediately remove it from the text file?
so, call line 1 from links.txt for use in script.php
one it's called, and used, delete the line 1 from links.txt
I think what I'm looking for, is to always pull the 1st line from the text file, but to delete that same line after it's been placed into an array for use in script.php
The problem is, I'm letting some of my subscribers use this - and explaining database setup to them would be over their heads.
I need this to all be contained within: links.txt and script.php
thanks so much for your help guys, let me know.
Re: Pull ordered line from text file, instead of random line
Posted: Tue Jul 06, 2010 3:18 pm
by AbraCadaver
Even easier:
Code: Select all
function NextLine() {
$textfile = "links.txt";
if(file_exists($textfile)){
$sites = file($textfile, FILE_SKIP_EMPTY_LINES);
if(!empty($sites)) {
$string = array_shift($sites);
file_put_contents($textfile, implode($sites));
} else {
$string = "Empty";
}
} else {
$string = "Error";
};
return $string;
}
Re: Pull ordered line from text file, instead of random line
Posted: Tue Jul 06, 2010 5:21 pm
by shanehunter
Thanks SO much!
That works perfectly!
Next question:
What happens once this gets to the end of the text file? Will it start over, or just stop?
Re: Pull ordered line from text file, instead of random line
Posted: Tue Jul 06, 2010 5:24 pm
by John Cartwright
shanehunter wrote:Thanks SO much!
That works perfectly!
Next question:
What happens once this gets to the end of the text file? Will it start over, or just stop?
It will stop.
And also, this line
Code: Select all
file_put_contents($textfile, implode($sites));
should probably be
Code: Select all
file_put_contents($textfile, implode(PHP_EOL, $sites));
Re: Pull ordered line from text file, instead of random line
Posted: Tue Jul 06, 2010 5:34 pm
by shanehunter
Thanks John!
Will do, but what does IT do? What's the difference? =)
Re: Pull ordered line from text file, instead of random line
Posted: Tue Jul 06, 2010 5:39 pm
by John Cartwright
implode() without the glue parameter will simply implode the database with an empty character.
I.e.,
Code: Select all
$myarray = array('foo','bar','fee','bah');
echo implode($myarray); //foobarfeebah
//instead of
echo implode(PHP_EOL, $myarray);
//foo
//bar
//fee
//bah
The PHP_EOL constant is basically a safe way to define newlines between different OS.
Re: Pull ordered line from text file, instead of random line
Posted: Tue Jul 06, 2010 7:31 pm
by AbraCadaver
Since we're using file(), each array element will end with a newline. That's why I removed the FILE_IGNORE_NEW_LINES.