problem with counter

Reply

Join Date: Oct 2005
Posts: 36
Reputation: tirivamwe is an unknown quantity at this point 
Solved Threads: 2
tirivamwe's Avatar
tirivamwe tirivamwe is offline Offline
Light Poster

problem with counter

 
0
  #1
Oct 5th, 2007
i have this code which is supposed to increment a counter on page


  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: problem with counter

 
0
  #2
Oct 5th, 2007
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>";
?>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC