Survey in PHP not saving votes correctly to txt file

124 views Asked by At

My problem is that when I vote for for example last option in survey the .txt file with survey answers gets empty row which ruins my graph which is generated after voting.

If you want you can check the site here

My .txt file looks like this, each row is each pool position.

0
0
0
0
0

My index.php

<html>
    <head>
        <title>Ankieta</title>
        <meta charset="utf-8">
    </head>
    <body>
        Oceń poziom nauczania na swojej uczelni:
        <form action="send.php" method="GET" >
            <input type=radio name=1 value="1" checked>2<br>
            <input type=radio name=1 value="2">3<br>
            <input type=radio name=1 value="3">4<br>
            <input type=radio name=1 value="4">5<br>
            <input type=radio name=1 value="5">Nie wiem<br>
            <input type=submit value="Wyślij"/>
        </form>
    </body>
</html>

My send.php I think something might be wrong here.

<html>
    <head>
        <title>Ankieta</title>
        <meta charset="utf-8">
    </head>
    <body>
    <?php 
    if(!empty($_COOKIE['glos'])) {
        echo "Głos już oddano! Spróbuj za 40 sekund.";
    } else {
        echo " Dziękuję za głos! Zagłosowano na: ";
        $wyniki=file("wyniki.txt");
        switch($_GET['1']) {
            case "1":
                $wyniki[0]=$wyniki[0]+1;
                echo "2";
            break;
            case "2":
                $wyniki[1]=$wyniki[1]+1;
                echo "3";
            break;
            case "3":
                $wyniki[2]=$wyniki[2]+1;
                echo "4";
            break;
            case "4":
                $wyniki[3]=$wyniki[3]+1;
                echo "5";
            break;
            case "5":
                $wyniki[4]=$wyniki[4]+1;
                echo "Nie wiem";
            break;
        }
        $koncowo=$wyniki[0]."\n".$wyniki[1].$wyniki[2].$wyniki[3].$wyniki[4];
        file_put_contents('wyniki.txt',$koncowo);
    }
    setcookie("glos", 1, time()+40);
    ?>
    <BR>
    <form action="http://nwwnd.cba.pl/ankieta/wyniki.php">
        <input type="submit" value="Wyniki" />
    </form>
    </body>
</html>

My wynik.php file

<? 
$wysokosc = 600; 
$szerokosc = 700; 
$graph = ImageCreate($szerokosc, $wysokosc); 
$bialy = ImageColorAllocate($graph,255,255,255); 
$czarny = ImageColorAllocate($graph,0,0,0); 
$rozowy = ImageColorAllocate($graph,255,233,241);
$czerwony = ImageColorAllocate($graph,255,0,0);
$fiolet = ImageColorAllocate($graph,148,0,254); 
$niebieski = ImageColorAllocate($graph,0,0,255); 
$wyniki=file("wyniki.txt");
$wynik1=$wyniki[0];
$wynik2=$wyniki[1];
$wynik3=$wyniki[2];
$wynik4=$wyniki[3];
$wynik5=$wyniki[4];
$wszystko=$wynik1+$wynik2+$wynik3+$wynik4+$wynik5;
$szer1=600*$wynik1/$wszystko;
$szer2=600*$wynik2/$wszystko;
$szer3=600*$wynik3/$wszystko;
$szer4=600*$wynik4/$wszystko;
$szer5=600*$wynik5/$wszystko;
Imagefilledrectangle($graph,0,0,$szerokosc,$wysokosc,$bialy);
Imagerectangle($graph,1,1,($szerokosc-2),($wysokosc-2),$czarny);
Imagefilledrectangle($graph,30,100,($szer1+30),130,$rozowy);
Imagefilledrectangle($graph,30,200,($szer2+30),230,$czerwony);
Imagefilledrectangle($graph,30,300,($szer3+30),330,$fiolet);
Imagefilledrectangle($graph,30,400,($szer4+30),430,$niebieski);
Imagefilledrectangle($graph,30,500,($szer5+30),530,$czarny);
Imagestring ($graph,4,($szer1+40), 100 , $wynik1 , $czarny);
Imagestring ($graph,4,($szer2+40), 200 , $wynik2 , $czarny);
Imagestring ($graph,4,($szer3+40), 300 , $wynik3 , $czarny);
Imagestring ($graph,4,($szer4+40), 400 , $wynik4 , $czarny);
Imagestring ($graph,4,($szer5+40), 500 , $wynik5 , $czarny);
Imagestring ($graph,2,30, 80 , "2" ,$czarny );
Imagestring ($graph,2,30, 180 , "3" ,$czarny );
Imagestring ($graph,2,30, 280 , "4",$czarny );
Imagestring ($graph,2,30, 380 , "5",$czarny );
Imagestring ($graph,2,30, 480 , "Nie wiem",$czarny );
imagettftext ($graph,20,0,300,60,$czarny,"arial.ttf","Ankieta");
Header("Content-type: image/png"); 
ImagePng($graph); 
ImageDestroy($graph); 
?> 

When I vote for 5th option wyniki.txt looks like below

0

0
0
0
1

What is wrong? Where come this empty row comes?

1

There are 1 answers

3
Jay Blanchard On BEST ANSWER

Remove the "\n" in this line $koncowo=$wyniki[0]."\n"


Since all of your new values will need a new line you should do something like this to make sure each line is handled properly:

$koncowo = array($wyniki[0], $wyniki[1], $wyniki[2], $wyniki[3], $wyniki[4]);
$contents = implode(PHP_EOL, $koncowo); // add an EOL to each array member
file_put_contents('wyniki.txt',$contents);