PHP ViewNumber COUNT HELP! I will pay $20 if I get real fix!

Discussion in 'PHP' started by n00bl3t, Sep 10, 2011.

  1. #1
    Hello,

    I have this PHP code:

    $sql = "UPDATE video SET viewnumber=viewnumber+1, viewtime='" .date('Y-m-d H:i:s'). "' WHERE VID = '" .$VID. "' LIMIT 1";
    $conn->execute($sql);

    Currently, when the page is visited the view count gets added +1 and so on.

    I want that view count to be as little as possible. Let's say 4 visits = 1 view count.

    Let's say 4 people visited http://blahblah.com/videoblablah.html = the counter will only count 1view on the video.

    Is it okay to put it like this:

    viewnumber=viewnumber+0.25
    so the count of 1 is divided into 4? so 4 views will be 1view only?

    or just divide total view counts by 4?
     
    n00bl3t, Sep 10, 2011 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    If the field is a real number, you can do it that way. If it's an integer the count will never increase. If this is a MySQL database, use phpMyAdmin to go to it, look at the structure of the table 'video' and make sure that 'viewnumber' is a field that can take a fraction. Then make sure that the rest of the code doesn't blow up. (Always take a full backup of the dsatabase before playing with it.)
     
    Rukbat, Sep 10, 2011 IP
  3. n00bl3t

    n00bl3t Well-Known Member

    Messages:
    383
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    120
    #3
    Hi Rukbat,

    This is a MySQL DB and I have this:

    viewnumber bigint(10)

    on table video..

    Is this okay to put. +0.25 ?
     
    n00bl3t, Sep 10, 2011 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    You can, but since 0.25 isn't an integer, you'll be adding 0. Change the field to number/real (after you back up at least that table, in case you lose data when you make the change).
     
    Rukbat, Sep 10, 2011 IP
  5. n00bl3t

    n00bl3t Well-Known Member

    Messages:
    383
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    120
    #5
    what do you mean real number?

    Like this:

    $sql = "UPDATE video SET viewnumber=viewnumber-1, viewtime='" .date('Y-m-d H:i:s'). "' WHERE VID = '" .$VID. "' LIMIT 1";
    $conn->execute($sql);



    sorry im really confused.
     
    n00bl3t, Sep 10, 2011 IP
  6. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #6
    Just divide by 4. It's much easier.

    The other suggestion was to change the Field to a Float/Real in the database structure.
     
    shallowink, Sep 10, 2011 IP
  7. n00bl3t

    n00bl3t Well-Known Member

    Messages:
    383
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    120
    #7
    Hi shallowink,

    Do you have any idea how to divide by 4?
     
    n00bl3t, Sep 10, 2011 IP
  8. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #8
    um, would depend on the code you use to extract data. That is, do it when you show the number of views and leave the DB alone.
    So it would be $view_cnt = $viewcount / 4;
     
    shallowink, Sep 10, 2011 IP
  9. n00bl3t

    n00bl3t Well-Known Member

    Messages:
    383
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    120
    #9
    Hi,

    will that add +0.25?

    I think you meant:

    UPDATE video SET viewnumber=viewnumber+1/4 right?

    or just directly UPDATE video SET viewnumber=viewnumber/4 ?

    Here is the original code:

    $sql = "UPDATE video SET viewnumber=viewnumber+1, viewtime='" .date('Y-m-d H:i:s'). "' WHERE VID = '" .$VID. "' LIMIT 1";
    $conn->execute($sql);
     
    n00bl3t, Sep 10, 2011 IP
  10. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #10
    No, its not what I meant. Add 1 for each view. Go find the code where you view the count and change that so its divided by 4. If you want to add .25, you will have to go into phpmyadmin and change the viewnumber field to a Real or Float. Then you can modify the insertion code but odds are if you do that, you will have to find the code to display the viewcount and modify it to round the result. So just modify the code used to display the result.
     
    shallowink, Sep 10, 2011 IP
  11. n00bl3t

    n00bl3t Well-Known Member

    Messages:
    383
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    120
    #11
    I think I will have to go with your solution shallowink.

    Below is the code:
    Would you mind helping create that +1 viewcount and then divide by 4 to be in accordance to your suggestion?
     
    n00bl3t, Sep 10, 2011 IP
  12. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #12
    The type of the field in the database that holds the data. Change that from the int field it is now to a number field of type Real.

    The problem with adding 1 and dividing by 4 is that your first view will show 0.25 views. You could write code to round the view up to the next lowest integer, so 0, 0.25, 0.5 and 0.75 would all show 0, 1, 1.25, 1.5 and 1.75 would all show 1, etc. But it's a lot easier just changing the database field to a real number, then displaying the integer of that number [use intval()].
     
    Rukbat, Sep 10, 2011 IP
  13. hotnoob

    hotnoob Member

    Messages:
    96
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    28
    #13
    if you are going to be adding +0.25 to the view count, you will need to change the structure type in the database, from an integer to a float, otherwise you view count won't change, because when you add the 0.25 it will be rounded down to 0.

    as for your reasons for reducing the view count, i am truly confused by it.
     
    hotnoob, Sep 11, 2011 IP
  14. n00bl3t

    n00bl3t Well-Known Member

    Messages:
    383
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    120
    #14
    It works! Except for the fact that it is showing a decimal on the view count.

    Views: 173.5

    How do you make that only appear wholenumber and no decimal?

    I only want to alter how the count per view is adding up, but update the view count on frontend when it is a wholenumber and note decimal?
     
    n00bl3t, Sep 11, 2011 IP
  15. hotnoob

    hotnoob Member

    Messages:
    96
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    28
    #15
    you need to apply the round function to every place where you are displaying the number.
     
    hotnoob, Sep 11, 2011 IP
  16. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #16
    You have 2 options,

    
    ceil($number); #Rounds off to highest
    floor($number); #Rounds off to lowest
    
    PHP:
    Glen
     
    HuggyEssex, Sep 12, 2011 IP
  17. Pwnography

    Pwnography Member

    Messages:
    58
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #17
    I am suprised no one just added a new column to the field and added +1, when the value reached = to the value of the split view counter you would then reset to 0 and +1 to the actual view counter.
     
    Pwnography, Sep 13, 2011 IP