954,219 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

problem with counter

i have this code which is supposed to increment a counter on page

<?php  
 

//create a file called counter.txt and upload it to your server
//now open the file
$fp = fopen("counter.txt" , "r");
//read in the current count
$count = (int)fread($fp, 1024);
//increment the count by 1
$count++;
//close the file
fclose($fp);
//image display, get the lentgth of the count





echo"<table cellspacing=0 width =4% cellpadding=0 row=1 column=".strlen($count);
echo"<tr>";
for ($i = 0 ;$i < strlen($count) ; $i++)
{
$imgsrc = substr($count,$i ,1);
//display the image(s) note our images are in a folder located at
//images/count1/ change this to your location
echo"<td width=2%>";
echo "<img src =\"images/count1/" . $imgsrc . ".gif\">";
echo"</td>";
}
echo"</tr>";
echo"</table>";


//open the counter file
$fp = fopen("counter.txt", "w");
//write the new count to the file
fwrite($fp, $count);
//close the file
fclose($fp);
//include("grcounter.php"); 

 ?>


it is reading from text file and display the value from file but it is failing to update the file with the new value which is an increment of the counter

tirivamwe
Light Poster
36 posts since Oct 2005
Reputation Points: 11
Solved Threads: 2
 

try this:

<?php
$open = fopen('counter.txt', 'r');
$read = fread($open, filesize('counter.txt'));
fclose($open);
$count = $read + 1;
$open = fopen('counter.txt', 'w');
$write = fwrite($open, $count);
fclose($open);

echo""; for ($i = 0 ;$i < strlen($count) ; $i++) { $imgsrc = substr($read,$i ,1); //display the image(s) note our images are in a folder located at //images/count1/ change this to your location echo""; echo """; echo""; } echo""; echo""; ?>

kkeith29
Nearly a Posting Virtuoso
1,353 posts since Jun 2007
Reputation Points: 235
Solved Threads: 195
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You