943,981 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 844
  • PHP RSS
Oct 5th, 2007
0

problem with counter

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


PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3.  
  4. //create a file called counter.txt and upload it to your server
  5. //now open the file
  6. $fp = fopen("counter.txt" , "r");
  7. //read in the current count
  8. $count = (int)fread($fp, 1024);
  9. //increment the count by 1
  10. $count++;
  11. //close the file
  12. fclose($fp);
  13. //image display, get the lentgth of the count
  14.  
  15.  
  16.  
  17.  
  18.  
  19. echo"<table cellspacing=0 width =4% cellpadding=0 row=1 column=".strlen($count);
  20. echo"<tr>";
  21. for ($i = 0 ;$i < strlen($count) ; $i++)
  22. {
  23. $imgsrc = substr($count,$i ,1);
  24. //display the image(s) note our images are in a folder located at
  25. //images/count1/ change this to your location
  26. echo"<td width=2%>";
  27. echo "<img src =\"images/count1/" . $imgsrc . ".gif\">";
  28. echo"</td>";
  29. }
  30. echo"</tr>";
  31. echo"</table>";
  32.  
  33.  
  34. //open the counter file
  35. $fp = fopen("counter.txt", "w");
  36. //write the new count to the file
  37. fwrite($fp, $count);
  38. //close the file
  39. fclose($fp);
  40. //include("grcounter.php");
  41.  
  42. ?>

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
Similar Threads
Reputation Points: 11
Solved Threads: 2
Light Poster
tirivamwe is offline Offline
36 posts
since Oct 2005
Oct 5th, 2007
0

Re: problem with counter

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"<table cellspacing=0 width =4% cellpadding=0 row=1 column=".strlen($count);
echo"<tr>";
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"<td width=2%>";
echo "<img src =\"images/count1/" . $imgsrc . ".gif\">";
echo"</td>";
}
echo"</tr>";
echo"</table>";
?>
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: php email
Next Thread in PHP Forum Timeline: Toll of a function call





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC