Save all PHP results to a single HTML table

Asked

Viewed 193 times

0

On my host there is an index.html with a form that is linked to a login.php that takes the results that the user typed and saves in an HTML table at the site root. 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’m very young, I researched a lot and I couldn’t solve it. 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");

1 answer

0


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

but within this scenario I believe you can remove the last 2 tags that indicate the end of the table, and continue your writing. But you would have to check whether 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");
  • Perfect friend! It worked perfectly, thank you!

  • The right solution depends on each case. There are cases that are quite obvious, but the one presented is not, it is not known what the purpose is to save html in this way. Therefore, it is irrelevant to say that the appropriate would be to use databases.

Browser other questions tagged

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