Export Excel PHP Workbench Variable data

Asked

Viewed 282 times

2

I’m having doubts regarding generating an excel with the query.

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

First question is this: In the $file part I can 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>';

The second question is: I will export data to Excel with many dates. It is possible for me to export the data to Excel and where the dates have been passed in relation to today’s day become a red rectangle?

  • Your very broad question could be more specific?

  • What is the doubt? Can you tell me to try to explain better?

  • Well I have the routine that does this, takes a data from the Mysql database and generates a xls ouput, which you get as a download via browser, all generated by PHP... would that be it? You need a code that generates an xls (excel)?

  • I have the code that gives me Excel. But when generating Excel it has to have a name, in that name I want to put the name of a variable

  • Of course, it can, but to get to this page and this code as you do, and in your question you have two doubts? On Monday if you’re going to have to change the code, wouldn’t it be nice to send your code in the body of the question?

  • I have now placed in the body of the question the code

Show 1 more comment

1 answer

1

In the Code

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

you can use another $_GET['arquivo'] and assign the variable $arquivo so that you can put the name of your preference. NOTE: I would 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’s the Validity field so it’s the row[3] I compared the current date with the date coming from the bank (date('Y-m-d')), there’s no way I know if the code has an error, I just did the actual processing.

<?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>';
  • Just one question. In the part of comparing the date do I not have to make a comparison with today’s date? IF date now() < $Row[3]

  • if (date('Y-m-d') == $row[3]){ here is the conference of Date, understood ? Search in the code doing favor

  • <a id="link_opens" href="Excelnaauthorized.php? id=<? php echo $_GET['id']; ? >"> only with this information I can’t get the Name of this id?

  • Can you id if $_GET['id'] is filled in, ! if you want file this link would be: <a id="link_opens" href="Excelnaauthorized.php? id=<? php echo $_GET['id']; ? >&file=<? php echo $_GET['file']; ? >"> ... How do you generate this link?

  • The first link looks like this: echo '<li class="ui-widget-content"><H3><a href="Naoautorizadomostrar.php? id='. $displays['id']. '">'. $displays['Name']. '</H3></a></li>';

  • <li class="ui-widget-content"><H3><a href="Naoautorizadomostrar.php? id='. $displays['id']. '&file='. $displays['Name']. '">'. $displays['Name']. '</H3></a>< /li> will come out with the name !!! now

Show 1 more comment

Browser other questions tagged

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