If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Hybrid View

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).

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:

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.