Popular table in page load

0

Hello. I am creating a people registration page, with each click on the save button, a csv file is generated, which on the next page loading should be used to show the already done entries on the screen, so that the user can know who has already is registered and does not duplicate the process. I am not able to get the data saved in csv to be shown in their respective fields after the page loads. If anyone can help, I appreciate it. The code I have so far is this:

     <?php
    //$csv = file_get_contents("/var/www/html/dados.csv");
    //echo $csv;
    //$multicasts = str_getcsv ($csv);
$meuArray = Array();
$file = fopen('dados.csv', 'r');
while (($line = fgetcsv($file)) !== false)
{
  $meuArray[] = $line;
}
fclose($file);
print_r($meuArray);
    ?>

    <html>
    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Cadastro</title>

    <style type="text/css" media="all">
      body{ font-family:Arial, Helvetica, sans-serif }
      #tudo{ border:#CCCCCC 1px solid;width:550px;margin:0 auto }
      .bd_titulo{
        text-align:center;
        background-color:#CCCCCC;
        font-weight:bold
      }
    </style>

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script><scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://rawgit.com/RobinHerbots/jquery.inputmask/3.x/dist/jquery.inputmask.bundle.js"></script><scripttype="text/javascript">

     $(document).ready(function() {
          $(".telefone").inputmask({
            mask: ["(99)99999-9999"],
            keepStatic: true
          });
        });

    $(function () {
      $(".adicionarCampo").click(function () {
          novoCampo = $("tr.linhas:first").clone();
          novoCampo.find("input").val("");
          novoCampo.insertAfter("tr.linhas:last");
        $(".telefone").inputmask({
            mask: ["(99)99999-9999"],
            keepStatic: true
          });
      });
    });
    </script>

    </head>
    <body>

    <form method="post" name="cadastro" action="">
    <div id="tudo">
    <table id = "multiTable" border="0" cellpadding="2" cellspacing="4">
      <tr><td colspan="4" class="bd_titulo">Cadastro-Pessoas</td></tr>
      <tr><td class="bd_titulo" width="10">Nome</td><td class="bd_titulo">Telefone</td><td class="bd_titulo">Função</td><td class="bd_titulo">Setor</td></tr>
      <tr class="linhas">

        <td><input type="text" name="nome[]" style="text-align:center" /></td>
        <td><input type="text" name="telefone[]" class = "telefone"/></td>
        <td>
          <select name="funcao[]">
          <option>Selecione</option>
          <option value="vendedor" >vendedor</option>
          <option value="gerente" >gerente</option>
          <option value="assistente" >assistente</option>          
          </select>
        </td>
        <td><select name="setor[]">
          <option>Selecione</option>
          <option value="loja" >loja</option>
          <option value="escritório" >escritório</option>        
          </select>
        </td></td>
        </tr>

      <tr><td colspan="4">
            <a href="#" class="adicionarCampo" title="Adicionar item"><img src="add.svg" border="0" /></a>
        </td></tr>

        <tr>
      <td align="center" colspan="0">
            <td align="right" colspan="4"><input type="submit" id="Salvar" value="Salvar" /></td>
      </tr>

    </table>
    </form>
    </div>

    <?php 
     if ($_POST){
       $nome = $_POST['nome'];
       $telefone = $_POST['telefone'];
       $funcao = $_POST['funcao'];
       $setor = $_POST['setor'];

       $quant_linhas = count($nome);
       $dados = "";

       for ($i=0; $i<$quant_linhas; $i++) {
            $dados .=  "$nome[$i],";
            $dados .= "$telefone[$i],";
            $dados .= "$funcao[$i],";
            $dados .= "$setor[$i]";
            $dados .= "\n";
          } 

      $fileName = "dados.csv"; 
      $fileHandle = fopen($fileName,"w"); 
      fwrite($fileHandle,"$dados"); 
      fclose($fileHandle);
      $file = fopen($fileName, 'r'); 
      fclose($file);

    }
    ?>
    </body>
    </html>

    
asked by anonymous 16.07.2018 / 15:25

3 answers

1

The names of your inputs are different from the keys you are using in php,

no input this 'name []' in the key this 'name'

You can not do this, the names must be identical

and there are still other errors

    
16.07.2018 / 16:12
0

I made other adjustments for you, it works, as I believe, that you wanted.

<?php 

if ($ _POST) {

$ name1 = $ _POST ['name'];

$ telpref = $ _POST ['tel'];

$ source1 = $ _POST ['function'];

$ destination1 = $ _POST ['sector'];

// $ quant_lines = count ($ name);

$ data.="$ name1,";

$ data.="$ telpref,";

$ data.="$ source1,";

$ data.="$ target1";

$ data.="\ n";

$ fileName="data.csv";

$ fileHandle = fopen ($ fileName, "a");

fwrite ($ fileHandle, "$ data");

fclose ($ fileHandle);

$ file = fopen ($ fileName, 'r'); while (($ line = fgetcsv ($ file))! == FALSE) {   // $ line is an array of the csv elements   echo $ line [0]. ','. $ line [1]. ','. $ line [2]. ','. $ line [3]. '

'; } fclose ($ file);

}

? >

    
16.07.2018 / 16:43
0

Wouldnotthatbeit?

trytofixsomethinglikethisinyourtable

$file=fopen("dados.csv", 'r');
    while (($line = fgetcsv($file)) !== FALSE) {
    //$line is an array of the csv elements
    echo '<tr>'. 
        $line[0].', '.$line[1].', '.$line[2].', '.$line[3].'</p>'
        .'<tr>';
    }
    fclose($file);  
    
16.07.2018 / 19:14