Save table values in Excel

1

I have all the necessary code but this is giving an error and has to do with $i . How can I fix it?

<?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:

    
asked by anonymous 24.10.2016 / 17:49

2 answers

1

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

This error happens because this array with that key has not been declared yet, so you're concatenating an array with a key that does not "exist." To get this, just 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>";
...

This is a notice error, meaning your script was probably working normally. But it's cool to sort these things out. =)

    
24.10.2016 / 23:28
-1

Put $html = []; , before for .

    
24.10.2016 / 18:02