Form Return Incorrect

0

Hello

From a csv file, because after doing submit on the form I do not have the return with the correct values? If%% of countries is equal $_POST , I wanted you to return the $ registry [0] and $registo[1] corresponding to that $registo[2]

<?php
$br = "</br>";
$file = fopen("paises.txt", "r");

$codigo = (isset($_POST['codigo']) ? $_POST['codigo'] : null);
$pais = (isset($_POST['pais']) ? $_POST['pais'] : null);
$capital = (isset($_POST['capital']) ? $_POST['capital'] : null);
$cod = "";
$cap = "";

if (array_key_exists("pais", $_POST)) {
    echo "<table border='1'><th>Pais</th>";
    while (!feof($file)) {
        $registo = fgetcsv($file);

        if ($pais = $registo[1]) {

            $cod = $registo[0];
            $cap = $registo[2];
        }

        echo"<tr>";
        echo"<td>" . $registo[1] . "</td>";
        echo"</tr>";
    }
    echo "</table>";
    fclose($file);
}
?>

<?php
$br = "</br>";

if ($_POST) {
    echo "Codigo do pais escolhido: " . $cod . "<br>";
    echo "Capital: " . $cap . " - " . $registo[1] . "-" . $cod . "<br>";
} else {
    ?>

    <form action="eta15_2teste.php" method="post">
        Pais: <input type="text" name="pais"><br>
        <input type="Submit" name="enviar" value="Enviar">
    </form>

    <?php
}
?>

Always return the values of registo[1] and $registo[0] of the last line of the file

    
asked by anonymous 29.10.2016 / 17:14

1 answer

1

The problem is in if within its while , it is assigning the value of $registo[1] to the variable $pais every time a line is read in the file, so it displays only the value of the last line, change the assignment signal ( = ) to a comparison ( == ), so the value of the variables $cod and $cap will only be defined when the condition is satisfied

    
04.11.2016 / 00:07