Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 20

Thread: php counter....

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    16
    Member #
    31024
    I have roughly 35 webpages in my website, in each page is a form that is specific to that page, once submitted it tallies up on a counter on that same page through php and a .dat file but what I would like to do is write a code that can take all the counters and add them into one total on a counter on the home page. I have no idea how to write that. Can anyone help me or lead me to a tutorial? Thanks

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    So I assume you have 35 .dat files?
    You could open each one, read the contents, and add them up ... that's sort of time consuming.

    Does the total counter on the home page need to be permanent, or only for the user
    while they are visiting your site?

    If it's based on the user visiting, in that one session, you can use PHP SESSION to add the
    counters as they submit the pages. SESSION is like COOKIE, except it's on the server.


  4. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    16
    Member #
    31024
    Quote Originally Posted by mlseim, post: 231509
    So I assume you have 35 .dat files?
    You could open each one, read the contents, and add them up ... that's sort of time consuming.

    Does the total counter on the home page need to be permanent, or only for the user
    while they are visiting your site?

    If it's based on the user visiting, in that one session, you can use PHP SESSION to add the
    counters as they submit the pages. SESSION is like COOKIE, except it's on the server.
    I would like for it to be permanent so that it is visible to everyone all the time.

  5. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    16
    Member #
    31024
    Quote Originally Posted by csclark0530, post: 231513
    I would like for it to be permanent so that it is visible to everyone all the time.
    and yes, I do have multiple .dat files, and I honestly wouldn't have a problem adding everything up but if there is another way I would be happier

  6. #5
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    Are all the .dat files in one directory?

    And if your main page is not in the same directory as the .dat files, what is the .dat file directory name called?

    And provide an example of the contents in the .dat file.
    Is it one line, one number? Show us that.


  7. #6
    Junior Member
    Join Date
    Mar 2012
    Posts
    16
    Member #
    31024
    all the .dat files are in the same directory as well as the homepage. all my .dat files are single line numbers. ex: 0 is the starting number in all of them.

  8. #7
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    Save the script below as "count.php".
    Test it out by running it all by itself.
    If it works, put an include statement in your home page where it should display.
    <?php include("count.php");?>

    PHP Code:
    <?php

    // put all.dat files into an array.
    $files array_merge(
    glob("*.dat")
    );
    $total=0;
    // loop through them to get a total
    foreach($files as $file){
    $number=file($file);
    $total $total $number[0];
    }
    echo 
    "Total: $total";

    ?>


  9. #8
    Junior Member
    Join Date
    Mar 2012
    Posts
    16
    Member #
    31024
    Thanks, it works but it counts up in increments of 10, ex: "010" "020" "030" etc. ....any ideas?

    Also, with the counters I have made they seem to start at 1 instead of zero, any thoughts on that? here is the code
    <?php
    $cf = "counter.dat";
    $fp = fopen($cf,"r");
    $ct = trim(fread($fp,filesize($cf)));
    if ($ct != "") $ct++; else
    $ct = 0;
    @fclose($fp);
    echo "Total Pledges: ".$ct;
    ?>

  10. #9
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    Attach a few (like 3) of actual .dat files to a post.
    zip them if the editor won't let you attach .dat files


  11. #10
    Junior Member
    Join Date
    Mar 2012
    Posts
    16
    Member #
    31024
    Here you go, they are just simple .dat files though. Thanks for the help


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 04:42 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com