HenryJohan Posted November 26, 2006 Share Posted November 26, 2006 Im not that old in php programing but i still tryed the challange of making a simple voting script. My problem is that the values i have vritten to the text file which is storing the submitted votes doesnt seem to work in an operator.code olddata.txt5 + 5 + 5 + 10 + 8submitvote.php $optionboxvalue = $_POST['optionbox'];//Add submitted value and add a "+" before.$myfile = "olddata.txt";$fh = fopen($myfile, 'a') or die ("CAn't open file");$stringdata =" + ";fwrite($fh, $stringdata);$stringdata = "$optionboxvalue";fwrite($fh, $stringdata);$myfile = "olddata.txt";$fh = fopen($myfile, 'r');$thedata = fread($fh, 99999);//Now the operation where we sum up the olddata.txt values and divede them and then echo ´them on the page. $division = $thedata / 2;echo "$division";The result is that it echos only the first number in "olddata.txt" and divide it by two. I want it to sum up all the values in "olddata.txt" and then divide.Did i miss somethin' in the operators class ??? Link to comment https://forums.phpfreaks.com/topic/28550-what-to-do-ami-a-newbie-perhaps-voting-script-php/ Share on other sites More sharing options...
genericnumber1 Posted November 26, 2006 Share Posted November 26, 2006 When you fread() the values and you divide by 2 you aren't actually evaluating "5 + 5 + 5 + 10 + 8" then dividing by two.What you're doing is saying "5 + 5 + 5 + 10 + 8" (the string) / 2since it's a string php is doing the next best thing it can do and turning it into an int, and I'm sure it's deciding 5 = (int)"5 + 5 + 5 + 10 + 8" The way you COULD do it, and I'm sure there is a better way is...[code]<?php// Get the file contents, you may want to refer to it absolutely "/home/htdocs/text.txt" or the like$file_contents = file_get_contents('test.txt');// Split the numbers into an array$num_array = explode(' + ', $file_contents);// Start counting at 0$sum = 0;// Loop through the values, adding them to $sumfor($i = 0; $i < count($num_array); ++$i) { $sum += (int)$num_array[$i]; // I like to typecast things... it's optional}// Finish with whatever operation you would likeecho $sum / 2;?>[/code]Hope this helped! Link to comment https://forums.phpfreaks.com/topic/28550-what-to-do-ami-a-newbie-perhaps-voting-script-php/#findComment-130650 Share on other sites More sharing options...
HenryJohan Posted November 26, 2006 Author Share Posted November 26, 2006 Thanx alot ;D it works. Hmm but what if I would want to change the "2" and divede by the number of votes so i can get some kind of reall average value ??? That will be my next projekt, any suggestions. Link to comment https://forums.phpfreaks.com/topic/28550-what-to-do-ami-a-newbie-perhaps-voting-script-php/#findComment-130690 Share on other sites More sharing options...
genericnumber1 Posted November 26, 2006 Share Posted November 26, 2006 changingecho $sum / 2;toecho $sum / count($num_array);divides by the number of integers in the file if that's what you wanted and will get the average of the numbers in the file though you might wanna make a precaution against a repeating decimal with round() Link to comment https://forums.phpfreaks.com/topic/28550-what-to-do-ami-a-newbie-perhaps-voting-script-php/#findComment-130694 Share on other sites More sharing options...
HenryJohan Posted November 26, 2006 Author Share Posted November 26, 2006 Wow quic reply ;D But the issue is that since its a voting script it should ideally when a user posts a new value in the olddata.txt the "divide - with - value" also should increase by one to allways get the sum of values be divided by the number of values inserted. I think im gonna create an other textfile to store the number of votes and then read it into the submitvote.php like the oldtext.php. But if someone has a better suggestion i'm all ears. 8) Link to comment https://forums.phpfreaks.com/topic/28550-what-to-do-ami-a-newbie-perhaps-voting-script-php/#findComment-130703 Share on other sites More sharing options...
genericnumber1 Posted November 26, 2006 Share Posted November 26, 2006 Show me what you have so far and I'll see if I can help yah ;D Link to comment https://forums.phpfreaks.com/topic/28550-what-to-do-ami-a-newbie-perhaps-voting-script-php/#findComment-130709 Share on other sites More sharing options...
HenryJohan Posted November 26, 2006 Author Share Posted November 26, 2006 This is voting.php the original file (where the voting takes place) now im looking for a way of posting along a second value that can be stored in a different textfile to be added along with the vote alues.<form method="post" action="submitvote.php"><table border="0"><tr><td><input type="radio" name="optionbox" value="1" onclick="this.form.submit()"></td><td><input type="radio" name="optionbox" value="2" onclick="this.form.submit()"></td><td><input type="radio" name="optionbox" value="3" onclick="this.form.submit()"></td><td><input type="radio" name="optionbox" value="4" onclick="this.form.submit()"></td><td><input type="radio" name="optionbox" value="5" onclick="this.form.submit()"></td><td><input type="radio" name="optionbox" value="6" onclick="this.form.submit()"></td><td><input type="radio" name="optionbox" value="7" onclick="this.form.submit()"></td><td><input type="radio" name="optionbox" value="8" onclick="this.form.submit()"></td><td><input type="radio" name="optionbox" value="9" onclick="this.form.submit()"></td><td><input type="radio" name="optionbox" value="10" onclick="this.form.submit()"><br>[color=red]<input type="hidden" name="submit_add" value="1"> ---ryed adding this but it doesnt seem to be posted along with the radibuttin value when submitted ???[/color]</tr><tr><td align="center">1</td><td align="center">2</td><td align="center">3</td><td align="center">4</td><td align="center">5</td><td align="center">6</td><td align="center">7</td><td align="center">8</td><td align="center">9</td><td align="center">10</td></tr></table> </form> Link to comment https://forums.phpfreaks.com/topic/28550-what-to-do-ami-a-newbie-perhaps-voting-script-php/#findComment-130719 Share on other sites More sharing options...
HenryJohan Posted November 26, 2006 Author Share Posted November 26, 2006 and the writing to the second textfile would go like this(this is added to submitvote.php//Numbers of values$add_number = $_POST['submit_add'];//Append to file$myfile = "number_of_votes.txt";$fh = fopen($myfile, 'a') or die("can't open file");$stringdata = " + ";fwrite($fh, $stringdata);$stringdata = "$add_number";fwrite($fh, $stringdata); Link to comment https://forums.phpfreaks.com/topic/28550-what-to-do-ami-a-newbie-perhaps-voting-script-php/#findComment-130720 Share on other sites More sharing options...
HenryJohan Posted November 26, 2006 Author Share Posted November 26, 2006 And ofcourse changein the $sum to $sum2 so there are two different strings, This works great now , :D Thans for the help , just wondering if theres some way to limit the echoed value to only have like four digits ,? Link to comment https://forums.phpfreaks.com/topic/28550-what-to-do-ami-a-newbie-perhaps-voting-script-php/#findComment-130724 Share on other sites More sharing options...
genericnumber1 Posted November 26, 2006 Share Posted November 26, 2006 [quote author=HenryJohan link=topic=116386.msg474245#msg474245 date=1164583206]just wondering if theres some way to limit the echoed value to only have like four digits ,?[/quote]round() -- http://www.php.net/roundExample...[code=php:0]echo round(4.5346434756, 3); // echos 4.535[/code] Link to comment https://forums.phpfreaks.com/topic/28550-what-to-do-ami-a-newbie-perhaps-voting-script-php/#findComment-130727 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.