www.webdeveloper.com
Results 1 to 3 of 3

Thread: Simple question about adding up numbers in a text file.

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    2

    Simple question about adding up numbers in a text file.

    Hi,

    I'm new to php (though discovering it's similar to actionscript, which I like), and I've trawled the web looking for answers to my problem. Every time I find an answer that might apply to my situation, I try it and the code doesn't word - nothing shows up on my page. I know it's a really simple piece of code that I need, but I haven't learned enough php to discover it yet.

    I know that \d stands for "digits", but I can't work out how to apply it to my code...

    So here's an explanation of my problem. I've got a poll on my home page, which is basically a .txt file that repeats a line of various-length text (the poll answers to pick from), a colon, and then a number (which is the number of clicks each poll answer has). Rough example of one line:

    XX XXXXX X XXXXX X XXXXXXXXXX XXXXX:6

    Now I need to have some php that adds up the number of clicks every poll answer has, then displays it as a total. I've got the code that tells php to get and read the text file ("polldata.txt"), and it works fine, but the part below that says "WHAT_GOES_HERE_SO_PHP_WILL_ADD_UP_MY_NUMBERS?" needs fixed. The segment of code below is part of a larger script, though this section basically displays the results of the poll in a table once a user clicks on "View Stats", or adds the user's vote to the text file once the user selects an answer and votes (all of that script works fine).

    Can anyone help? Thank you in advance.



    PHP Code:
    <?php    
    } else {
          if ((isset(
    $_POST['submitBtn'])) && (!isset($_POST['statsBtn']))){
              
    $count 0;
           foreach (
    $answers as $value) {
            if (
    $value['text']  == $_POST['polling']) {
                
    $answers[$count]['count'] = ((int)$value['count'])+1;
                (int)
    $totalCount++;
            }
            ++
    $count;
           }
               
        
    writeData();
        echo 
    nl2br(".\nThanks for your vote!\n.");
        echo 
    '   <table width="400">';
        foreach (
    $answers as $value) {
            echo 
    '<tr><td> '.$value['text'].'</td><td>'.$value['count'].'</td></tr>';
        }
        echo 
    '  </table>';
    }else {
        if ((isset(
    $_POST['statsBtn'])) && (!isset($_POST['submitBtn']))) {
          
    writeData();
        echo 
    nl2br(".\nCurrent Poll Stats:\n.");
        echo 
    '   <table width="400">';
        foreach (
    $answers as $value) {
            echo 
    '<tr><td> '.$value['text'].'</td><td>'.$value['count'].'</td></tr>';
        }
        echo 
    '  </table>';
        }
    $numbers preg_match_all("WHAT_GOES_HERE_SO_PHP_WILL_ADD_UP_MY_NUMBERS?"$contents);
    $total 0;
    foreach (
    $numbers as $number) {
        
    $total += $number;
    }
    echo 
    $total;
    }
    }
    ?>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,224
    Personally, I think it would be cleaner, more robust, and more extendable if you stored the data in a database (even SQLite if you don't want to deal with a separately installed RDBMS). But if you really want to do it this way:
    PHP Code:
    $data file_get_contents($file);
    preg_match_all('/:\s*(\d+)\s*(\r?\n|$)/'$data$matches);
    $totalVotes array_sum($matches[1]);
    echo 
    $totalVotes
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Oct 2012
    Posts
    2
    Yay! NogDog, thank you! I added my .txt file name in, and bingo! You've saved me hours of trying various combinations of code.

    Oh, and I tried setting up my poll using a database - I even created a MySQL DB - but then I got completely stuck with what to do and so found a method online that showed how to make a poll using a text file... which was far easier and worked. And then I tweaked it a little to add extra buttons and stuff.


    Thanks!!! This site is wonderful. You've made my day.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles