PHPBuilder - Random Line Loader



RSS Twitter
Snippets File Management

Random Line Loader

by: Oron Gill Haus
|
April 26, 2001

Version: 1.0

Type: Class

Category: File Management

License: GNU General Public License

Description: Loads a file into memory one line at a time. The lines can then be accessed randomly from the class. Possible uses include blind banner ad rotation.



<?php
	// lineloader.php
	// Loads a file into memory one line at a time.  The lines can then
	// be accessed randomly from the class.  Possible uses include blind
	// banner ad rotation.
	// This code is freeware - however, please include reference to Oron and 
	// http://www.eastcoasthappy.com in your modified code.
	//
	// Author: Oron Gill Haus
	// Email:  oron@eastcoasthappy.com
		
	class Lineloader {
		
		// properties definitions
		var $filename;
		// relative paths are acceptable...fully qualified guaranteed
		var $filepath = "http://yoursite.com/flatfiles/";
		var $linearray = array();

		function Lineloader($filename) {
			$this->setFileName($filename);
		}

		function setFilename($filename) {
			$this->filename=$filename;
			$this->parseFile();
		}
			
		function getFilepath() {
			return $this->filepath;
		}

		function setFilepath($filepath) {
			$this->filepath=$filepath;
		}

		function getFilename() {
			return $this->filename;
		}

		function getLinearray() {
			return $this->linearray;
		}

		function getRandomArrayLine() {
			// choose a random line from the array and return
			// fastest randomization used - PHP recommended values
			mt_srand((double)microtime()*1000000);
			$loc = mt_rand(0,sizeof($this->linearray)-1);
			return $this->linearray[$loc];
		}

		function parseFile() {
			// parse the flat file by line and store in object's line array	
			$fd = fopen ($this->getFilepath() . $this->getFilename(), "r");
			while (!feof($fd)) {
				$buffer = fgets($fd, 4096);
				$this->linearray[] = $buffer; // add the line to the array
			}
			fclose ($fd);
		}
	
	}
?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Oron Gill Haus

Comment:



Comment:

(Maximum characters: 1200). You have characters left.