I am using fputcsv
to export my database into the excel format. Currently it is showing all fields, including the id from the database. But I want Serial Numbers in the first column of the exported CSV file. The Database id series may change if some records are deleted from database. So I Need Serial numbers.
Is it possible ?
My current code is -
require_once("../db.php");
$contents="Database Id,Name,Amount,Cheque/Cash,Bank,Cheque Date,Branch,Address,Receipt No,Receipt Date\n";
$user_query = mysql_query("SELECT * from tablename ORDER BY RAND()");
$contents = strip_tags($contents);
header("Content-Disposition: attachment; filename=my_file_".date('d-F-Y').".csv");
$out = fopen('php://output', 'w');
fputcsv($out, array('Database Id', 'NAME', 'AMOUNT', 'CHEQUE/CASH DETAILS' , 'BANK' , 'CHEQUE DATE' , 'BRANCH', 'ADDRESS', 'RECEIPT NUMBER', 'RECEIPT DATE'));
while ($row = mysql_fetch_assoc($user_query)) {
fputcsv($out, $row);
}