Re: voting using text files
| From: | 1LT John W. Holmes | Date: | Wed, 22 May 2002 18:15:06 +0000 |
| Subject: | Re: voting using text files | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-98786@lists.php.net to get a copy of this message | ||
> A good structure would be to have one file for each possible answer and
each
> file contains the number of votes it has recieved.
> Then:
> --> Open file for the chosen option as read only
> --> Read the value in the file
> --> Close the file
> --> Increase the value by one using ++
> --> Open the file again in write mode
> --> Lock the file
> --> Write the new value to the file - old one overwritten
> --> Unlock the file
> --> Close the file
That's a bad method. You have to have the lock around the read and the
write. With your method, 5 users might read the file, all getting 99 for the
count, and then each one will try to seperatly write 100 to the file. So you
lose 4 actual counts. You want to open the file, read it, update value,
write it, unlock, and close the file.
Using multiple files would just be a waste of space in my opinion. A locked
file doesn't stop the script, it simply waits for the file to be unlocked
and then continues on.
---John Holmes...