Thread: Counter .dat

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Thumbs up Counter .dat Code Help


    Hi All
    This is my first post here, I'm hoping someone can help?

    I run a free online auction site

    I'm trying to get the code below to print 0 on the first
    .dat it enters into a folder called counters
    at the moment it prints count 1 sometimes it prints count 2
    can anyone edit the code so it prints a count of 0 on the first .dat


    Here's the full run-down of what the code should do
    if someone lists an item when you first submit the item
    it display's a count of 0 if you are logged in
    it should not add to the count on your item
    but if you look at other peoples items it counts your visit/s
    and if you are logged out it counts your visits to your own item/s as well as visits to other peoples listings
    Has anyone got any ides how to fix the below code ?
    if I can make the first count .dat print 0 it will sort out the problem.

    Many thanks for any help
    Best Regards
    Len


    #####################################################

    if (($usecounter eq "Yes") && ($loginalias eq "$firstbid[0]")) {

    open(READCOUNTER,"$config{'basepath'}$config{'countdir'}/$form{'item'}.dat");
    my $totalReads = <READCOUNTER>;
    chomp $totalReads;
    $totalReads;
    close(READCOUNTER);

    print "<table align='center' border='0' cellpadding='3' cellspacing='0' width='40%'>
    <tr>
    <td align='center'><font size='2' face='Tahoma' color='#003366'>";

    if ($totalReads < '1') {
    print "This Item has been viewed ( <b><font color='#FF0000'>0</font></b> ) times.";
    }
    else {
    if ($totalReads > '1') {
    print "This Item has been viewed ( <b><font color='#FF0000'>$totalReads</font></b> ) times.";
    }
    else {
    print "This Item has been viewed ( <b><font color='#FF0000'>$totalReads</font></b> ) time.";
    }
    }
    print "</font></td>
    </tr>
    </table>\n";
    }
    else {
    if ($usecounter eq "Yes") {

    open(READCOUNT,"$config{'basepath'}$config{'countdir'}/$form{'item'}.dat");
    my $totalReads = <READCOUNT>;
    chomp $totalReads;
    close(READCOUNT);

    $totalReads++;
    open(COUNT,">$config{'basepath'}$config{'countdir'}/$form{'item'}.dat");
    if ($config{'flock'}) {
    flock(COUNT, 2);
    seek(COUNT, 0, 2);
    }
    print (COUNT "$totalReads\n");
    flock(COUNT, 8);
    close(COUNT);

    print "<table align='center' border='0' cellpadding='3' cellspacing='0' width='40%'>
    <tr>
    <td align='center'><font size='2' face='Tahoma' color='#003366'>";

    if ($totalReads > '1') {
    print "This Item has been viewed ( <b><font color='#FF0000'>$totalReads</font></b> ) times.";
    }
    else {
    print "This Item has been viewed ( <b><font color='#FF0000'>$totalReads</font></b> ) time.";
    }
    print "</font></td>
    </tr>
    </table>\n";
    }
    }

    ####################################################




    If someone looks at a listing via
    a page that has the code below it adds a count
    to the listing.




    #-############# code for view count

    my $totalReads;

    if ($usecounter eq "Yes") {
    open(count, "$config{'basepath'}$config{'countdir'}/$file.dat");
    $totalReads = <count>;
    $totalReads;
    close(count);

    $totalReads = "<font face='arial' size='-1'>0</font>"
    if ($totalReads <= 0);
    $totalReads = "<font face='arial' size='-1' color='cc0000'>$totalReads</font>"
    if ($totalReads >= 50);
    $totalReads = "<font face='arial' size='-1' color='000080'>$totalReads</font>"
    if ($totalReads <= 49);
    }
    else {
    if ($usecounter eq "No") {
    $totalReads = "<font face='arial' size='-1'>nc</font>";
    }
    }

    #-############# end of view counter
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    Use the File::CounterFile module to maintain the counter instead of rolling your own.

    You should also use css instead of those depreciated html formatting tags.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    counter


    Hi FishMonger
    Many thanks for your help
    it's much appreciated
    what I'm really wanting is the same code edited
    so it adds a count.dat with 0 on it.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,875
    Rep Power
    1225
    Here's a rewrite of the beginning portion of your code. which could be cleaned up a little more than I have done.

    Code:
    use File::CounterFile;
    
    my $counter_file = "$config{'basepath'}$config{'countdir'}/$form{'item'}.dat";
    
    # counter value defaults to 0 if file does not already exist.
    my $counter      = File::CounterFile->new( $counter_file, 0 );
    my $totalReads   = $counter->value;
    
    if ( ( $usecounter eq "Yes" ) && ( $loginalias eq "$firstbid[0]" ) ) {
    
        print "<table align='center' border='0' cellpadding='3' cellspacing='0' width='40%'>
    <tr>
    <td align='center'><font size='2' face='Tahoma' color='#003366'>";
    
        my $times = $totalReads == 1 ? 'time' : 'times';
    
        print "This Item has been viewed ( <b><font color='#FF0000'>$totalReads</font></b> ) $times.";
        print "</font></td>
    </tr>
    </table>\n";
    }
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    counter


    Originally Posted by FishMonger
    Here's a rewrite of the beginning portion of your code. which could be cleaned up a little more than I have done.

    Code:
    use File::CounterFile;
    
    my $counter_file = "$config{'basepath'}$config{'countdir'}/$form{'item'}.dat";
    
    # counter value defaults to 0 if file does not already exist.
    my $counter      = File::CounterFile->new( $counter_file, 0 );
    my $totalReads   = $counter->value;
    
    if ( ( $usecounter eq "Yes" ) && ( $loginalias eq "$firstbid[0]" ) ) {
    
        print "<table align='center' border='0' cellpadding='3' cellspacing='0' width='40%'>
    <tr>
    <td align='center'><font size='2' face='Tahoma' color='#003366'>";
    
        my $times = $totalReads == 1 ? 'time' : 'times';
    
        print "This Item has been viewed ( <b><font color='#FF0000'>$totalReads</font></b> ) $times.";
        print "</font></td>
    </tr>
    </table>\n";
    
    
    
    }
    Hi FishMonger
    Many thanks for the code I'll give it a try.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Thumbs up Count Code


    Hi
    I managed to sort my problem with the below code.
    many thanks fishmonger for your help
    Best Regards
    Len


    #####################################################

    if (($usecounter eq "Yes") && ($loginalias eq "$firstbid[0]")) {

    my $totalReads = 0;
    open(READCOUNT,"$config{'basepath'}$config{'countdir'}/$form{'item'}.dat");
    $totalReads = <READCOUNT>;
    close(READCOUNT);

    print "<table align='center' border='0' cellpadding='3' cellspacing='0' width='40%'>
    <tr>
    <td align='center'><font size='2' face='Tahoma' color='#003366'>";

    if ($totalReads < '1') {
    print "This Item has been viewed ( <b><font color='#FF0000'>0</font></b> ) times.";
    }
    else {
    if ($totalReads > '1') {
    print "This Item has been viewed ( <b><font color='#FF0000'>$totalReads</font></b> ) times.";
    }
    else {
    print "This Item has been viewed ( <b><font color='#FF0000'>$totalReads</font></b> ) time.";
    }
    }
    print "</font></td>
    </tr>
    </table>\n";
    }
    else {
    if ($usecounter eq "Yes") {

    my $totalReads = 0;
    open(READCOUNT,"$config{'basepath'}$config{'countdir'}/$form{'item'}.dat");
    $totalReads = <READCOUNT>;
    close(READCOUNT);

    ++$totalReads;
    open(COUNT,">$config{'basepath'}$config{'countdir'}/$form{'item'}.dat");
    if ($config{'flock'}) {
    flock(COUNT, 2);
    seek(COUNT, 0, 2);
    }
    print (COUNT "$totalReads\n");

    print "<table align='center' border='0' cellpadding='3' cellspacing='0' width='40%'>
    <tr>
    <td align='center'><font size='2' face='Tahoma' color='#003366'>";

    if ($totalReads > '1') {
    print "This Item has been viewed ( <b><font color='#FF0000'>$totalReads</font></b> ) times.";
    }
    else {
    print "This Item has been viewed ( <b><font color='#FF0000'>$totalReads</font></b> ) time.";
    }
    print "</font></td>
    </tr>
    </table>\n";
    }
    }

    ####################################################

IMN logo majestic logo threadwatch logo seochat tools logo