...

View Full Version : Counter



gsnedders
04-08-2004, 12:44 AM
How come you dont use a PHP counter if your site says powered by PHP?

Use this code I wrote you if you'd like.




<?php
$getcount = fopen("count.txt", "r");
while (!feof($getcount))
{
$oldcount .= fgets($getcount);
}
fclose($getcount);
$oldcount++;
$storenew = fopen("count.txt", "w");
fputs($storenew, $oldcount);
fclose($storenew);

print $oldcount;
?>


Thanks for the code, I'm now using it :) but is there anyway to add st, nd, rd, th to the end, I know it will be complicated but it would be a great help as it currently doesn't make sense.

firepages
04-08-2004, 05:47 PM
as it currently doesn't make sense.

a bit like your question ;)

what exactly do you mean by `add st, nd, rd, th to the end` , and to the end of what ?

Nightfire
04-08-2004, 05:51 PM
Think he means by having the count in a sentence, like

You are the 1st/2nd/3rd/4th/etc person here

People don't care what number they are btw

gsnedders
04-08-2004, 06:09 PM
Think he means by having the count in a sentence, like

You are the 1st/2nd/3rd/4th/etc person here

People don't care what number they are btw

That's what I mean...

DesignersToolz
04-10-2004, 12:19 AM
You just need some if statements is all. Alot of if statements...

raf
04-10-2004, 12:28 AM
You just need some if statements is all. Alot of if statements...
Actualy, it only requies three if-statements or cases. as you can see here
http://www.php.net/strings where there are at least 4 usercomments with code to do exactly this. Like this one for stanance


/*from lille dot findus at telia dot com
posted at http://www.php.net/strings */
function nth($in)
{
if(strlen($in)>1 && substr($in,-2,1)==1)
return $in."th";
switch(substr($in,-1,1))
{
case 1:
return $in."st";
case 2:
return $in."nd";
case 3:
return $in."rd";
default:
return $in."th";
}
}

DesignersToolz
04-10-2004, 03:09 AM
Heh, you can always count on you to know what to do.

raf
04-10-2004, 09:14 AM
Heh, you can always count on you to know what to do.Well, every time i'm looking at a page in the php-manual, i read the usercomments, which contain examplecode or comments on about everyfeature you'll ever need. Great timesaver :D

gsnedders
04-10-2004, 07:12 PM
Actualy, it only requies three if-statements or cases. as you can see here
http://www.php.net/strings where there are at least 4 usercomments with code to do exactly this. Like this one for stanance


/*from lille dot findus at telia dot com
posted at http://www.php.net/strings */
function nth($in)
{
if(strlen($in)>1 && substr($in,-2,1)==1)
return $in."th";
switch(substr($in,-1,1))
{
case 1:
return $in."st";
case 2:
return $in."nd";
case 3:
return $in."rd";
default:
return $in."th";
}
}


Thanks raf, I thought it would be possible somehow like that, however, how do I put it on the end of the original code?

raf
04-11-2004, 12:22 PM
You first include the code for the function somewhere above your actual code. Yhen instead of just printing the value, you apply the function to it. So if it's the oldcount that you wouls like to have postfixed, you'd have


/*from lille dot findus at telia dot com
posted at http://www.php.net/strings */
function nth($in)
{
if(strlen($in)>1 && substr($in,-2,1)==1)
return $in."th";
switch(substr($in,-1,1))
{
case 1:
return $in."st";
case 2:
return $in."nd";
case 3:
return $in."rd";
default:
return $in."th";
}
}
//here can go whatever code you want inbetween
$getcount = fopen("count.txt", "r");
while (!feof($getcount))
{
$oldcount .= fgets($getcount);
}
fclose($getcount);
$oldcount++;
$storenew = fopen("count.txt", "w");
fputs($storenew, $oldcount);
fclose($storenew);

print nth($oldcount);

Note, you only need to include the functionscode once, and then you can use it like a build in function (just using nth($variable)

gsnedders
04-11-2004, 01:38 PM
Alright, but it now says


You are the 422ndth visitor to this site.

using
<?PHP
function nth($in)
{
if(strlen($in)>1 && substr($in,-2,1)==1)
return $in."th";
switch(substr($in,-1,1))
{
case 1:
return $in."st";
case 2:
return $in."nd";
case 3:
return $in."rd";
default:
return $in."th";
}
}
$getcount = fopen("count.txt", "r");
while (!feof($getcount))
{
$oldcount .= fgets($getcount);
}
fclose($getcount);
$oldcount++;
$storenew = fopen("count.txt", "w");
fputs($storenew, $oldcount);
fclose($storenew);

print nth($oldcount);
print nth($variable);
?>

gsnedders
04-11-2004, 01:46 PM
I didn't put those * in...

Fixed

raf
04-12-2004, 08:25 AM
where is

print nth($variable);

comming from ?

Try

print nth($oldcount);
print '<br />' . nth($variable);

to see what each line prints. I assume that print nth($variable); will only print 'th'

gsnedders
04-12-2004, 11:56 AM
Oh, my miss understanding, I thought we had to put
print nth($variable); on the end

gsnedders
04-12-2004, 09:40 PM
New error: it says
You are the
Warning: fclose(): supplied argument is not a valid stream resource in /home/geoffers/public_html/f1unleashed/templates/counterfooter.php on line 26
440th visitor to this site

My code is
You are the
<?PHP
function nth($in)
{
if(strlen($in)>1 && substr($in,-2,1)==1)
return $in."th";
switch(substr($in,-1,1))
{
case 1:
return $in."st";
case 2:
return $in."nd";
case 3:
return $in."rd";
default:
return $in."th";
}
}
$oldcount = file_get_contents("count.txt");
fclose($getcount);
$oldcount++;
$storenew = fopen("count.txt", "w");
fputs($storenew, $oldcount);
fclose($storenew);

print nth($oldcount);
?> visitor to this site.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum