Jump to content

Read next line from file after hits specific counter stat


Recommended Posts

//creates a text file counter to read and write to
function counter() {
global $dat;
$countFile = dirname(__FILE__)."/counter.txt";
if(!file_exists($countFile)) {

	$fh = fopen($countFile, 'w') or die("Can't open Counter");
	$dat = @fread($fh, filesize($countFile));
  	fclose($fh);
} else {

 	$fh = fopen($countFile, "r") or die("Can't read Counter");
   	$dat = fread($fh, filesize($countFile));
   	fclose($fh);

   	$fh = fopen($countFile, "w") or die("Cant write to Counter");
 	fwrite($fh, intval($dat)+1);	
               // at x number, in this case 5, rewrite file and start counter over
	if($dat == 5) {
		$fh = fopen($countFile, 'w');
		fwrite($fh, intval(1));
	//	timeCheck();

	}
		fclose($fh);
}
}

$dat = counter();

function timeCheck() {
global $dat;

$t = time();
$b = date("h:i A", $t);
$fileName = "links2.php";
  
if (($b == "00:01 AM") || (!file_exists($fileName))) { //at 12:01 AM, go and update file
	$fileHandle = fopen($fileName, 'w') or die("can't open file");
	fclose($fileHandle);

	$fh = fopen($fileName, 'w');

	$stringData = file_get_contents('links.php');
	fwrite($fh, $stringData);
	fclose($fh);
	rename("links2.php", dirname(__FILE__)."/links2.php"); 
}

if(file_exists($fileName)) {

	$link = dirname(__FILE__)."/links2.php";

	$handle = fopen($link, 'r');
	$lines = file($link);

	if($dat == 5) {
		shuffle($lines);
		array_pop($lines);	
	}				
		print end($lines);
		fclose($handle); 
}

} 

The first function is the counter, that part works. The second function is the one that I have a problem with. What it's supposed to do is at a certain time, go out and grab the file in question if it does not exist or it is at that required time. When it does, it opens the file and puts all the contents into an array. What it's supposed to do is after the counter goes to a certain number (say 5 in this case), it's supposed to print the next line from the array, but it doesn't. I tried using pop/shift, end, shuffle and all the array features to try and get the next line to read, but have no idea how to actually to get it to go to the next line. I tried shuffling after a certain number, but the shuffled result does not stay, it always goes back to the first value. (That's what I have in the function right now.)

 

I have to point out also that databases cannot be used, hence why all the files. Any ideas?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.