Export Excel Data PHP Variable Workbench

2

I'm having second thoughts about generating an excel with the query.

<?php
include("conectar.php");
$id = $_GET['id'];
$arquivo = 'Não autorizado .xls';

First question: In the $ share part, can I put a variable to rename the file?

// -- Cabeçalho do arquivo -------------------------------------
$html .= '<tr><td align="center"><b>Instalador</b></td>';
$html .= '<td align="center"><b>Morada</b></td>';
$html .= '<td align="center"><b>Email</b></td>';
$html .= '<td align="center"><b>Validade</b></td>';

</tr>';

for($i=1; $i<=$aux; $i ++){
$sql = mysql_query("SELECT tb_trabalhador.Nome,Morada,Email,Validade from             
          tb_detalhe_trabalhador inner join tb_trabalhador on 
          tb_detalhe_trabalhador.id = tb_trabalhador.id inner join 
          tb_equipamentos on tb_detalhe_trabalhador.id = tb_equipamentos.id 
          inner join tb_funcoes on tb_detalhe_trabalhador.id = tb_funcoes.id
          WHERE tb_trabalhador.id = $id = ".$i) or die(mysql_error());
          $row=mysql_fetch_array($sql);
          $html .= '<tr><td>'.$row[0].'</td><td>'.$row[1].'</td>
          <td>'.$row[2].'</td><td>'.$row[3].'</td></tr>';
  }
  $html .= '</table>';

Second question: I'm going to export data to Excel with many dates. Is it possible for me to export the data to Excel and where dates have been passed in relation to today's date to be a red rectangle?

    
asked by anonymous 28.05.2014 / 10:45

1 answer

1

No Code

<?php
include("conectar.php");
$id = $_GET['id'];
$arquivo = $_GET['arquivo'];

You can use another $_GET['arquivo'] and assign the $arquivo variable so that you can put the name of your preference. Note: I would use filter_input for security reasons.

Example with filter_input

<?php
    include("conectar.php");
    $id      = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT)
    $arquivo = filter_input(INPUT_GET, 'arquivo', FILTER_SANITIZE_STRING)

And in the code, apparently it is by the Validity field so it is row[3] I compared the current date with the date that comes from the database ( date('Y-m-d') ), I can not know if the code has an error, I just made the adptation.

<?php

// -- Cabeçalho do arquivo -------------------------------------
$html .= '<tr><td align="center"><b>Instalador</b></td>';
$html .= '<td align="center"><b>Morada</b></td>';
$html .= '<td align="center"><b>Email</b></td>';
$html .= '<td align="center"><b>Validade</b></td>';

$html .= '</tr>';

for($i=1; $i<=$aux; $i ++){
        $sql = mysql_query("SELECT tb_trabalhador.Nome,Morada,Email,Validade from             
              tb_detalhe_trabalhador inner join tb_trabalhador on 
              tb_detalhe_trabalhador.id = tb_trabalhador.id inner join 
              tb_equipamentos on tb_detalhe_trabalhador.id = tb_equipamentos.id 
              inner join tb_funcoes on tb_detalhe_trabalhador.id = tb_funcoes.id
              WHERE tb_trabalhador.id = $id = ".$i) or die(mysql_error());

          $row=mysql_fetch_array($sql);       

          $html .= '<tr><td>'.$row[0].'</td><td>'.$row[1].'</td><td>'.$row[2].'</td>';
          //SE FOR DATA IGUAL A DE HOJE PINTE AS BORDAS DE VERMELHO
          if (date('Y-m-d') == $row[3]){
            $html .= '<td bordercolor="#FF0000" style="border:1px solid #FF0000; border-color:#FF0000">'.$row[3].'</td>';
          } else {
            $html .= '<td>'.$row[3].'</td>';
          }
          $html .= '</tr>';

  }
$html .= '</table>';
    
28.05.2014 / 16:29