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 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Echo different messages based on random visitor

    Hello,

    I would like to combine a few different PHP scripts to create one solution...maybe you guys can help.

    I want to start out with a counter script, to count the visits on a single page. Now, instead of "echoing/displaying" a number, I would like it to echo/display a snippet of text based on the number/visitor...

    I already have a code to count the visits...it is a "write to" a text file.

    But what I want to do is to use the "$fgets" to read the last character/number in the text file. If that number is a five (5) or a zero (0), then I would like to display/echo a message, if it isn't either a 5 or 0, then I would like to display/echo a different message.

    So for example: If a visitor lands on the page, and the counter senses that he is visitor number 235, then it will display a message like "great for visiting", but when the next person lands on the page, and is number 236, then a message that will be diplayed will be "thank you."

    I have all of the code, such as the counter and the if/then, but I am having trouble with reading the last number/character of the text file. Because the text file will always expand, from single digit for number 1-9 up to say 5 digits for the 10,000 - 99,999, how can I create a script to read the last digit...

    Thanks.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Think mathematically. You are really asking for all multiples of 5. If you divide the hit count by 5, then a count that is a multiple of 5 will have a remainder of 0. If it's not a multiple of 5, the remainder will be something other than 0.

    The way to test for this in PHP is using the modulus operator (a % sign):

    PHP Code:
    $remainder $count 5;
    if (
    $remainder == 0) {
        echo 
    "yo";
    } else {
        echo 
    "not yo";


  • #3
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    After you get the number with fgets...
    PHP Code:
    $lastdigit substr($totalhits, -1); 

  • #4
    New Coder
    Join Date
    Aug 2007
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    It worked...but can you help me with something else

    I used "fumigators" solution...it works great. It will also work with other numbers, which is perfect as well.

    One other thing is instead of "echo "yo"" is there a way that I could pop open a new window?

    Thanks again.


    Michael

  • #5
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts
    echo the javascript command you need to pop open a new window...


  •  

    Posting Permissions

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