/    Sign up×
Community /Pin to ProfileBookmark

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.

[code=php]<?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;
}
}
?>[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 27.2012 — 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:
[code=php]
$data = file_get_contents($file);
preg_match_all('/:s*(d+)s*(r?n|$)/', $data, $matches);
$totalVotes = array_sum($matches[1]);
echo $totalVotes;
[/code]
Copy linkTweet thisAlerts:
@LeishauthorOct 27.2012 — 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. ?
×

Success!

Help @Leish spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...