Find what there is to modify when reading in a txt file

0

I have a txt file that contains this:

  

How many times do you access the Internet?

     

More than once a day | 32

     

Once a day | 12

     

Once a week | 16

     

Once a month | 14

     

I do not agree | 15

And in the index.php:

<html>
    <head>
        <title>Encuesta</title>
    </head>
    <body bgcolor=91E5F2>
    <center>
        <table border=1><tr><td>
            <table width=100% border=0 cellspacing=0 cellpadding=10>
                <tr><td colspan=2 bgcolor=CCCCCC><font size=3><b>Encuesta</b></td></tr>
                <tr><td bgcolor=FFFFFF><font size=3>
                    <form action="index.php" method="post">
                        <?php
                            $id_fichero=fopen("encuesta.txt","r")or die("<b>No se ha podido abrir el fichero 'encuesta.txt'.</b><p>");
                            $cont=0;
                            while(!feof($id_fichero)){
                                $linea=fgets($id_fichero);
                                if($cont==0){
                                    echo "<br><font size=3><b>".$linea."</b></font><br>";
                                }else{
                                    echo "<input type='radio' name='voteID' value=".$cont.">".substr($linea,0,-5)."<br>";
                                }
                                $cont++;
                            }
                            fclose($id_fichero);
                        ?>
                        <td align=left bgcolor=FFFFFF><input type=submit name="votar" value="Votar"></td>
                    </form>
                    </center>
                </tr>
            </table>
        </td></tr></table>
        <p>
        <?php
            if(isset($_POST['votar'])){
                $op=$_POST['voteID'];
                $id_fichero=fopen("encuesta.txt","r+")or die("<b>No se ha podido abrir el fichero 'encuesta.txt'.</b><p>");
                $cont=0;
                while(!feof($id_fichero)){
                    $linea=fgets($id_fichero);
                    if($cont!=0){
                        switch($op){
                            case 1:
                                if(substr($linea,0,-2)){

                                }
                                break;
                            case 2:

                                break;
                            case 3:

                                break;
                            case 4:

                                break;
                            case 5:

                                break;
                        }
                    }
                            $cont++;
                }
                fclose($id_fichero);
                /*echo "<b><u>Resultados</b></u>";
                echo "<br>";
                echo "<img height=15 width=$por1 src=barra.jpg>";//muestra opcion 1 con barra de imagen
                echo "<br>";
                echo "A: <b>$total1</b> votos - <b>$por1 %</b>";//opcion 1 con numero de votos y porcentaje
                echo "<br><br>"; 
                echo "<img height=15 width=$por2 src=barra.jpg>";//muestra opcion 2 con barra de imagen
                echo "<br>";
                echo "B: <b>$total2</b> votos - <b>$por2 %</b>";//opcion 2 con numero de votos y porcentaje
                echo "<br><br><br>";
                echo "Total Votos: <b>$votos</b>";*/
            }
        ?>
    </body>
</html>

I need that, in the switch, if it enters in the corresponding case, read the piece of text of the option and I increase in one the amount of the figure that is behind the text. In the first case I started the if, but I do not know how to put it.

    
asked by Charly Utrilla 11.10.2018 в 19:00
source

1 answer

1

You can use the PHP explode function that separates the string for a character. That way, explode('|',$linea) will result in an arrangement of two elements, where the first is the text and the second is the number.

    
answered by 11.10.2018 в 19:26