Pull ordered line from text file, instead of random line?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shanehunter
Forum Commoner
Posts: 30
Joined: Sun Jun 27, 2010 3:43 pm

Pull ordered line from text file, instead of random line?

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Pull ordered line from text file, instead of random line

Post 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";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Pull ordered line from text file, instead of random line

Post 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());
?>
shanehunter
Forum Commoner
Posts: 30
Joined: Sun Jun 27, 2010 3:43 pm

Re: Pull ordered line from text file, instead of random line

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Pull ordered line from text file, instead of random line

Post 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;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
shanehunter
Forum Commoner
Posts: 30
Joined: Sun Jun 27, 2010 3:43 pm

Re: Pull ordered line from text file, instead of random line

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Pull ordered line from text file, instead of random line

Post 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));
shanehunter
Forum Commoner
Posts: 30
Joined: Sun Jun 27, 2010 3:43 pm

Re: Pull ordered line from text file, instead of random line

Post by shanehunter »

Thanks John!

Will do, but what does IT do? What's the difference? =)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Pull ordered line from text file, instead of random line

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Pull ordered line from text file, instead of random line

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply