Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-07-2004, 11:44 PM   PM User | #1
gsnedders
Senior Coder

 
gsnedders's Avatar
 
Join Date: Jan 2004
Posts: 2,340
Thanks: 1
Thanked 7 Times in 7 Posts
gsnedders will become famous soon enough
Counter

Quote:
Originally Posted by DesignersToolz
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.


Code:
<?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.
__________________
Geoffrey Sneddon
gsnedders is offline   Reply With Quote
Old 04-08-2004, 04:47 PM   PM User | #2
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,890
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
Quote:
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 ?
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 04-08-2004, 04:51 PM   PM User | #3
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
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
__________________
Blue Panda
Website Design | 1 Pound Ads | 'ow much? | Coding Geeks
Nightfire is offline   Reply With Quote
Old 04-08-2004, 05:09 PM   PM User | #4
gsnedders
Senior Coder

 
gsnedders's Avatar
 
Join Date: Jan 2004
Posts: 2,340
Thanks: 1
Thanked 7 Times in 7 Posts
gsnedders will become famous soon enough
Quote:
Originally Posted by Nightfire
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...
__________________
Geoffrey Sneddon
gsnedders is offline   Reply With Quote
Old 04-09-2004, 11:19 PM   PM User | #5
DesignersToolz
Regular Coder

 
Join Date: Mar 2004
Location: Jackson, Georgia
Posts: 102
Thanks: 0
Thanked 0 Times in 0 Posts
DesignersToolz is an unknown quantity at this point
You just need some if statements is all. Alot of if statements...
__________________
Designer's Toolz; Web & software development community.

Get your coding questions answered:
DTZ Forums
DesignersToolz is offline   Reply With Quote
Old 04-09-2004, 11:28 PM   PM User | #6
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
Quote:
Originally Posted by DesignersToolz
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
PHP Code:
/*from lille dot findus at telia dot com
posted at [url]http://www.php.net/strings[/url] */
function nth($in
{
   if(
strlen($in)>&& 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";
   }

__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 04-10-2004, 02:09 AM   PM User | #7
DesignersToolz
Regular Coder

 
Join Date: Mar 2004
Location: Jackson, Georgia
Posts: 102
Thanks: 0
Thanked 0 Times in 0 Posts
DesignersToolz is an unknown quantity at this point
Heh, you can always count on you to know what to do.
__________________
Designer's Toolz; Web & software development community.

Get your coding questions answered:
DTZ Forums
DesignersToolz is offline   Reply With Quote
Old 04-10-2004, 08:14 AM   PM User | #8
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
Quote:
Originally Posted by DesignersToolz
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
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 04-10-2004, 06:12 PM   PM User | #9
gsnedders
Senior Coder

 
gsnedders's Avatar
 
Join Date: Jan 2004
Posts: 2,340
Thanks: 1
Thanked 7 Times in 7 Posts
gsnedders will become famous soon enough
Quote:
Originally Posted by raf
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
PHP Code:
/*from lille dot findus at telia dot com
posted at [url]http://www.php.net/strings[/url] */
function nth($in
{
   if(
strlen($in)>&& 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?
__________________
Geoffrey Sneddon
gsnedders is offline   Reply With Quote
Old 04-11-2004, 11:22 AM   PM User | #10
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
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
PHP Code:
/*from lille dot findus at telia dot com 
posted at [url]http://www.php.net/strings[/url] */ 
function nth($in)  

   if(
strlen($in)>&& 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)
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 04-11-2004, 12:38 PM   PM User | #11
gsnedders
Senior Coder

 
gsnedders's Avatar
 
Join Date: Jan 2004
Posts: 2,340
Thanks: 1
Thanked 7 Times in 7 Posts
gsnedders will become famous soon enough
Alright, but it now says

Quote:
You are the 422ndth visitor to this site.
using
PHP Code:
<?PHP
function nth($in)   
{  
   if(
strlen($in)>&& 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);
?>
__________________
Geoffrey Sneddon

Last edited by gsnedders; 04-11-2004 at 12:52 PM..
gsnedders is offline   Reply With Quote
Old 04-11-2004, 12:46 PM   PM User | #12
gsnedders
Senior Coder

 
gsnedders's Avatar
 
Join Date: Jan 2004
Posts: 2,340
Thanks: 1
Thanked 7 Times in 7 Posts
gsnedders will become famous soon enough
I didn't put those * in...

Fixed
__________________
Geoffrey Sneddon

Last edited by gsnedders; 04-11-2004 at 12:50 PM..
gsnedders is offline   Reply With Quote
Old 04-12-2004, 07:25 AM   PM User | #13
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf will become famous soon enoughraf will become famous soon enough
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'
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 04-12-2004, 10:56 AM   PM User | #14
gsnedders
Senior Coder

 
gsnedders's Avatar
 
Join Date: Jan 2004
Posts: 2,340
Thanks: 1
Thanked 7 Times in 7 Posts
gsnedders will become famous soon enough
Oh, my miss understanding, I thought we had to put
PHP Code:
print nth($variable); 
on the end
__________________
Geoffrey Sneddon
gsnedders is offline   Reply With Quote
Old 04-12-2004, 08:40 PM   PM User | #15
gsnedders
Senior Coder

 
gsnedders's Avatar
 
Join Date: Jan 2004
Posts: 2,340
Thanks: 1
Thanked 7 Times in 7 Posts
gsnedders will become famous soon enough
New error: it says
Quote:
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
PHP Code:
You are the 
<?PHP
function nth($in)   
{  
   if(
strlen($in)>&& 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.
__________________
Geoffrey Sneddon
gsnedders is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:14 PM.


Advertisement
Log in to turn off these ads.