<?php
//include'session.php';
include'../../connection.php';
//include'../functions.php';

$quDataMonth = "SELECT Rechargedate FROM recharge_backup ORDER BY Id ASC LIMIT 1";
$reDataMonth = mysqli_query($con,$quDataMonth);
$DataMonth   = mysqli_fetch_array($reDataMonth)[0];
$newDate     = date_create($DataMonth);

$monthNo     = date_format($newDate, "m");
$year        = date_format($newDate, "Y");

$fdate = $year."-".$monthNo."-01";
$ldate = $year."-".$monthNo."-31";
$fileDate = $year.$monthNo;

$output = '';

    if(1==1)
    {
        $quMain = "SELECT * FROM recharge_backup WHERE Rechargedate>='$fdate' AND Rechargedate<='$ldate'";
        $reMain = mysqli_query($con, $quMain);
        if(mysqli_num_rows($reMain) > 0)
        {
            $output .= '<table class="table" Border="1" cellpadding="10"> 
                    <tr>';
                    $sql = "DESCRIBE recharge";
                    $result = $con->query($sql);

                    while($row = $result->fetch_assoc()) 
                    {
                        $output .= '<th>'.$row['Field'].'</th>';
                    }

                        
                    $output .= '</tr>';
            while($dataMain = mysqli_fetch_array($reMain))
            {
                $colCount = count($dataMain);
                
                $output .= ' <tr>';
                
                for($x=0; $x<$colCount; $x++)
                {
                    $output.= '<td>'.$dataMain[$x].'</td>';
                }
                $output.='</tr>';
            }
            $output .= '</table>';
            $fileName=$fileDate."_recharge_old";
            header('Content-Type: application/xls');
            header('Content-Disposition: attachment; filename='.$fileName.'.xls');
            echo $output;
        }
    }    
?>
