| | |
problem with counter
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
i have this code which is supposed to increment a counter on page
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
PHP Syntax (Toggle Plain Text)
<?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
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>";
?>
<?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>";
?>
![]() |
Similar Threads
- bank system problem !!!! (C++ programming) (C++)
- Find the duplicate numbers in a set of ints (C++)
- Help with calculating avrg from input file. (C++)
- 6600 GT AGP valve issues (Monitors, Displays and Video Cards)
- c:\counter.cab:\counter.exe (Viruses, Spyware and other Nasties)
- letter and word counter (C)
Other Threads in the PHP Forum
- Previous Thread: php email
- Next Thread: Toll of a function call
| Thread Tools | Search this Thread |
.htaccess apache api array autocomplete beginner binary body broken cakephp class cms code convert cron curl database dataentry date date/time display duplicates dynamic ebooks email emptydisplayvalue error execute explodefunction file firstoptioninphpdroplist folder form forms function functions google hack href htaccess html htmlspecialchars image include ip javasciptvalidation javascript joomla keywords limit link login mail matching mediawiki menu methods multiple mycodeisbad mysql network number object oop paypal pdf php phpincludeissue query random recursive redirect remote script search securephp server sessions shot source sp space speed sql subdomain subscription system table tag tutorial tutorials upload url validator variable vbulletin video web white youtube






