Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array problem

Status
Not open for further replies.

asm1

Technical User
Oct 9, 2003
77
GB
The powers that be whant a page counter but the counter always returns 1 like TRUE I think because even if I put 2 in the text file, it is over written with 1. have a feeling it is to do with the array as I haven’t just got my head into how they work in php. Any suggestions would be appreciated. thanks
Code:
$fileForCounter = "indexCounter.txt";
   $filePointer = fopen ($fileForCounter, "r");
   if($filePointer) {
	  $data = file ($theFile);
	  $somenum = $data[theData];
	  $counter = $somenum[0];
	  $counter = $counter + 1;
	  fclose ($filePointer);
	}else{
	  print "Open File Failed";
	}
		
	$fileForCounter = "indexCounter.txt";
   $filePointer = fopen ($fileForCounter, "w");
	if($filePointer) {
	   fwrite ($filePointer, $counter);
		fclose ($filePointer);
	}else{
	   print "write to File Failed";
	}
 
You code has multiple problems.

You're using fopen to open the file, yet invoking file() to read the file. If you're trying to determine whether the file exists, I recommend the use of file_exists() (
When you read in the entire file by invoking file(), $data will have each line of the file as a separate element of the array. If the file consists of only the number on a single line, something like

$counter = $data[0];

should be all you need to get the current counter value.

A handy tool to help you understand arrays is the PHP function print_r() (

There are also problems with things I don't see in the script. You must assume that a web app will be a multi-user application. You're going to need to use something like flock() to insure that only one instantiation of the script at a time is modifying the counter.

An alternative to storing the counter in a file is to store the counter on a database server and update it using atomic operations. It takes care of the multiuser problem nicely.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Thanks the print_r looks helpful. I was aware that I would have to lock the file but I didn’t go looking for the method, as I wanted to get the writing to and from a file working first.

I didn’t consider the database aspect as I was only taking one number from a file and thought that this would use less sever resources than a database ie there is no search or order needed to the data and I also didn’t want to take the easier option if it was not the best option. Has anybody any suggestions on which is the best option. Write to file or database when only incrementing a number by 1.
 
I would use the database server. You don't have to worry about file locking, as the database server will take care of concurrency problems for you.

And I don't think it'll that much more resources, provided that you're using a database server that PHP can communicate with using native libraries, like MySQL or PostgreSQL.

All it would take is to initialize a table with one record which has one column with a value of zero. Then your scripts can update the value by simply issuing the query "UPDATE tablename SET columname = columnname + 1".

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Thank for your prompt reply, i will move the counter to a database table.

i got the file counter working, had to do it for my own sanity. The print_r proved very helpful. Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top