Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15

Thread: Counter

  1. #1
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts

    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.

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    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)

  • #3
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    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

  • #4
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    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...

  • #5
    Regular Coder
    Join Date
    Mar 2004
    Location
    Jackson, Georgia
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    Regular Coder
    Join Date
    Mar 2004
    Location
    Jackson, Georgia
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #8
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #9
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    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?

  • #10
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #11
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Alright, but it now says

    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);
    ?>
    Last edited by gsnedders; 04-11-2004 at 12:52 PM.

  • #12
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    I didn't put those * in...

    Fixed
    Last edited by gsnedders; 04-11-2004 at 12:50 PM.

  • #13
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #14
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Oh, my miss understanding, I thought we had to put
    PHP Code:
    print nth($variable); 
    on the end

  • #15
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    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
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •