Forum Moderators: coopster
Because I got such good help here last time I had a query, I thought I would post my problem here again :)
I'm assisting to make a website, and a simple hit counter is wanted, now I'm using a text file hit counter with the code:
<?php
$counttxt = ("counter.txt");
$hits = file($counttxt);
$hits[0] ++;
$fp = fopen($counttxt, "w");
fputs($fp, $hits[0]);
fclose($fp);
echo '<p class="small">There have been ' . $hits[0] . ' page hits since 20/12/2009</p>';
?> with
include ("counter.php"); To display it
Which works completely fine save for one minor cosmetic detail. I want it to have a minimum number of digits in the output (so it would read "0001" as opposed to just "1") but not simply have a crude
echo '000' . $hits[0]; As at higher numbers that would need manual trimming.
Is this possible? And if so, how?
Regards,
Readie
Since posting this I've had a quick think and realised that something along the lines of:
if($hits < 10)
{
$num = '000';
}
else if($hits < 100)
{
$num = '00';
}
else if($hits < 1000)
{
$num = '0';
}
else
{
$num = '';
}
echo '<p class="small">There have been ' . $num . $hits[0] . ' page hits since 20/12/2009</p>'; Would work, however that seems a very inefficient method.