Jump to content

What to do? AmI a newbie perhaps ? Voting script , php


HenryJohan

Recommended Posts

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

5 + 5 + 5 + 10 + 8

submitvote.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  ???
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) / 2
since 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 $sum
for($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 like
echo $sum / 2;
?>
[/code]

Hope this helped!
changing
echo $sum / 2;

to
echo $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()
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)
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>
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);
[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/round
Example...
[code=php:0]
echo round(4.5346434756, 3); // echos 4.535
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.