/** * Exports table specific data to excel file according to some condition * * @param string $table table from which the data will be exported * @param string $fields (Optional) A comma separated list of fields to export from the specified table * @param string $condition (Optional) Free condition according to which data will be retrieved * @param string $orderBy (Optional) A comma separated list of fields to order exported data according to it * @param string $outputFilename (Optional) The name of the excel file to be exported */ function exportDataToExcel($table, $fields = "*", $condition = "", $orderBy = "", $outputFilename = "default.xls") { $sql = "SELECT {$fields} FROM `{$table}` "; $sql .= $condition != "" ? " WHERE " . $condition : ""; $sql .= $orderBy != "" ? " ORDER BY " . $orderBy : ""; if (exportSqlToExcel($sql, $outputFilename) == false) { return false; } }
include_once "../common/top.php"; //If SQL statement is provided use it directly if (isset($_POST['SQL'])) { if ($_POST['SQL'] != "") { if (!isset($_POST['outputFilename'])) { $outputFilename = "default.xls"; } else { if ($_POST['outputFilename'] == "") { $outputFilename = "default.xls"; } else { $outputFilename = $_POST['outputFilename']; } } $sql = $_POST['SQL']; //Export data . . exportSqlToExcel($sql, $outputFilename); //End script . . exit; } } //Check values and set defaults . . if (!isset($_POST['table'])) { echo "Nothing to export!"; exit; } else { if ($_POST['table'] == "") { echo "Nothing to export!"; exit; } else { $table = $_POST['table']; }