Popular table in page loading

Asked

Viewed 93 times

0

Hello. I am creating a page of registration of people, every click on the save button is generated a csv file that the next page load should be used to show on the screen the already made entries, so that the user can know who is already registered and does not duplicate the process. I’m not getting the data saved in csv shown in their respective fields after the page is loaded. If anyone can help, thank you. 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>
    <script src="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>
    <script type="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>

inserir a descrição da imagem aqui

3 answers

1

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

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

you can’t do it, the names must be identical

and there are still other mistakes

0

I made other adjustments for you, it’s working, as I believe, that you wanted.

<?php 

if ($_POST){

$name1 = $_POST['name'];

$telpref = $_POST['tel'];

$origem1 = $_POST['function'];

$destino1 = $_POST['sector'];

//$quant_lines = Count($name);

$data .= " $name1,";

$data .= " $telpref,";

$data .= " $origem1,";

$data .= " $destino1";

$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);

}

?>

  • Thanks for the help. So what you’ve given me is doing the same thing I’ve been able to echo in the csv file_get_contents. It plots what is saved in csv out of the corresponding inputs. I wanted him to plot in, like in the name have three saved lines, one written mary, another john and another ze and so on. I edited the post with an image for better viewing.

0

inserir a descrição da imagem aqui

that wouldn’t be it ?

try to get something like this on your table

    $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);  
  • No. I wanted the data to be saved inside the inputs (in the square referring to each parameter). That way I managed to do, I don’t know how to put inside the input. I had thought of making an array in which I could keep the column index and vary the row. Thus, I would always have to value [0] corresponding to the column of the array would be a name. This part I managed to do, I will edit the file again and put, I just don’t know how to put inside the input box the value.

  • what input you want to put ?

  • Like, I have the little square of the name, if I had 3 names saved in csv, for example, I wanted to add 3 lines with the names saved when apágina was reloaded. That’s for all four fields. Then the table I made would have the three rows of data saved + the blank line to add. He was able to understand more or less?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.