<?php

// Database configuration
include ('../inc/dbcon.php');
$con->set_charset("utf8");


// Get All Table Names From the Database
$tables = array();
$sql = "SHOW TABLES";
$result = mysqli_query($con, $sql);



while ($row = mysqli_fetch_row($result)) {
    $tables[] = $row[0];
}


$sqlScript = "";
foreach ($tables as $table) {

    if($table=='tbl_acledger'||$table=='tbl_acmobile '||$table=='tbl_bacount '||$table==' 	tbl_bank '||$table=='tbl_banktra '||$table=='tbl_banktrade '||$table==' 	tbl_branch '||$table=='tbl_brand '||$table=='tbl_category '||$table=='tbl_cgroup '||$table=='tbl_cheqman '||$table=='tbl_customer '||$table==' 	tbl_expenses '||$table=='tbl_expensesde '||$table==' 	tbl_item '||$table==' 	tbl_parent  '||$table=='tbl_journalde '||$table=='tbl_payvoucher '||$table==' 	tbl_payvoucherde'||$table==' 	tbl_recvoucher '||$table=='tbl_recvoucherde '||$table=='tbl_serial '||$table=='tbl_serialsale '||$table==' 	tbl_stock '||$table==' 	tbl_subcat '||$table==' 	tbl_supplier '||$table==' 	tbl_trarecord '||$table=='tbl_purchase'||$table=='tbl_sales'||$table=='tbl_traproduct'){
    // Prepare SQLscript for creating table structure
    $query = "SHOW CREATE TABLE $table";
    $result = mysqli_query($con, $query);
    $row = mysqli_fetch_row($result);
    
    $sqlScript .= "\n\n" . $row[1] . ";\n\n";
    
    
    $query = "SELECT * FROM $table";
    $result = mysqli_query($con, $query);
    
    $columnCount = mysqli_num_fields($result);
    
    // Prepare SQLscript for dumping data for each table
    for ($i = 0; $i < $columnCount; $i ++) {
        while ($row = mysqli_fetch_row($result)) {
            $sqlScript .= "INSERT INTO $table VALUES(";
            for ($j = 0; $j < $columnCount; $j ++) {
                $row[$j] = $row[$j];
                
                if (isset($row[$j])) {
                    $sqlScript .= '"' . $row[$j] . '"';
                } else {
                    $sqlScript .= '""';
                }
                if ($j < ($columnCount - 1)) {
                    $sqlScript .= ',';
                }
            }
            $sqlScript .= ");\n";
        }
    }
    
    $sqlScript .= "\n"; 

    }
   
}

if(!empty($sqlScript))
{
    // Save the SQL script to a backup file
    $backup_file_name = $dbname . '_backup_' . time() . '.sql';
    $fileHandler = fopen($backup_file_name, 'w+');
    $number_of_lines = fwrite($fileHandler, $sqlScript);
    fclose($fileHandler); 

    // Download the SQL backup file to the browser
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . basename($backup_file_name));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($backup_file_name));
    ob_clean();
    flush();
    readfile($backup_file_name);
    unlink($backup_file_name);
    exec('rm ' . $backup_file_name); 
}
?>