Save table values in Excel

Asked

Viewed 54 times

1

I’ve got all the code you need, but it’s a mistake and it has to do with $i. how can I correct?

<?php 
include('conetar.php');
$query = "SELECT nome, contacto  FROM utilizadores";
$executar_query = mysqli_query($conn, $query);
$contar = mysqli_num_rows($executar_query);

for($i=0;$i<1;$i++){   
$html[$i] = "";
    $html[$i] .= "<table>";
    $html[$i] .= "<tr>";
    $html[$i] .= "<td><b>Nome</b></td>";
    $html[$i] .= "<td><b>contacto</b></td>";
    $html[$i] .= "</tr>";
    $html[$i] .= "</table>";
}

$i = 1;
while($ret = mysqli_fetch_array($executar_query)){
    $retorno_nome = $ret['nome'];
    $retorno_contacto = $ret['contacto'];
    $html[$i] .= "<table>";
    $html[$i] .= "<tr>";
    $html[$i] .= "<td>".$retorno_nome."</td>";
    $html[$i] .= "<td>".$retorno_contacto."</td>";
    $html[$i] .= "</tr>";
    $html[$i] .= "</table>";
    $i++;
}

$arquivo = 'soudev.xlsx';
header ("Expires: Mon, 26 Jul 2100 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header ("Content-type: application/x-msexcel");
header ("Content-Disposition: attachment; filename={$arquivo}" );
header ("Content-Description: PHP Generated Data" );


for($i=0;$i<=$contar;$i++){  
    echo $html[$i];
}


 ?>

IMAGE:
inserir a descrição da imagem aqui

2 answers

1

Line 21, from what I told you, stays here: $html[$i] .= "<table>";.

This error happens because this array with that key has not been declared yet, so you are concatenating an array with a "non-existent" key. To fix this, simply declare that array with the key before concatenating the values.

...
$html[$i] = ""; // aqui está declarado o array com a chave determinada 
$html[$i] .= "<table>";
$html[$i] .= "<tr>";
...

That’s a mistake like notice, that is, your script was probably working normally. But it’s nice to pack these little things. =)

  • just change $html[$i] .= "<table>"; for $html[$i] = "<table>" em ambas as tags <table>`

  • Yes, it will also work. But I believe my answer is correct. Just look at my explanation.

  • Including, in the for you did just that: $html[$i] = "";. I just followed your line of reasoning. I think you should approve my answer. Hug.

  • You can also put mine as a good doubt because so the exportar for xls works perfectly and will know how to use.

-1

Place $html = [];, before the for.

Browser other questions tagged

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